More work on #39
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2022-09-22 12:08:21 -04:00
parent 2276bc4c75
commit c6ce8237ec
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
{
$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;

View File

@ -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,

View File

@ -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']);

View File

@ -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)
{

View File

@ -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<string, mixed>
*/
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<string, mixed>
*/
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 !== [];
}
}