From c6ce8237ec9a39555b444ce6fa4168b31748cda0 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 22 Sep 2022 12:08:21 -0400 Subject: [PATCH] More work on #39 --- src/AnimeClient/API/Anilist/Model.php | 49 ++++++++++++------- .../Transformer/LibraryEntryTransformer.php | 16 +++++- src/AnimeClient/Controller/Anime.php | 8 +-- src/AnimeClient/Controller/Manga.php | 10 ++-- src/AnimeClient/Model/MediaTrait.php | 38 ++++---------- 5 files changed, 63 insertions(+), 58 deletions(-) diff --git a/src/AnimeClient/API/Anilist/Model.php b/src/AnimeClient/API/Anilist/Model.php index ee3ab4bb..82063023 100644 --- a/src/AnimeClient/API/Anilist/Model.php +++ b/src/AnimeClient/API/Anilist/Model.php @@ -181,7 +181,7 @@ final class Model */ public function incrementListItem(FormItem $data, string $type): ?Request { - $id = $this->getListIdFromMalId($data['mal_id'], $type); + $id = $this->getListIdFromData($data, $type); if ($id === NULL) { return NULL; @@ -197,8 +197,7 @@ final class Model */ public function updateListItem(FormItem $data, string $type): ?Request { - $id = $this->getListIdFromMalId($data['mal_id'], mb_strtoupper($type)); - + $id = $this->getListIdFromData($data, $type); if ($id === NULL) { return NULL; @@ -213,32 +212,46 @@ final class Model * @param string $malId - The id of the list item to remove * @param string $type - Them media type (anime/manga) */ - public function deleteListItem(string $malId, string $type): ?Request +// public function deleteListItem(string $malId, string $type): ?Request +// { +// $id = $this->getListIdFromMalId($malId, $type); +// if ($id === NULL) +// { +// return NULL; +// } +// +// return $this->listItem->delete($id); +// } + + /** + * Remove a list item + * + * @param FormItem $data - The entry to remove + * @param string $type - The media type (anime/manga) + */ + public function deleteItem(FormItem $data, string $type): ?Request { - $id = $this->getListIdFromMalId($malId, $type); - if ($id === NULL) + $mediaId = $this->getMediaId((array)$data, $type); + if ($mediaId === NULL) { return NULL; } - return $this->listItem->delete($id); - } + $id = $this->getListIdFromMediaId($mediaId); + if (is_string($id)) + { + return $this->listItem->delete($id); + } - public function deleteItem(FormItem $data, string $type): ?Request - { - $mediaId = $this->getMediaId((array)$data, $type); - - return $this->listItem->delete($mediaId); + return NULL; } /** - * Get the id of the specific list entry from the malId - * - * @param string $type - The media type (anime/manga) + * Get the id of the specific list entry from the data */ - public function getListIdFromMalId(string $malId, string $type): ?string + public function getListIdFromData(FormItem $data, string $type = 'ANIME'): ?string { - $mediaId = $this->getMediaIdFromMalId($malId, $type); + $mediaId = $this->getMediaId((array)$data, $type); if ($mediaId === NULL) { return NULL; diff --git a/src/AnimeClient/API/Kitsu/Transformer/LibraryEntryTransformer.php b/src/AnimeClient/API/Kitsu/Transformer/LibraryEntryTransformer.php index b1eff0b6..28b7181b 100644 --- a/src/AnimeClient/API/Kitsu/Transformer/LibraryEntryTransformer.php +++ b/src/AnimeClient/API/Kitsu/Transformer/LibraryEntryTransformer.php @@ -58,6 +58,7 @@ final class LibraryEntryTransformer extends AbstractTransformer : '-'; $MALid = NULL; + $AnilistId = NULL; if (isset($anime['mappings']['nodes'])) { @@ -66,7 +67,11 @@ final class LibraryEntryTransformer extends AbstractTransformer if ($mapping['externalSite'] === 'MYANIMELIST_ANIME') { $MALid = $mapping['externalId']; - break; + } + + if ($mapping['externalSite'] === 'ANILIST_ANIME') + { + $AnilistId = $mapping['externalId']; } } } @@ -80,6 +85,7 @@ final class LibraryEntryTransformer extends AbstractTransformer return AnimeListItem::from([ 'id' => $item['id'], + 'anilist_id' => $AnilistId, 'mal_id' => $MALid, 'episodes' => [ 'watched' => (int) $item['progress'] !== 0 @@ -135,6 +141,7 @@ final class LibraryEntryTransformer extends AbstractTransformer : '-'; $MALid = NULL; + $AnilistId = NULL; if (isset($manga['mappings']['nodes'])) { @@ -143,7 +150,11 @@ final class LibraryEntryTransformer extends AbstractTransformer if ($mapping['externalSite'] === 'MYANIMELIST_MANGA') { $MALid = $mapping['externalId']; - break; + } + + if ($mapping['externalSite'] === 'ANILIST_MANGA') + { + $AnilistId = $mapping['externalId']; } } } @@ -153,6 +164,7 @@ final class LibraryEntryTransformer extends AbstractTransformer return MangaListItem::from([ 'id' => $item['id'], + 'anilist_id' => $AnilistId, 'mal_id' => $MALid, 'chapters' => [ 'read' => $readChapters, diff --git a/src/AnimeClient/Controller/Anime.php b/src/AnimeClient/Controller/Anime.php index 243b8a37..352623c1 100644 --- a/src/AnimeClient/Controller/Anime.php +++ b/src/AnimeClient/Controller/Anime.php @@ -147,7 +147,7 @@ final class Anime extends BaseController $this->redirect('anime/add', 303); } - $result = $this->model->createLibraryItem($data); + $result = $this->model->createItem($data); if ($result) { @@ -169,7 +169,7 @@ final class Anime extends BaseController { $this->checkAuth(); - $item = $this->model->getLibraryItem($id); + $item = $this->model->getItem($id); $this->setSessionRedirect(); $this->outputHTML('anime/edit', [ @@ -210,7 +210,7 @@ final class Anime extends BaseController // large form-based updates $transformer = new AnimeListTransformer(); $postData = $transformer->untransform($data); - $fullResult = $this->model->updateLibraryItem(FormItem::from($postData)); + $fullResult = $this->model->updateItem(FormItem::from($postData)); if ($fullResult['statusCode'] === 200) { @@ -250,7 +250,7 @@ final class Anime extends BaseController exit(); } - $response = $this->model->incrementLibraryItem(FormItem::from($data)); + $response = $this->model->incrementItem(FormItem::from($data)); $this->cache->clear(); $this->outputJSON($response['body'], $response['statusCode']); diff --git a/src/AnimeClient/Controller/Manga.php b/src/AnimeClient/Controller/Manga.php index 9467dbbf..a0d091f4 100644 --- a/src/AnimeClient/Controller/Manga.php +++ b/src/AnimeClient/Controller/Manga.php @@ -142,7 +142,7 @@ final class Manga extends Controller unset($data['mal_id']); } - $result = $this->model->createLibraryItem($data); + $result = $this->model->createItem($data); if ($result) { @@ -170,7 +170,7 @@ final class Manga extends Controller $this->checkAuth(); $this->setSessionRedirect(); - $item = $this->model->getLibraryItem($id); + $item = $this->model->getItem($id); $title = $this->formatTitle( $this->config->get('whose_list') . "'s Manga List", 'Edit' @@ -211,7 +211,7 @@ final class Manga extends Controller // large form-based updates $transformer = new MangaListTransformer(); $post_data = $transformer->untransform($data); - $full_result = $this->model->updateLibraryItem(FormItem::from($post_data)); + $full_result = $this->model->updateItem(FormItem::from($post_data)); if ($full_result['statusCode'] === 200) { @@ -243,7 +243,7 @@ final class Manga extends Controller $data = $this->request->getParsedBody(); } - $res = $this->model->incrementLibraryItem(FormItem::from($data)); + $res = $this->model->incrementItem(FormItem::from($data)); $body = $res['body']; $statusCode = $res['statusCode']; @@ -261,7 +261,7 @@ final class Manga extends Controller $this->checkAuth(); $body = (array) $this->request->getParsedBody(); - $response = $this->model->deleteLibraryItem($body['id'], $body['mal_id']); + $response = $this->model->deleteItem(FormItem::from($body)); if ($response) { diff --git a/src/AnimeClient/Model/MediaTrait.php b/src/AnimeClient/Model/MediaTrait.php index af1c1058..f525eab3 100644 --- a/src/AnimeClient/Model/MediaTrait.php +++ b/src/AnimeClient/Model/MediaTrait.php @@ -74,7 +74,7 @@ trait MediaTrait * Get information about a specific list item * for editing/updating that item */ - public function getLibraryItem(string $itemId): AnimeListItem|MangaListItem + public function getItem(string $itemId): AnimeListItem|MangaListItem { return $this->kitsuModel->getListItem($itemId); } @@ -84,7 +84,7 @@ trait MediaTrait * * @throws Throwable */ - public function createLibraryItem(array $data): bool + public function createItem(array $data): bool { $requester = new ParallelAPIRequest(); $kitsuRequest = $this->kitsuModel->createListItem($data); @@ -116,7 +116,7 @@ trait MediaTrait * @throws Throwable * @return array */ - public function incrementLibraryItem(FormItem $data): array + public function incrementItem(FormItem $data): array { $requester = new ParallelAPIRequest(); $requester->addRequest($this->kitsuModel->incrementListItem($data), 'kitsu'); @@ -148,7 +148,7 @@ trait MediaTrait * @throws Throwable * @return array */ - public function updateLibraryItem(FormItem $data): array + public function updateItem(FormItem $data): array { $requester = new ParallelAPIRequest(); $requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu'); @@ -174,6 +174,11 @@ trait MediaTrait ]; } + /** + * Delete a list entry + * + * @throws Throwable + */ public function deleteItem(FormItem $data): bool { $requester = new ParallelAPIRequest(); @@ -193,29 +198,4 @@ trait MediaTrait return $results !== []; } - - /** - * Delete a list entry - * - * @throws Throwable - */ - public function deleteLibraryItem(string $id, ?string $malId = NULL): bool - { - $requester = new ParallelAPIRequest(); - $requester->addRequest($this->kitsuModel->deleteListItem($id), 'kitsu'); - - if ($this->anilistEnabled && $malId !== NULL) - { - // If can't map MAL id, this will be null - $maybeRequest = $this->anilistModel->deleteListItem($malId, strtoupper($this->type)); - if ($maybeRequest !== NULL) - { - $requester->addRequest($maybeRequest, 'anilist'); - } - } - - $results = $requester->makeRequests(); - - return $results !== []; - } }