aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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