From a507674426a6bc149cdbc741cdb25f4ee66a56dd Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 14 Feb 2022 22:07:08 +0530 Subject: improved the UpdateAnime code --- user/anime/animelist.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'user/anime/animelist.go') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 4f5eac5..8a673c1 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -20,6 +20,7 @@ import ( "encoding/json" "strconv" "fmt" + "log" "errors" a "github.com/MikunoNaka/mal2go/anime" e "github.com/MikunoNaka/mal2go/errhandlers" @@ -37,17 +38,21 @@ func (c AnimeListClient)DeleteAnime(id int) string { } // Update/Add an anime to user's anime list -func (c AnimeListClient)UpdateAnime(id int, data UpdateAnimeData) string { +func (c AnimeListClient)UpdateAnime(id int, data UpdateAnimeData) serverResponse { + /* NOTE: UpdateAnime only adds anime to the list + * It doesn't add new anime to the list. + * This might be a problem with MAL, + * I haven't researched this much */ endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id) // turn data struct into json - pepe, err := json.Marshal(data) + jsonData, err := json.Marshal(data) if err != nil { - fmt.Println(err) + log.Println(err) } // finally make API request - res := c.putRequestHandler(endpoint, pepe) + res := c.putRequestHandler(endpoint, jsonData) return res } -- cgit v1.2.3 From 74a5f1ce1594d01ef7caeb3c5fcac047a7d8f9b3 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 14 Feb 2022 23:02:38 +0530 Subject: fixed UpdateAnime not working, though incomplete and kinda dangerous right now --- user/anime/animelist.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) (limited to 'user/anime/animelist.go') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 8a673c1..78242ac 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -20,7 +20,6 @@ import ( "encoding/json" "strconv" "fmt" - "log" "errors" a "github.com/MikunoNaka/mal2go/anime" e "github.com/MikunoNaka/mal2go/errhandlers" @@ -39,20 +38,10 @@ 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 { - /* NOTE: UpdateAnime only adds anime to the list - * It doesn't add new anime to the list. - * This might be a problem with MAL, - * I haven't researched this much */ endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id) - // turn data struct into json - jsonData, err := json.Marshal(data) - if err != nil { - log.Println(err) - } - - // finally make API request - res := c.putRequestHandler(endpoint, jsonData) + // make API request + res := c.putRequestHandler(endpoint, data) return res } -- cgit v1.2.3 From 831a57e8d065a01cefe40dd8545770064759eb13 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 15 Feb 2022 21:55:56 +0530 Subject: somehow made UpdateAnime work decently --- user/anime/animelist.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'user/anime/animelist.go') 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 -- cgit v1.2.3 From 5c94f2f8e7026b9a4dff874edbf3b4fa3b266516 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 16 Feb 2022 22:43:23 +0530 Subject: Defined different functions to update different anime fields --- user/anime/animelist.go | 34 +++------------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) (limited to 'user/anime/animelist.go') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 3065919..bbc7d7a 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -1,4 +1,4 @@ -/* mal2go - MyAnimeList V2 API wrapper for Go +/* MAL2Go - MyAnimeList V2 API wrapper for Go * Copyright (C) 2022 Vidhu Kant Sharma * This program is free software: you can redistribute it and/or modify @@ -29,43 +29,15 @@ const BASE_URL string = "https://api.myanimelist.net/v2" const maxListLimit int = 1000 // Delete an anime from user's anime list -func (c AnimeListClient)DeleteAnime(id int) string { +func (c Client)DeleteAnime(id int) string { endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id) /* Returns 200 if anime successfully deleted * Alternatively returns 404 if anime not in user's anime list */ return c.requestHandler(endpoint, "DELETE") } -// Update/Add an anime to user's anime list -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 - return c.putRequestHandler(endpoint, data), nil -} - // Get authenticated user's anime list -func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ +func (c Client) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ var userAnimeList a.AnimeList // error handling for limit and offset limitsErr := e.LimitsErrHandler(limit, offset, maxListLimit) -- cgit v1.2.3