aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2023-08-15 14:19:33 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2023-08-15 14:19:33 +0530
commitf4051d90a3e0520017020a145c1ccd129bb27aa3 (patch)
tree056da5a42467bca5f4df82fed21d399fe935e838
parentd7cf6b9ed5828d8ca5d1e56adf00ff3fd6a5a4ab (diff)
added validators for updating anime and mangav0.0.3
-rw-r--r--anime_list.go27
-rw-r--r--errors.go2
-rw-r--r--manga_list.go27
-rw-r--r--request_handlers.go3
4 files changed, 53 insertions, 6 deletions
diff --git a/anime_list.go b/anime_list.go
index 9d0f370..440e672 100644
--- a/anime_list.go
+++ b/anime_list.go
@@ -77,10 +77,33 @@ func (c Client) GetAnimeList(animes *[]Anime, params *ListParams) (bool, error)
}
func (c Client) UpdateAnime(res *AnimeUpdateResponse, id int, params map[string]interface{}) error {
- // TODO: validate params
p := url.Values{}
for k, v := range params {
- p.Set(k, fmt.Sprint(v))
+ vString := fmt.Sprint(v)
+ var err error
+
+ // validate params
+ switch(k) {
+ case Status:
+ err = validateAnimeListStatus(vString)
+ case Score:
+ err = validateScore(v.(int))
+ case Priority:
+ err = validatePriority(v.(int))
+ case RewatchValue:
+ err = validateRewatchValue(v.(int))
+ case Tags, Comments, IsRewatching, EpisodesWatched, TimesRewatched:
+ // these ones don't need to be validated
+ err = nil
+ default:
+ err = ErrUnknownUpdateParam
+ }
+
+ if err != nil {
+ return err
+ }
+
+ p.Set(k, vString)
}
body, err := c.put(getUpdateQuery(id, "anime"), p)
diff --git a/errors.go b/errors.go
index 3c127b6..ad125c2 100644
--- a/errors.go
+++ b/errors.go
@@ -32,4 +32,6 @@ var (
ErrInvalidRereadValue = errors.New("mg: invalid reread value")
ErrEmptySearchString = errors.New("mg: invalid search string (empty string)")
ErrInvalidYear = errors.New("mg: invalid year (not integer)")
+ ErrUnknownUpdateParam = errors.New("mg: unknown update param")
+ ErrAnimeNotInList = errors.New("mg: anime is already not in list")
)
diff --git a/manga_list.go b/manga_list.go
index 1c04463..373c733 100644
--- a/manga_list.go
+++ b/manga_list.go
@@ -78,10 +78,33 @@ func (c Client) GetMangaList(mangas *[]Manga, params *ListParams) (bool, error)
}
func (c Client) UpdateManga(res *MangaUpdateResponse, id int, params map[string]interface{}) error {
- // TODO: validate params
p := url.Values{}
for k, v := range params {
- p.Set(k, fmt.Sprint(v))
+ vString := fmt.Sprint(v)
+ var err error
+
+ // validate params
+ switch(k) {
+ case Status:
+ err = validateAnimeListStatus(vString)
+ case Score:
+ err = validateScore(v.(int))
+ case Priority:
+ err = validatePriority(v.(int))
+ case RereadValue:
+ err = validateRereadValue(v.(int))
+ case Tags, Comments, IsRereading, ChaptersRead, VolumesRead, TimesReread:
+ // these ones don't need to be validated
+ err = nil
+ default:
+ err = ErrUnknownUpdateParam
+ }
+
+ if err != nil {
+ return err
+ }
+
+ p.Set(k, vString)
}
body, err := c.put(getUpdateQuery(id, "manga"), p)
diff --git a/request_handlers.go b/request_handlers.go
index 2c23afb..adf5535 100644
--- a/request_handlers.go
+++ b/request_handlers.go
@@ -88,8 +88,7 @@ func (c Client) delete(endpoint string) error {
// if anime wasn't in the list from the start
if res.StatusCode == 404 {
- // TODO: write better error message
- return errors.New("anime not in list")
+ return ErrAnimeNotInList
}
return nil