From 3e68fec704ac4c509c24d8e57f895951bbeddaab Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 8 Mar 2017 12:55:49 -0500 Subject: [PATCH] Add basic character pages --- app/appConf/routes.php | 17 +++++++++++++++ app/views/character.php | 12 +++++++++++ src/API/Kitsu/Model.php | 35 +++++++++++++++++++++++++++++- src/Controller.php | 7 ++++++ src/Controller/Character.php | 41 ++++++++++++++++++++++++++++++++++++ src/Util.php | 3 ++- 6 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 app/views/character.php create mode 100644 src/Controller/Character.php diff --git a/app/appConf/routes.php b/app/appConf/routes.php index 22f2e66e..936dd29e 100644 --- a/app/appConf/routes.php +++ b/app/appConf/routes.php @@ -149,6 +149,23 @@ return [ // Manga Collection Routes // --------------------------------------------------------------------- // --------------------------------------------------------------------- + // Other Routes + // --------------------------------------------------------------------- + 'character' => [ + 'path' => '/character/{slug}', + 'action' => 'index', + 'params' => [], + 'tokens' => [ + 'slug' => '[a-z0-9\-]+' + ] + ], + 'user_info' => [ + 'path' => '/me', + 'action' => 'me', + 'controller' => 'me', + 'verb' => 'get', + ], + // --------------------------------------------------------------------- // Default / Shared routes // --------------------------------------------------------------------- 'cache_purge' => [ diff --git a/app/views/character.php b/app/views/character.php new file mode 100644 index 00000000..96d96e02 --- /dev/null +++ b/app/views/character.php @@ -0,0 +1,12 @@ +
+
+
+ +
+
+

+ +

+
+
+
\ No newline at end of file diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index 429034db..4873cba9 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -93,7 +93,7 @@ class Model { * @param string $username * @return string */ - public function getUserIdByUsername(string $username = NULL) + public function getUserIdByUsername(string $username = NULL): string { if (is_null($username)) { @@ -119,6 +119,39 @@ class Model { return $cacheItem->get(); } + /** + * Get information about a character + * + * @param string $slug + * @return array + */ + public function getCharacter(string $slug): array + { + $data = $this->getRequest('/characters', [ + 'query' => [ + 'filter' => [ + 'slug' => $slug + ], + // 'include' => 'primaryMedia,castings' + ] + ]); + + return $data; + } + + public function getUserData(string $username): array + { + $userId = $this->getUserIdByUsername($username); + $data = $this->getRequest("/users/{$userId}", [ + 'query' => [ + 'include' => 'waifu,pinnedPost,blocks,linkedAccounts,profileLinks,mediaFollows,userRoles' + ] + ]); + // $data['included'] = JsonAPI::organizeIncludes($data['included']); + + return $data; + } + /** * Get the access token from the Kitsu API * diff --git a/src/Controller.php b/src/Controller.php index 963374d8..45ea16ce 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -123,6 +123,13 @@ class Controller { $this->baseData['message'] = $this->session->getFlash('message'); } + public function me() + { + $username = $this->config->get(['kitsu_username']); + $model = $this->container->get('kitsu-model'); + $this->outputJSON($model->getUserData($username)); + } + /** * Redirect to the default controller/url from an empty path * diff --git a/src/Controller/Character.php b/src/Controller/Character.php new file mode 100644 index 00000000..09c74796 --- /dev/null +++ b/src/Controller/Character.php @@ -0,0 +1,41 @@ + + * @copyright 2015 - 2017 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.0 + * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\Controller; + +use Aviat\AnimeClient\Controller as BaseController; + +class Character extends BaseController { + + public function index(string $slug) + { + $model = $this->container->get('kitsu-model'); + + $data = $model->getCharacter($slug); + + if ( ! array_key_exists('data', $data)) + { + return $this->notFound(); + } + + // $this->outputJSON($data); + $this->outputHTML('character', [ + 'title' => $this->config->get('whose_list') . + "'s Anime List · Characters · " . $data['data'][0]['attributes']['name'], + 'data' => $data['data'][0]['attributes'] + ]); + } +} \ No newline at end of file diff --git a/src/Util.php b/src/Util.php index 0fddc853..f7a559fe 100644 --- a/src/Util.php +++ b/src/Util.php @@ -38,7 +38,8 @@ class Util { 'update_form', 'login', 'logout', - 'details' + 'details', + 'character' ]; /**