1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/* MAL2Go - MyAnimeList V2 API wrapper for Go
* Copyright (C) 2022 Vidhu Kant Sharma <vidhukant@protonmail.ch>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
package manga
import (
e "github.com/MikunoNaka/MAL2Go/errhandlers"
"errors"
"fmt"
"net/url"
"strconv"
)
// generate the endpoint url with the manga id
func endpointGenerator(id int) string {
return fmt.Sprintf(BASE_URL + "/manga/%d/my_list_status", id)
}
// update just an manga's status
func (c Client)SetStatus(id int, status string) (serverResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified list status is valid
if !e.IsValidMangaListStatus(status) {
return serverResponse{}, errors.New(fmt.Sprintf("SetStatus: Invalid list status: \"%s\"", status))
}
// data to be sent to the server
params := url.Values{}
params.Set("status", status)
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just a manga's num of volumes read
func (c Client)SetVolumesRead(id int, volumes int) (serverResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
params := url.Values{}
params.Set("num_volumes_read", strconv.Itoa(volumes))
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just a manga's num of chapters read
func (c Client)SetChaptersRead(id int, chapters int) (serverResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
params := url.Values{}
params.Set("num_chapters_read", strconv.Itoa(chapters))
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just a manga's rereading status
func (c Client)SetIsRereading(id int, isRereading bool) (serverResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
params := url.Values{}
params.Set("is_rereading", strconv.FormatBool(isRereading))
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just the manga's score
func (c Client)SetScore(id int, score int) (serverResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified score is valid
if !e.IsValidScore(score) {
return serverResponse{}, errors.New(fmt.Sprintf("SetScore: Invalid score: %d doesn't lie within 0-10", score))
}
// data to be sent to the server
params := url.Values{}
params.Set("score", strconv.Itoa(score))
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just a manga's priority
func (c Client)SetPriority(id int, priority int) (serverResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified priority is valid
if !e.IsValidPriority(priority) {
return serverResponse{}, errors.New(fmt.Sprintf("SetPriority: Invalid priority: %d doesn't lie within 0-2", priority))
}
// data to be sent to the server
params := url.Values{}
params.Set("priority", strconv.Itoa(priority))
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just a manga's reread value
func (c Client)SetRereadValue(id int, rereadValue int) (serverResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified reread value is valid
if !e.IsValidRewatchValue(rereadValue) {
return serverResponse{}, errors.New(fmt.Sprintf("SetRereadValue: Invalid rewatch value: %d doesn't lie within 0-5", rereadValue))
}
// data to be sent to the server
params := url.Values{}
params.Set("reread_value", strconv.Itoa(rereadValue))
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just a manga's reread count
func (c Client)SetRereadCount(id int, rereadCount int) (serverResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
params := url.Values{}
params.Set("num_times_reread", strconv.Itoa(rereadCount))
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just a manga's tags
func (c Client)UpdateTags(id int, tags string) (serverResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
params := url.Values{}
params.Set("tags", tags)
// make API request
return c.putRequestHandler(endpoint, params), nil
}
// update just a manga's comments
func (c Client)UpdateComments(id int, comments string) (serverResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
params := url.Values{}
params.Set("comments", comments)
// make API request
return c.putRequestHandler(endpoint, params), nil
}
/* NOTE: This will overwrite everything
* i won't use it.. but it's pretty flexible
* so this will stay here */
// Update/Add a manga to user's manga list
func (c Client)UpdateManga(id int, data UpdateMangaData) (serverResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified list status is valid
if !e.IsValidMangaListStatus(data.Status) {
return serverResponse{}, errors.New(fmt.Sprintf("UpdateManga: Invalid list status: \"%s\"", data.Status))
}
// checks if specified score is valid
if !e.IsValidScore(data.Score) {
return serverResponse{}, errors.New(fmt.Sprintf("UpdateManga: Invalid score: %d doesn't lie within 0-10", data.Score))
}
// checks if specified priority is valid
if !e.IsValidPriority(data.Priority) {
return serverResponse{}, errors.New(fmt.Sprintf("UpdateManga: Invalid priority: %d doesn't lie within 0-2", data.Priority))
}
// checks if specified rewatch value is valid
if !e.IsValidRewatchValue(data.RereadValue) {
return serverResponse{}, errors.New(fmt.Sprintf("UpdateManga: Invalid reread value: %d doesn't lie within 0-5", data.RereadValue))
}
params := url.Values{}
/* NOTE: THIS WILL OVERWRITE ANY DATA THAT
* IS NOT SPECIFIED AND SET IT TO NULL */
params.Set("status", data.Status)
params.Set("is_rereading", strconv.FormatBool(data.IsRereading))
params.Set("score", strconv.Itoa(data.Score))
params.Set("num_chapters_read", strconv.Itoa(data.ChaptersRead))
params.Set("priority", strconv.Itoa(data.Priority))
params.Set("num_times_reread", strconv.Itoa(data.TimesReread))
params.Set("reread_value", strconv.Itoa(data.RereadValue))
params.Set("tags", data.Tags)
params.Set("comments", data.Comments)
// make API request
return c.putRequestHandler(endpoint, params), nil
}
|