Fix Manga List Incrementing, start of GraphQL conversion

This commit is contained in:
Timothy Warren 2020-10-12 14:06:49 -04:00
parent ecb913322f
commit 94d227b08e
6 changed files with 161 additions and 11 deletions

View File

@ -21,7 +21,7 @@ use Aviat\AnimeClient\Kitsu as K;
use Aviat\AnimeClient\API\Enum\MangaReadingStatus\Kitsu as KitsuReadingStatus; use Aviat\AnimeClient\API\Enum\MangaReadingStatus\Kitsu as KitsuReadingStatus;
use Aviat\AnimeClient\API\JsonAPI; use Aviat\AnimeClient\API\JsonAPI;
use Aviat\AnimeClient\API\Kitsu\Transformer\MangaHistoryTransformer; 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\Kitsu\Transformer\MangaTransformer;
use Aviat\AnimeClient\API\Mapping\MangaReadingStatus; use Aviat\AnimeClient\API\Mapping\MangaReadingStatus;
use Aviat\AnimeClient\API\ParallelAPIRequest; use Aviat\AnimeClient\API\ParallelAPIRequest;
@ -40,9 +40,9 @@ trait MangaTrait {
protected MangaTransformer $mangaTransformer; protected MangaTransformer $mangaTransformer;
/** /**
* @var MangaListTransformer * @var OldMangaListTransformer
*/ */
protected MangaListTransformer $mangaListTransformer; protected OldMangaListTransformer $mangaListTransformer;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// ! Manga-specific methods // ! Manga-specific methods

View File

@ -32,7 +32,7 @@ use Aviat\AnimeClient\API\Kitsu\Transformer\{
OldAnimeListTransformer, OldAnimeListTransformer,
LibraryEntryTransformer, LibraryEntryTransformer,
MangaTransformer, MangaTransformer,
MangaListTransformer OldMangaListTransformer
}; };
use Aviat\Banker\Exception\InvalidArgumentException; use Aviat\Banker\Exception\InvalidArgumentException;
@ -68,7 +68,7 @@ final class Model {
$this->animeTransformer = new AnimeTransformer(); $this->animeTransformer = new AnimeTransformer();
$this->oldListTransformer = new OldAnimeListTransformer(); $this->oldListTransformer = new OldAnimeListTransformer();
$this->mangaTransformer = new MangaTransformer(); $this->mangaTransformer = new MangaTransformer();
$this->mangaListTransformer = new MangaListTransformer(); $this->mangaListTransformer = new OldMangaListTransformer();
$this->listItem = $listItem; $this->listItem = $listItem;
} }

View File

@ -1,4 +1,4 @@
mutation($id: ID!, $progress: Int) { mutation($id: ID!, $progress: Int!) {
libraryEntry{ libraryEntry{
update(input: { id: $id, progress: $progress }) { update(input: { id: $id, progress: $progress }) {
libraryEntry { libraryEntry {

View File

@ -0,0 +1,148 @@
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
* PHP version 7.4
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @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

View File

@ -18,7 +18,7 @@ namespace Aviat\AnimeClient\Controller;
use Aura\Router\Exception\RouteNotFound; use Aura\Router\Exception\RouteNotFound;
use Aviat\AnimeClient\Controller; 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\API\Mapping\MangaReadingStatus;
use Aviat\AnimeClient\Model\Manga as MangaModel; use Aviat\AnimeClient\Model\Manga as MangaModel;
use Aviat\AnimeClient\Types\FormItem; use Aviat\AnimeClient\Types\FormItem;
@ -229,7 +229,7 @@ final class Manga extends Controller {
// Do some minor data manipulation for // Do some minor data manipulation for
// large form-based updates // large form-based updates
$transformer = new MangaListTransformer(); $transformer = new OldMangaListTransformer();
$post_data = $transformer->untransform($data); $post_data = $transformer->untransform($data);
$full_result = $this->model->updateLibraryItem(FormItem::from($post_data)); $full_result = $this->model->updateLibraryItem(FormItem::from($post_data));
@ -264,7 +264,9 @@ final class Manga extends Controller {
$data = $this->request->getParsedBody(); $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->cache->clear();
$this->outputJSON($body, $statusCode); $this->outputJSON($body, $statusCode);

View File

@ -17,7 +17,7 @@
namespace Aviat\AnimeClient\Tests\API\Kitsu\Transformer; namespace Aviat\AnimeClient\Tests\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\JsonAPI; 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\Tests\AnimeClientTestCase;
use Aviat\AnimeClient\Types\{ use Aviat\AnimeClient\Types\{
FormItem, FormItem,
@ -52,7 +52,7 @@ class MangaListTransformerTest extends AnimeClientTestCase {
$this->beforeTransform = $rawBefore['data']; $this->beforeTransform = $rawBefore['data'];
// $this->afterTransform = Json::decodeFile("{$this->dir}/mangaListAfterTransform.json"); // $this->afterTransform = Json::decodeFile("{$this->dir}/mangaListAfterTransform.json");
$this->transformer = new MangaListTransformer(); $this->transformer = new OldMangaListTransformer();
} }
public function testTransform() public function testTransform()