More work on #39

This commit is contained in:
Timothy Warren 2022-09-22 12:08:21 -04:00
parent 4a2273c93c
commit f673a84cf6
5 changed files with 63 additions and 58 deletions

View File

@ -181,7 +181,7 @@ final class Model
*/ */
public function incrementListItem(FormItem $data, string $type): ?Request public function incrementListItem(FormItem $data, string $type): ?Request
{ {
$id = $this->getListIdFromMalId($data['mal_id'], $type); $id = $this->getListIdFromData($data, $type);
if ($id === NULL) if ($id === NULL)
{ {
return NULL; return NULL;
@ -197,8 +197,7 @@ final class Model
*/ */
public function updateListItem(FormItem $data, string $type): ?Request 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) if ($id === NULL)
{ {
return NULL; return NULL;
@ -213,32 +212,46 @@ final class Model
* @param string $malId - The id of the list item to remove * @param string $malId - The id of the list item to remove
* @param string $type - Them media type (anime/manga) * @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); $mediaId = $this->getMediaId((array)$data, $type);
if ($id === NULL) if ($mediaId === NULL)
{ {
return 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 return NULL;
{
$mediaId = $this->getMediaId((array)$data, $type);
return $this->listItem->delete($mediaId);
} }
/** /**
* Get the id of the specific list entry from the malId * Get the id of the specific list entry from the data
*
* @param string $type - The media type (anime/manga)
*/ */
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) if ($mediaId === NULL)
{ {
return NULL; return NULL;

View File

@ -58,6 +58,7 @@ final class LibraryEntryTransformer extends AbstractTransformer
: '-'; : '-';
$MALid = NULL; $MALid = NULL;
$AnilistId = NULL;
if (isset($anime['mappings']['nodes'])) if (isset($anime['mappings']['nodes']))
{ {
@ -66,7 +67,11 @@ final class LibraryEntryTransformer extends AbstractTransformer
if ($mapping['externalSite'] === 'MYANIMELIST_ANIME') if ($mapping['externalSite'] === 'MYANIMELIST_ANIME')
{ {
$MALid = $mapping['externalId']; $MALid = $mapping['externalId'];
break; }
if ($mapping['externalSite'] === 'ANILIST_ANIME')
{
$AnilistId = $mapping['externalId'];
} }
} }
} }
@ -80,6 +85,7 @@ final class LibraryEntryTransformer extends AbstractTransformer
return AnimeListItem::from([ return AnimeListItem::from([
'id' => $item['id'], 'id' => $item['id'],
'anilist_id' => $AnilistId,
'mal_id' => $MALid, 'mal_id' => $MALid,
'episodes' => [ 'episodes' => [
'watched' => (int) $item['progress'] !== 0 'watched' => (int) $item['progress'] !== 0
@ -135,6 +141,7 @@ final class LibraryEntryTransformer extends AbstractTransformer
: '-'; : '-';
$MALid = NULL; $MALid = NULL;
$AnilistId = NULL;
if (isset($manga['mappings']['nodes'])) if (isset($manga['mappings']['nodes']))
{ {
@ -143,7 +150,11 @@ final class LibraryEntryTransformer extends AbstractTransformer
if ($mapping['externalSite'] === 'MYANIMELIST_MANGA') if ($mapping['externalSite'] === 'MYANIMELIST_MANGA')
{ {
$MALid = $mapping['externalId']; $MALid = $mapping['externalId'];
break; }
if ($mapping['externalSite'] === 'ANILIST_MANGA')
{
$AnilistId = $mapping['externalId'];
} }
} }
} }
@ -153,6 +164,7 @@ final class LibraryEntryTransformer extends AbstractTransformer
return MangaListItem::from([ return MangaListItem::from([
'id' => $item['id'], 'id' => $item['id'],
'anilist_id' => $AnilistId,
'mal_id' => $MALid, 'mal_id' => $MALid,
'chapters' => [ 'chapters' => [
'read' => $readChapters, 'read' => $readChapters,

View File

@ -147,7 +147,7 @@ final class Anime extends BaseController
$this->redirect('anime/add', 303); $this->redirect('anime/add', 303);
} }
$result = $this->model->createLibraryItem($data); $result = $this->model->createItem($data);
if ($result) if ($result)
{ {
@ -169,7 +169,7 @@ final class Anime extends BaseController
{ {
$this->checkAuth(); $this->checkAuth();
$item = $this->model->getLibraryItem($id); $item = $this->model->getItem($id);
$this->setSessionRedirect(); $this->setSessionRedirect();
$this->outputHTML('anime/edit', [ $this->outputHTML('anime/edit', [
@ -210,7 +210,7 @@ final class Anime extends BaseController
// large form-based updates // large form-based updates
$transformer = new AnimeListTransformer(); $transformer = new AnimeListTransformer();
$postData = $transformer->untransform($data); $postData = $transformer->untransform($data);
$fullResult = $this->model->updateLibraryItem(FormItem::from($postData)); $fullResult = $this->model->updateItem(FormItem::from($postData));
if ($fullResult['statusCode'] === 200) if ($fullResult['statusCode'] === 200)
{ {
@ -250,7 +250,7 @@ final class Anime extends BaseController
exit(); exit();
} }
$response = $this->model->incrementLibraryItem(FormItem::from($data)); $response = $this->model->incrementItem(FormItem::from($data));
$this->cache->clear(); $this->cache->clear();
$this->outputJSON($response['body'], $response['statusCode']); $this->outputJSON($response['body'], $response['statusCode']);

View File

@ -142,7 +142,7 @@ final class Manga extends Controller
unset($data['mal_id']); unset($data['mal_id']);
} }
$result = $this->model->createLibraryItem($data); $result = $this->model->createItem($data);
if ($result) if ($result)
{ {
@ -170,7 +170,7 @@ final class Manga extends Controller
$this->checkAuth(); $this->checkAuth();
$this->setSessionRedirect(); $this->setSessionRedirect();
$item = $this->model->getLibraryItem($id); $item = $this->model->getItem($id);
$title = $this->formatTitle( $title = $this->formatTitle(
$this->config->get('whose_list') . "'s Manga List", $this->config->get('whose_list') . "'s Manga List",
'Edit' 'Edit'
@ -211,7 +211,7 @@ final class Manga extends Controller
// large form-based updates // large form-based updates
$transformer = new MangaListTransformer(); $transformer = new MangaListTransformer();
$post_data = $transformer->untransform($data); $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) if ($full_result['statusCode'] === 200)
{ {
@ -243,7 +243,7 @@ final class Manga extends Controller
$data = $this->request->getParsedBody(); $data = $this->request->getParsedBody();
} }
$res = $this->model->incrementLibraryItem(FormItem::from($data)); $res = $this->model->incrementItem(FormItem::from($data));
$body = $res['body']; $body = $res['body'];
$statusCode = $res['statusCode']; $statusCode = $res['statusCode'];
@ -261,7 +261,7 @@ final class Manga extends Controller
$this->checkAuth(); $this->checkAuth();
$body = (array) $this->request->getParsedBody(); $body = (array) $this->request->getParsedBody();
$response = $this->model->deleteLibraryItem($body['id'], $body['mal_id']); $response = $this->model->deleteItem(FormItem::from($body));
if ($response) if ($response)
{ {

View File

@ -74,7 +74,7 @@ trait MediaTrait
* Get information about a specific list item * Get information about a specific list item
* for editing/updating that 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); return $this->kitsuModel->getListItem($itemId);
} }
@ -84,7 +84,7 @@ trait MediaTrait
* *
* @throws Throwable * @throws Throwable
*/ */
public function createLibraryItem(array $data): bool public function createItem(array $data): bool
{ {
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
$kitsuRequest = $this->kitsuModel->createListItem($data); $kitsuRequest = $this->kitsuModel->createListItem($data);
@ -116,7 +116,7 @@ trait MediaTrait
* @throws Throwable * @throws Throwable
* @return array<string, mixed> * @return array<string, mixed>
*/ */
public function incrementLibraryItem(FormItem $data): array public function incrementItem(FormItem $data): array
{ {
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->incrementListItem($data), 'kitsu'); $requester->addRequest($this->kitsuModel->incrementListItem($data), 'kitsu');
@ -148,7 +148,7 @@ trait MediaTrait
* @throws Throwable * @throws Throwable
* @return array<string, mixed> * @return array<string, mixed>
*/ */
public function updateLibraryItem(FormItem $data): array public function updateItem(FormItem $data): array
{ {
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu'); $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 public function deleteItem(FormItem $data): bool
{ {
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
@ -193,29 +198,4 @@ trait MediaTrait
return $results !== []; 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 !== [];
}
} }