From 39118d63e51e0010f5270560966f55236a792bcf Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 10 Jan 2017 12:35:46 -0500 Subject: [PATCH] All basic API functionality: * Anime List Item: * Creation * Updating * Retreiving * Deletion * Manga List Item: * Creation * Updating * Retreiving * Deletion * Anime detail page * Manga detail page --- app/views/anime/add.php | 1 + app/views/main-menu.php | 6 +++--- app/views/manga/add.php | 1 + public/js/base/events.js | 2 +- public/js/manga_edit.js | 2 +- src/API/Kitsu/KitsuModel.php | 16 +++++++++++++--- src/API/Kitsu/ListItem.php | 37 +++++++++++++++++++++--------------- src/Controller/Anime.php | 4 ++-- src/Controller/Manga.php | 14 ++++++++------ src/Model/Anime.php | 5 +++++ src/Model/Manga.php | 27 ++++++++++++++++++++++++++ 11 files changed, 84 insertions(+), 31 deletions(-) diff --git a/app/views/anime/add.php b/app/views/anime/add.php index 9224905d..fac82700 100644 --- a/app/views/anime/add.php +++ b/app/views/anime/add.php @@ -28,6 +28,7 @@   + diff --git a/app/views/main-menu.php b/app/views/main-menu.php index be89435e..6f3b99f9 100644 --- a/app/views/main-menu.php +++ b/app/views/main-menu.php @@ -17,13 +17,13 @@ [Manga List] - is_authenticated()): ?> + is_authenticated()): ?>     - + is_authenticated()): ?> Logout @@ -41,4 +41,4 @@
  • ">List View
  • - \ No newline at end of file + diff --git a/app/views/manga/add.php b/app/views/manga/add.php index 33609c55..d0a3efd3 100644 --- a/app/views/manga/add.php +++ b/app/views/manga/add.php @@ -28,6 +28,7 @@   + diff --git a/public/js/base/events.js b/public/js/base/events.js index 8178d7bd..9ec6e1ab 100644 --- a/public/js/base/events.js +++ b/public/js/base/events.js @@ -21,7 +21,7 @@ }); // Clear the api cache - ac.on('.js-clear-cache', 'click', function (event) { + ac.on('.js-clear-cache', 'click', function () { ac.get('/cache_purge', () => { ac.showMessage('success', `Sucessfully purged api cache`); }); diff --git a/public/js/manga_edit.js b/public/js/manga_edit.js index 7e13973d..afe712ce 100644 --- a/public/js/manga_edit.js +++ b/public/js/manga_edit.js @@ -38,7 +38,7 @@ } // Update the total count - data['data']['progress'] = ++completed; + data.data.progress = ++completed; _.ajax(_.url('/manga/update'), { data: data, diff --git a/src/API/Kitsu/KitsuModel.php b/src/API/Kitsu/KitsuModel.php index ef5890f1..ea624b42 100644 --- a/src/API/Kitsu/KitsuModel.php +++ b/src/API/Kitsu/KitsuModel.php @@ -16,7 +16,6 @@ namespace Aviat\AnimeClient\API\Kitsu; -use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\API\Kitsu as K; use Aviat\AnimeClient\API\Kitsu\Transformer\{ AnimeTransformer, AnimeListTransformer, MangaTransformer, MangaListTransformer @@ -24,7 +23,6 @@ use Aviat\AnimeClient\API\Kitsu\Transformer\{ use Aviat\Ion\Di\ContainerAware; use Aviat\Ion\Json; use GuzzleHttp\Exception\ClientException; -use PHP_CodeSniffer\Tokenizers\JS; /** * Kitsu API Model @@ -77,6 +75,12 @@ class KitsuModel { $this->mangaListTransformer = new MangaListTransformer(); } + /** + * Get the userid for a username from Kitsu + * + * @param string $username + * @return string + */ public function getUserIdByUsername(string $username) { $data = $this->getRequest('users', [ @@ -154,7 +158,7 @@ class KitsuModel { 'include' => 'media,media.genres', 'page' => [ 'offset' => 0, - 'limit' => 200 + 'limit' => 1000 ], 'sort' => '-updated_at' ] @@ -251,6 +255,12 @@ class KitsuModel { } } + public function createListItem(array $data): bool + { + $data['user_id'] = $this->getUserIdByUsername($this->getUsername()); + return $this->listItem->create($data); + } + public function updateListItem(array $data) { try diff --git a/src/API/Kitsu/ListItem.php b/src/API/Kitsu/ListItem.php index 67c8c566..757df26f 100644 --- a/src/API/Kitsu/ListItem.php +++ b/src/API/Kitsu/ListItem.php @@ -37,24 +37,31 @@ class ListItem extends AbstractListItem { public function create(array $data): bool { - $response = $this->getResponse('post', 'library-entries', [ - 'body' => [ - 'type' => 'libraryEntries', - 'attributes' => [ - 'status' => $data['status'], - 'progress' => $data['progress'] ?? 0 - ], - 'relationships' => [ - 'user' => [ - 'id' => $data['user_id'], - 'type' => 'users' +/*?>
    getResponse('POST', 'library-entries', [ + 'body' => Json::encode([ + 'data' => [ + 'type' => 'libraryEntries', + 'attributes' => [ + 'status' => $data['status'], + 'progress' => $data['progress'] ?? 0 ], - 'media' => [ - 'id' => $data['id'], - 'type' => $data['type'] + 'relationships' => [ + 'user' => [ + 'data' => [ + 'id' => $data['user_id'], + 'type' => 'users' + ] + ], + 'media' => [ + 'data' => [ + 'id' => $data['id'], + 'type' => $data['type'] + ] + ] ] ] - ] + ]) ]); return ($response->getStatusCode() === 201); diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index 4809e21e..a9746973 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -154,9 +154,9 @@ class Anime extends BaseController { $this->redirect("anime/add", 303); } - $result = $this->model->update($data); + $result = $this->model->createLibraryItem($data); - if (intval($result['statusCode']) === 201) + if ($result) { $this->set_flash_message('Added new anime to list', 'success'); // $this->cache->purge(); diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php index a0113207..d2b47ee4 100644 --- a/src/Controller/Manga.php +++ b/src/Controller/Manga.php @@ -137,12 +137,12 @@ class Manga extends Controller { $this->redirect("manga/add", 303); } - $result = $this->model->add($data); + $result = $this->model->createLibraryItem($data); - if ($result['statusCode'] >= 200 && $result['statusCode'] < 300) + if ($result) { $this->set_flash_message('Added new manga to list', 'success'); - $this->cache->purge(); + // $this->cache->purge(); } else { @@ -243,12 +243,14 @@ class Manga extends Controller { */ public function delete() { - $response = $this->model->delete($this->request->getParsedBody()); + $body = $this->request->getParsedBody(); + $id = $body['id']; + $response = $this->model->deleteLibraryItem($id); - if ((bool)$response['body'] === TRUE) + if ($response) { $this->set_flash_message("Successfully deleted manga.", 'success'); - $this->cache->purge(); + //$this->cache->purge(); } else { diff --git a/src/Model/Anime.php b/src/Model/Anime.php index 553d18e8..6b5453d4 100644 --- a/src/Model/Anime.php +++ b/src/Model/Anime.php @@ -105,6 +105,11 @@ class Anime extends API { return $this->kitsuModel->getListItem($itemId); } + public function createLibraryItem(array $data): bool + { + return $this->kitsuModel->createListItem($data); + } + /** * Update a list entry * diff --git a/src/Model/Manga.php b/src/Model/Manga.php index 7df7a4c3..420948a2 100644 --- a/src/Model/Manga.php +++ b/src/Model/Manga.php @@ -52,6 +52,11 @@ class Manga extends API 'dropped' => self::DROPPED ]; + /** + * @var Aviat\AnimeClient\API\Kitsu\KitsuModel + */ + protected $kitsuModel; + public function __construct(ContainerInterface $container) { parent::__construct($container); @@ -83,6 +88,17 @@ 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 +122,17 @@ class Manga extends API return $this->kitsuModel->updateListItem($data); } + /** + * Remove a list entry + * + * @param string $itemId + * @return bool + */ + public function deleteLibraryItem(string $itemId): bool + { + return $this->kitsuModel->deleteListItem($itemId); + } + /** * Search for anime by name *