Simultaneously update kitsu and MAL manga list item

This commit is contained in:
Timothy Warren 2017-03-29 13:29:03 -04:00
parent 08a882bbb6
commit 3bb4f32bdb
7 changed files with 131 additions and 41 deletions

View File

@ -10,7 +10,7 @@
<h2><?= $escape->html($name) ?></h2>
<section class="media-wrap">
<?php foreach($items as $item): ?>
<article class="media" id="manga-<?= $item['id'] ?>">
<article class="media" data-kitsu-id="<?= $item['id'] ?>" data-mal-id="<?= $item['mal_id'] ?>">
<?php if ($auth->isAuthenticated()): ?>
<div class="edit_buttons" hidden>
<button class="plus_one_chapter">+1 Chapter</button>

View File

@ -74,6 +74,7 @@
<td>&nbsp;</td>
<td>
<input type="hidden" value="<?= $item['id'] ?>" name="id" />
<input type="hidden" value="<?= $item['mal_id'] ?>" name="mal_id" />
<input type="hidden" value="<?= $item['manga']['slug'] ?>" name="manga_id" />
<input type="hidden" value="<?= $item['user_rating'] ?>" name="old_rating" />
<input type="hidden" value="true" name="edit" />
@ -92,6 +93,7 @@
<td>&nbsp;</td>
<td>
<input type="hidden" value="<?= $item['id'] ?>" name="id" />
<input type="hidden" value="<?= $item['mal_id'] ?>" name="mal_id" />
<button type="submit" class="danger">Delete Entry</button>
</td>
</tr>

View File

@ -18,6 +18,7 @@
<th>Completed Chapters</th>
<th># of Volumes</th>
<th>Type</th>
<th>Genres</th>
</tr>
</thead>
<tbody>
@ -40,6 +41,9 @@
<td><?= $item['chapters']['read'] ?> / <?= $item['chapters']['total'] ?></td>
<td><?= $item['volumes']['total'] ?></td>
<td><?= $item['manga']['type'] ?></td>
<td class="align_left">
<?= implode(', ', $item['manga']['genres']) ?>
</td>
</tr>
<?php endforeach ?>
</tbody>

View File

