aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--errhandlers/validators.go24
-rw-r--r--user/anime/animelist.go28
-rw-r--r--user/anime/animelist.structs.go18
-rw-r--r--user/anime/request_handler.go25
4 files changed, 74 insertions, 21 deletions
diff --git a/errhandlers/validators.go b/errhandlers/validators.go
index 6bf7a3f..1be61cb 100644
--- a/errhandlers/validators.go
+++ b/errhandlers/validators.go
@@ -122,3 +122,27 @@ func IsValidListStatus(status string) bool {
}
return false
}
+
+// Checks if given anime score is valid
+func IsValidScore(score int) bool {
+ if score >= 0 && score <= 10 {
+ return true
+ }
+ return false
+}
+
+// Checks if given anime priority is valid
+func IsValidPriority(priority int) bool {
+ if priority >= 0 && priority <= 2 {
+ return true
+ }
+ return false
+}
+
+// Checks if given rewatch value is valid
+func IsValidRewatchValue(r int) bool {
+ if r >= 0 && r <= 5 {
+ return true
+ }
+ return false
+}
diff --git a/user/anime/animelist.go b/user/anime/animelist.go
index 78242ac..3065919 100644
--- a/user/anime/animelist.go
+++ b/user/anime/animelist.go
@@ -37,12 +37,31 @@ func (c AnimeListClient)DeleteAnime(id int) string {
}
// Update/Add an anime to user's anime list
-func (c AnimeListClient)UpdateAnime(id int, data UpdateAnimeData) serverResponse {
+func (c AnimeListClient)UpdateAnime(id int, data UpdateAnimeData) (serverResponse, error) {
endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id)
+ // checks if specified list status is valid
+ if !e.IsValidListStatus(data.Status) {
+ return serverResponse{}, errors.New(fmt.Sprintf("UpdateAnime: Invalid list status: \"%s\"", data.Status))
+ }
+
+ // checks if specified score is valid
+ if !e.IsValidScore(data.Score) {
+ return serverResponse{}, errors.New(fmt.Sprintf("UpdateAnime: 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("UpdateAnime: Invalid priority: %d doesn't lie within 0-2", data.Priority))
+ }
+
+ // checks if specified rewatch value is valid
+ if !e.IsValidRewatchValue(data.RewatchValue) {
+ return serverResponse{}, errors.New(fmt.Sprintf("UpdateAnime: Invalid rewatch value: %d doesn't lie within 0-5", data.RewatchValue))
+ }
+
// make API request
- res := c.putRequestHandler(endpoint, data)
- return res
+ return c.putRequestHandler(endpoint, data), nil
}
// Get authenticated user's anime list
@@ -50,8 +69,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i
var userAnimeList a.AnimeList
// error handling for limit and offset
limitsErr := e.LimitsErrHandler(limit, offset, maxListLimit)
- if limitsErr != nil {
- return userAnimeList, limitsErr
+ if limitsErr != nil { return userAnimeList, limitsErr
}
// checks if valid sort is specified
diff --git a/user/anime/animelist.structs.go b/user/anime/animelist.structs.go
index d874f7b..06c40d1 100644
--- a/user/anime/animelist.structs.go
+++ b/user/anime/animelist.structs.go
@@ -29,14 +29,14 @@ type AnimeListRaw struct {
}
type UpdateAnimeData struct {
- Status string `json:"status"`
- IsRewatching bool `json:"is_rewatching"`
- Score int `json:"score"`
- EpWatched int `json:"num_watched_episodes"`
- Priority int `json:"priority"`
- TimesRewatched int `json:"num_times_rewatched"`
+ Status string
+ IsRewatching bool
+ Score int
+ EpWatched int
+ Priority int
+ TimesRewatched int
// NOTE: idk what RewatchValue is
- RewatchValue int `json:"rewatch_value"`
- Tags string `json:"tags"`
- Comments string `json:"comments"`
+ RewatchValue int
+ Tags string
+ Comments string
}
diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go
index 6f29d88..e44dab1 100644
--- a/user/anime/request_handler.go
+++ b/user/anime/request_handler.go
@@ -2,8 +2,7 @@
* 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
+ * 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,
@@ -64,19 +63,31 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string {
// for PUT requests (used by UpdateAnime)
func (c AnimeListClient) putRequestHandler(endpoint string, updateData UpdateAnimeData) serverResponse {
// TODO: make this do other stuff
- p := url.Values{}
- p.Set("score", strconv.Itoa(updateData.Score))
- p.Set("num_watched_episodes", strconv.Itoa(updateData.EpWatched))
+ params := url.Values{}
+
+ /* NOTE: THIS WILL OVERWRITE ANY DATA THAT
+ * IS NOT SPECIFIED AND SET IT TO NULL */
+ params.Set("status", updateData.Status)
+ params.Set("is_rewatching", strconv.FormatBool(updateData.IsRewatching))
+ params.Set("score", strconv.Itoa(updateData.Score))
+ params.Set("num_watched_episodes", strconv.Itoa(updateData.EpWatched))
+ params.Set("priority", strconv.Itoa(updateData.Priority))
+ params.Set("num_times_rewatched", strconv.Itoa(updateData.TimesRewatched))
+ params.Set("rewatch_value", strconv.Itoa(updateData.RewatchValue))
+ params.Set("tags", updateData.Tags)
+ params.Set("comments", updateData.Comments)
+
+ paramsEncoded := params.Encode()
// generate request
- req, err := http.NewRequest(http.MethodPut, endpoint, strings.NewReader(p.Encode()))
+ req, err := http.NewRequest(http.MethodPut, endpoint, strings.NewReader(paramsEncoded))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", c.AuthToken)
// this makes the sending-data-to-server magic work
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- req.Header.Add("Content-Length", strconv.Itoa(len(p.Encode())))
+ req.Header.Add("Content-Length", strconv.Itoa(len(paramsEncoded)))
// do request
res, err := c.HttpClient.Do(req)