From ed0113d3ff6e87d16d122d2d1f069ea551e07921 Mon Sep 17 00:00:00 2001 From: kiritokatklian Date: Sat, 15 May 2021 03:33:20 +0200 Subject: [PATCH] [Update] Anime & Studio - Added anime studio relationship to anime model - Added casting on anime studio attributes - Added AnimeStudioResource - Updated AnimeResource to make use of AnimeStudioResource - Updated anime staff factory --- app/Http/Resources/AnimeResource.php | 2 +- app/Http/Resources/AnimeStudioResource.php | 55 ++++++++++++++++++++++ app/Models/Anime.php | 27 +++++++++++ app/Models/AnimeStudio.php | 6 +++ config/app.php | 2 +- database/factories/AnimeStaffFactory.php | 2 +- 6 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 app/Http/Resources/AnimeStudioResource.php diff --git a/app/Http/Resources/AnimeResource.php b/app/Http/Resources/AnimeResource.php index f3dd78683..8f22298d6 100644 --- a/app/Http/Resources/AnimeResource.php +++ b/app/Http/Resources/AnimeResource.php @@ -159,7 +159,7 @@ protected function getStudiosRelationship(): array return [ 'studios' => [ 'href' => route('api.anime.studios', $anime, false), - 'data' => StudioResource::collection($anime->getStudios(Anime::MAXIMUM_RELATIONSHIPS_LIMIT)) + 'data' => AnimeStudioResource::collection($anime->getAnimeStudios(Anime::MAXIMUM_RELATIONSHIPS_LIMIT)) ] ]; } diff --git a/app/Http/Resources/AnimeStudioResource.php b/app/Http/Resources/AnimeStudioResource.php new file mode 100644 index 000000000..c43efb6ed --- /dev/null +++ b/app/Http/Resources/AnimeStudioResource.php @@ -0,0 +1,55 @@ +resource; + + $resource = [ + 'id' => $animeStudio->id, + 'type' => 'studio', + 'href' => route('api.anime.studios', $animeStudio->anime, false), + 'attributes' => $animeStudio->only(['is_licensor', 'is_producer', 'is_studio']), + ]; + + $relationships = []; + + $relationships = array_merge($relationships, $this->getStudioRelationship()); + + $resource = array_merge($resource, ['relationships' => $relationships]); + + return $resource; + } + + /** + * Returns the person relationship for the resource. + * + * @return array + */ + protected function getStudioRelationship(): array + { + /** @var AnimeStudio $animeStudio */ + $animeStudio = $this->resource; + + return [ + 'studio' => [ + 'href' => route('api.studios.details', $animeStudio, false), + 'data' => StudioResourceBasic::make($animeStudio->studio), + ] + ]; + } +} diff --git a/app/Models/Anime.php b/app/Models/Anime.php index 590907ab3..43ec09956 100644 --- a/app/Models/Anime.php +++ b/app/Models/Anime.php @@ -211,6 +211,33 @@ public function studios(): BelongsToMany return $this->belongsToMany(Studio::class); } + /** + * Retrieves the studios for an Anime item in an array + * + * @param ?int $limit + * @return mixed + */ + public function getAnimeStudios(?int $limit = null): mixed + { + // Find location of cached data + $cacheKey = self::cacheKey(['name' => 'anime.anime_studios', 'id' => $this->id, 'limit' => $limit]); + + // Retrieve or save cached result + return Cache::remember($cacheKey, self::CACHE_KEY_STUDIOS_SECONDS, function () use ($limit) { + return $this->anime_studios()->limit($limit)->get(); + }); + } + + /** + * Get the Anime's studios + * + * @return HasMany + */ + public function anime_studios(): HasMany + { + return $this->hasMany(AnimeStudio::class); + } + /** * Get the Anime's ratings * diff --git a/app/Models/AnimeStudio.php b/app/Models/AnimeStudio.php index b3a5fc3d8..378762de8 100644 --- a/app/Models/AnimeStudio.php +++ b/app/Models/AnimeStudio.php @@ -10,6 +10,12 @@ class AnimeStudio extends KModel const TABLE_NAME = 'anime_studio'; protected $table = self::TABLE_NAME; + protected $casts = [ + 'is_licensor' => 'boolean', + 'is_producer' => 'boolean', + 'is_studio' => 'boolean', + ]; + /** * Returns the anime belonging to the studio. * diff --git a/config/app.php b/config/app.php index f3445f056..f2b2baf99 100644 --- a/config/app.php +++ b/config/app.php @@ -38,7 +38,7 @@ | or any other location as required by the application or its packages. */ - 'version' => '1.2.0-alpha.63', + 'version' => '1.2.0-alpha.64', /* |-------------------------------------------------------------------------- diff --git a/database/factories/AnimeStaffFactory.php b/database/factories/AnimeStaffFactory.php index 56f5ea70c..52d2ab8a4 100644 --- a/database/factories/AnimeStaffFactory.php +++ b/database/factories/AnimeStaffFactory.php @@ -41,7 +41,7 @@ public function definition(): array return [ 'anime_id' => $anime, 'person_id' => $person, - 'staff_role_id' => $this->faker->randomElement([$staffRole, null]) + 'staff_role_id' => $staffRole, null, ]; } }