@ -530,7 +530,7 @@ class Model {
'media_type' => 'Manga',
'status' => $status,
],
'include' => 'media',
'include' => 'media,media.genres,media.mappings',
'page' => [
'offset' => $offset,
'limit' => $limit
@ -544,9 +544,16 @@ class Model {
if ( ! $cacheItem->isHit())
{
$data = $this->getRequest('library-entries', $options);
$data = JsonAPI::inlineRawIncludes($data, 'manga');
$transformed = $this->mangaListTransformer->transformCollection($data);
$included = JsonAPI::organizeIncludes($data['included']);
$included = JsonAPI::inlineIncludedRelationships($included, 'manga');
foreach($data['data'] as $i => &$item)
{
$item['included'] = $included;
}
$transformed = $this->mangaListTransformer->transformCollection($data['data']);
$cacheItem->set($transformed);
$cacheItem->save();

View File

@ -35,22 +35,42 @@ class MangaListTransformer extends AbstractTransformer {
*/
public function transform($item)
{
$manga =& $item['manga'];
$included = $item['included'];
$mangaId = $item['relationships']['media']['data']['id'];
$manga = $included['manga'][$mangaId];
$genres = array_column($manga['relationships']['genres'], 'name') ?? [];
sort($genres);
$rating = (is_numeric($item['attributes']['rating']))
? intval(2 * $item['attributes']['rating'])
: '-';
$totalChapters = ($manga['attributes']['chapterCount'] > 0)
? $manga['attributes']['chapterCount']
$totalChapters = ($manga['chapterCount'] > 0)
? $manga['chapterCount']
: '-';
$totalVolumes = ($manga['attributes']['volumeCount'] > 0)
? $manga['attributes']['volumeCount']
$totalVolumes = ($manga['volumeCount'] > 0)
? $manga['volumeCount']
: '-';
$MALid = NULL;
if (array_key_exists('mappings', $manga['relationships']))
{
foreach ($manga['relationships']['mappings'] as $mapping)
{
if ($mapping['externalSite'] === 'myanimelist/manga')
{
$MALid = $mapping['externalId'];
break;
}
}
}
$map = [
'id' => $item['id'],
'mal_id' => $MALid,
'chapters' => [
'read' => $item['attributes']['progress'],
'total' => $totalChapters
@ -60,13 +80,13 @@ class MangaListTransformer extends AbstractTransformer {
'total' => $totalVolumes
],
'manga' => [
'titles' => Kitsu::filterTitles($manga['attributes']),
'titles' => Kitsu::filterTitles($manga),
'alternate_title' => NULL,
'slug' => $manga['attributes']['slug'],
'url' => 'https://kitsu.io/manga/' . $manga['attributes']['slug'],
'type' => $manga['attributes']['mangaType'],
'image' => $manga['attributes']['posterImage']['small'],
'genres' => [], //$manga['genres'],
'slug' => $manga['slug'],
'url' => 'https://kitsu.io/manga/' . $manga['slug'],
'type' => $manga['mangaType'],
'image' => $manga['posterImage']['small'],
'genres' => $genres,
],
'reading_status' => $item['attributes']['status'],
'notes' => $item['attributes']['notes'],
@ -90,6 +110,7 @@ class MangaListTransformer extends AbstractTransformer {
$map = [
'id' => $item['id'],
'mal_id' => $item['mal_id'],
'data' => [
'status' => $item['status'],
'progress' => (int)$item['chapters_read'],

View File

@ -17,7 +17,11 @@
namespace Aviat\AnimeClient\API\MAL;
use Amp\Artax\Request;
use Aviat\AnimeClient\API\MAL\{ListItem, Transformer\AnimeListTransformer};
use Aviat\AnimeClient\API\MAL\{
ListItem,
Transformer\AnimeListTransformer,
Transformer\MangaListTransformer
};
use Aviat\AnimeClient\API\XML;
use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
use Aviat\Ion\Di\ContainerAware;
@ -33,7 +37,7 @@ class Model {
* @var AnimeListTransformer
*/
protected $animeListTransformer;
/**
* @var ListItem
*/
@ -47,6 +51,7 @@ class Model {
public function __construct(ListItem $listItem)
{
$this->animeListTransformer = new AnimeListTransformer();
$this->mangaListTransformer = new MangaListTransformer();
$this->listItem = $listItem;
}
@ -83,9 +88,7 @@ class Model {
];
}
return $this->listItem->create($createData);
return $this->listItem->create($createData, $type);
}
public function getMangaList(): array
@ -103,15 +106,23 @@ class Model {
return [];
}
public function updateListItem(array $data): Request
public function updateListItem(array $data, string $type = 'anime'): Request
{
$updateData = $this->animeListTransformer->untransform($data);
return $this->listItem->update($updateData['id'], $updateData['data']);
if ($type === 'anime')
{
$updateData = $this->animeListTransformer->untransform($data);
}
else if ($type === 'manga')
{
$updateData = $this->mangaListTransformer->untransform($data);
}
return $this->listItem->update($updateData['id'], $updateData['data'], $type);
}
public function deleteListItem(string $id): Request
public function deleteListItem(string $id, string $type = 'anime'): Request
{
return $this->listItem->delete($id);
return $this->listItem->delete($id, $type);
}
private function getList(string $type): array

View File

@ -22,6 +22,7 @@ use Aviat\AnimeClient\API\{
ParallelAPIRequest
};
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Json;
/**
* Model for handling requests dealing with the manga list
@ -83,17 +84,6 @@ class Manga extends API
return $this->kitsuModel->getManga($manga_id);
}
/**
* Create a new manga list item
*
* @param array $data
* @return bool
*/
public function createLibraryItem(array $data): bool
{
return $this->kitsuModel->createListItem($data);
}
/**
* Get information about a specific list item
* for editing/updating that item
@ -106,6 +96,35 @@ class Manga extends API
return $this->kitsuModel->getListItem($itemId);
}
/**
* Create a new manga list item
*
* @param array $data
* @return bool
*/
public function createLibraryItem(array $data): bool
{
$requester = new ParallelAPIRequest();
if ($this->useMALAPI)
{
$malData = $data;
$malId = $this->kitsuModel->getMalIdForManga($malData['id']);
if ( ! is_null($malId))
{
$malData['id'] = $malId;
$requester->addRequest($this->malModel->createListItem($malData, 'manga'), 'mal');
}
}
$requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu');
$results = $requester->makeRequests(TRUE);
return count($results[1]) > 0;
}
/**
* Update a list entry
*
@ -114,18 +133,44 @@ class Manga extends API
*/
public function updateLibraryItem(array $data): array
{
return $this->kitsuModel->updateListItem($data);
$requester = new ParallelAPIRequest();
if ($this->useMALAPI)
{
$requester->addRequest($this->malModel->updateListItem($data, 'manga'), 'mal');
}
$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu');
$results = $requester->makeRequests(TRUE);
return [
'body' => Json::decode($results[1]['kitsu']->getBody()),
'statusCode' => $results[1]['kitsu']->getStatus()
];
}
/**
* Remove a list entry
* Delete a list entry
*
* @param string $itemId
* @param string $id
* @param string|null $malId
* @return bool
*/
public function deleteLibraryItem(string $itemId): bool
public function deleteLibraryItem(string $id, string $malId = NULL): bool
{
return $this->kitsuModel->deleteListItem($itemId);
$requester = new ParallelAPIRequest();
if ($this->useMALAPI && ! is_null($malId))
{
$requester->addRequest($this->malModel->deleteListItem($malId, 'manga'), 'MAL');
}
$requester->addRequest($this->kitsuModel->deleteListItem($id), 'kitsu');
$results = $requester->makeRequests(TRUE);
return count($results[1]) > 0;
}
/**