From dddef5add6dd896b44f6d283581cc4d54e25a502 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 12 Oct 2020 14:06:49 -0400 Subject: [PATCH] Fix Manga List Incrementing, start of GraphQL conversion --- src/AnimeClient/API/Kitsu/MangaTrait.php | 6 +- src/AnimeClient/API/Kitsu/Model.php | 4 +- .../Mutations/IncrementLibraryItem.graphql | 2 +- .../Transformer/OldMangaListTransformer.php | 148 ++++++++++++++++++ src/AnimeClient/Controller/Manga.php | 8 +- .../Transformer/MangaListTransformerTest.php | 4 +- 6 files changed, 161 insertions(+), 11 deletions(-) create mode 100644 src/AnimeClient/API/Kitsu/Transformer/OldMangaListTransformer.php diff --git a/src/AnimeClient/API/Kitsu/MangaTrait.php b/src/AnimeClient/API/Kitsu/MangaTrait.php index ef2b15fa..45d50fac 100644 --- a/src/AnimeClient/API/Kitsu/MangaTrait.php +++ b/src/AnimeClient/API/Kitsu/MangaTrait.php @@ -21,7 +21,7 @@ use Aviat\AnimeClient\Kitsu as K; use Aviat\AnimeClient\API\Enum\MangaReadingStatus\Kitsu as KitsuReadingStatus; use Aviat\AnimeClient\API\JsonAPI; use Aviat\AnimeClient\API\Kitsu\Transformer\MangaHistoryTransformer; -use Aviat\AnimeClient\API\Kitsu\Transformer\MangaListTransformer; +use Aviat\AnimeClient\API\Kitsu\Transformer\OldMangaListTransformer; use Aviat\AnimeClient\API\Kitsu\Transformer\MangaTransformer; use Aviat\AnimeClient\API\Mapping\MangaReadingStatus; use Aviat\AnimeClient\API\ParallelAPIRequest; @@ -40,9 +40,9 @@ trait MangaTrait { protected MangaTransformer $mangaTransformer; /** - * @var MangaListTransformer + * @var OldMangaListTransformer */ - protected MangaListTransformer $mangaListTransformer; + protected OldMangaListTransformer $mangaListTransformer; // ------------------------------------------------------------------------- // ! Manga-specific methods diff --git a/src/AnimeClient/API/Kitsu/Model.php b/src/AnimeClient/API/Kitsu/Model.php index 4db7c866..25d4083d 100644 --- a/src/AnimeClient/API/Kitsu/Model.php +++ b/src/AnimeClient/API/Kitsu/Model.php @@ -32,7 +32,7 @@ use Aviat\AnimeClient\API\Kitsu\Transformer\{ OldAnimeListTransformer, LibraryEntryTransformer, MangaTransformer, - MangaListTransformer + OldMangaListTransformer }; use Aviat\Banker\Exception\InvalidArgumentException; @@ -68,7 +68,7 @@ final class Model { $this->animeTransformer = new AnimeTransformer(); $this->oldListTransformer = new OldAnimeListTransformer(); $this->mangaTransformer = new MangaTransformer(); - $this->mangaListTransformer = new MangaListTransformer(); + $this->mangaListTransformer = new OldMangaListTransformer(); $this->listItem = $listItem; } diff --git a/src/AnimeClient/API/Kitsu/Mutations/IncrementLibraryItem.graphql b/src/AnimeClient/API/Kitsu/Mutations/IncrementLibraryItem.graphql index 1ad752b3..190b9d2d 100644 --- a/src/AnimeClient/API/Kitsu/Mutations/IncrementLibraryItem.graphql +++ b/src/AnimeClient/API/Kitsu/Mutations/IncrementLibraryItem.graphql @@ -1,4 +1,4 @@ -mutation($id: ID!, $progress: Int) { +mutation($id: ID!, $progress: Int!) { libraryEntry{ update(input: { id: $id, progress: $progress }) { libraryEntry { diff --git a/src/AnimeClient/API/Kitsu/Transformer/OldMangaListTransformer.php b/src/AnimeClient/API/Kitsu/Transformer/OldMangaListTransformer.php new file mode 100644 index 00000000..4f645f23 --- /dev/null +++ b/src/AnimeClient/API/Kitsu/Transformer/OldMangaListTransformer.php @@ -0,0 +1,148 @@ + + * @copyright 2015 - 2020 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 5.1 + * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\API\Kitsu\Transformer; + +use Aviat\AnimeClient\Kitsu; +use Aviat\AnimeClient\Types\{ + FormItem, FormItemData, + MangaListItem, MangaListItemDetail +}; +use Aviat\Ion\Transformer\AbstractTransformer; +use Aviat\Ion\Type\StringType; + +/** + * Data transformation class for zippered Hummingbird manga + */ +final class OldMangaListTransformer extends AbstractTransformer { + /** + * Remap zipped anime data to a more logical form + * + * @param array $item manga entry item + * @return MangaListItem + */ + public function transform($item): MangaListItem + { + $included = $item['included']; + $mangaId = $item['relationships']['media']['data']['id']; + $manga = $included['manga'][$mangaId]; + + $genres = []; + + foreach ($manga['relationships']['categories'] as $genre) + { + $genres[] = $genre['title']; + } + + sort($genres); + + $rating = (int) $item['attributes']['ratingTwenty'] !== 0 + ? $item['attributes']['ratingTwenty'] / 2 + : '-'; + + $totalChapters = ((int) $manga['chapterCount'] !== 0) + ? $manga['chapterCount'] + : '-'; + + $totalVolumes = ((int) $manga['volumeCount'] !== 0) + ? $manga['volumeCount'] + : '-'; + + $readChapters = ((int) $item['attributes']['progress'] !== 0) + ? $item['attributes']['progress'] + : '-'; + + $MALid = NULL; + + if (array_key_exists('mappings', $manga['relationships'])) + { + foreach ($manga['relationships']['mappings'] as $mapping) + { + if ($mapping['externalSite'] === 'myanimelist/manga') + { + $MALid = $mapping['externalId']; + break; + } + } + } + + $titles = Kitsu::filterTitles($manga); + $title = array_shift($titles); + + return MangaListItem::from([ + 'id' => $item['id'], + 'mal_id' => $MALid, + 'chapters' => [ + 'read' => $readChapters, + 'total' => $totalChapters + ], + 'volumes' => [ + 'read' => '-', //$item['attributes']['volumes_read'], + 'total' => $totalVolumes + ], + 'manga' => MangaListItemDetail::from([ + 'genres' => $genres, + 'id' => $mangaId, + 'image' => $manga['posterImage']['small'], + 'slug' => $manga['slug'], + 'title' => $title, + 'titles' => $titles, + 'type' => (string)StringType::from($manga['subtype'])->upperCaseFirst(), + 'url' => 'https://kitsu.io/manga/' . $manga['slug'], + ]), + 'reading_status' => $item['attributes']['status'], + 'notes' => $item['attributes']['notes'], + 'rereading' => (bool)$item['attributes']['reconsuming'], + 'reread' => $item['attributes']['reconsumeCount'], + 'user_rating' => $rating, + ]); + } + + /** + * Untransform data to update the api + * + * @param array $item + * @return FormItem + */ + public function untransform($item): FormItem + { + $rereading = array_key_exists('rereading', $item) && (bool)$item['rereading']; + + $map = FormItem::from([ + 'id' => $item['id'], + 'mal_id' => $item['mal_id'], + 'data' => FormItemData::from([ + 'status' => $item['status'], + 'reconsuming' => $rereading, + 'reconsumeCount' => (int)$item['reread_count'], + 'notes' => $item['notes'], + ]), + ]); + + if (is_numeric($item['chapters_read']) && $item['chapters_read'] > 0) + { + $map['data']['progress'] = (int)$item['chapters_read']; + } + + if (is_numeric($item['new_rating']) && $item['new_rating'] > 0) + { + $map['data']['ratingTwenty'] = $item['new_rating'] * 2; + } + + return $map; + } +} +// End of MangaListTransformer.php \ No newline at end of file diff --git a/src/AnimeClient/Controller/Manga.php b/src/AnimeClient/Controller/Manga.php index 3775fc30..55cb03ab 100644 --- a/src/AnimeClient/Controller/Manga.php +++ b/src/AnimeClient/Controller/Manga.php @@ -18,7 +18,7 @@ namespace Aviat\AnimeClient\Controller; use Aura\Router\Exception\RouteNotFound; use Aviat\AnimeClient\Controller; -use Aviat\AnimeClient\API\Kitsu\Transformer\MangaListTransformer; +use Aviat\AnimeClient\API\Kitsu\Transformer\OldMangaListTransformer; use Aviat\AnimeClient\API\Mapping\MangaReadingStatus; use Aviat\AnimeClient\Model\Manga as MangaModel; use Aviat\AnimeClient\Types\FormItem; @@ -229,7 +229,7 @@ final class Manga extends Controller { // Do some minor data manipulation for // large form-based updates - $transformer = new MangaListTransformer(); + $transformer = new OldMangaListTransformer(); $post_data = $transformer->untransform($data); $full_result = $this->model->updateLibraryItem(FormItem::from($post_data)); @@ -264,7 +264,9 @@ final class Manga extends Controller { $data = $this->request->getParsedBody(); } - [$body, $statusCode] = $this->model->incrementLibraryItem(FormItem::from($data)); + $res = $this->model->incrementLibraryItem(FormItem::from($data)); + $body = $res['body']; + $statusCode = $res['statusCode']; $this->cache->clear(); $this->outputJSON($body, $statusCode); diff --git a/tests/AnimeClient/API/Kitsu/Transformer/MangaListTransformerTest.php b/tests/AnimeClient/API/Kitsu/Transformer/MangaListTransformerTest.php index 34561157..7a2fe86b 100644 --- a/tests/AnimeClient/API/Kitsu/Transformer/MangaListTransformerTest.php +++ b/tests/AnimeClient/API/Kitsu/Transformer/MangaListTransformerTest.php @@ -17,7 +17,7 @@ namespace Aviat\AnimeClient\Tests\API\Kitsu\Transformer; use Aviat\AnimeClient\API\JsonAPI; -use Aviat\AnimeClient\API\Kitsu\Transformer\MangaListTransformer; +use Aviat\AnimeClient\API\Kitsu\Transformer\OldMangaListTransformer; use Aviat\AnimeClient\Tests\AnimeClientTestCase; use Aviat\AnimeClient\Types\{ FormItem, @@ -52,7 +52,7 @@ class MangaListTransformerTest extends AnimeClientTestCase { $this->beforeTransform = $rawBefore['data']; // $this->afterTransform = Json::decodeFile("{$this->dir}/mangaListAfterTransform.json"); - $this->transformer = new MangaListTransformer(); + $this->transformer = new OldMangaListTransformer(); } public function testTransform()