Skip to content

Commit

Permalink
Merge pull request #136 from Kurozora/refactor-cast
Browse files Browse the repository at this point in the history
  • Loading branch information
kiritokatklian authored May 5, 2021
2 parents eb3e63e + 9f84040 commit 52ca148
Show file tree
Hide file tree
Showing 32 changed files with 404 additions and 677 deletions.
28 changes: 14 additions & 14 deletions app/Http/Controllers/AnimeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use App\Helpers\JSONResult;
use App\Http\Requests\RateAnimeRequest;
use App\Http\Requests\SearchAnimeRequest;
use App\Http\Resources\ActorCharacterAnimeResource;
use App\Http\Resources\AnimeCastResource;
use App\Http\Resources\ActorResource;
use App\Http\Resources\AnimeRelatedShowsResource;
use App\Http\Resources\AnimeResource;
Expand All @@ -23,7 +23,7 @@
class AnimeController extends Controller
{
/**
* Returns detailed information about an Anime.
* Returns detailed information of an Anime.
*
* @param Anime $anime
* @return JsonResponse
Expand All @@ -40,12 +40,12 @@ public function view(Anime $anime): JsonResponse
}

/**
* Returns actor information about an Anime.
* Returns actor information of an Anime.
*
* @param Anime $anime
* @return JsonResponse
*/
public function actorsAnime(Anime $anime): JsonResponse
public function actors(Anime $anime): JsonResponse
{
// Get the actors
$actors = $anime->getActors();
Expand All @@ -56,12 +56,12 @@ public function actorsAnime(Anime $anime): JsonResponse
}

/**
* Returns character information about an Anime.
* Returns character information of an Anime.
*
* @param Anime $anime
* @return JsonResponse
*/
public function charactersAnime(Anime $anime): JsonResponse
public function characters(Anime $anime): JsonResponse
{
// Get the actors
$actors = $anime->getCharacters();
Expand All @@ -72,28 +72,28 @@ public function charactersAnime(Anime $anime): JsonResponse
}

/**
* Returns actor-character-anime information about an Anime.
* Returns the cast information of an Anime.
*
* @param Anime $anime
* @return JsonResponse
*/
public function actorCharacterAnime(Anime $anime): JsonResponse
public function cast(Anime $anime): JsonResponse
{
// Get the actor-character-anime
$actorCharacterAnime = $anime->getActorCharacterAnime();
// Get the anime cast
$animeCast = $anime->getCast();

return JSONResult::success([
'data' => ActorCharacterAnimeResource::collection($actorCharacterAnime)
'data' => AnimeCastResource::collection($animeCast)
]);
}

/**
* Returns related-shows information about an Anime.
* Returns related-shows information of an Anime.
*
* @param Anime $anime
* @return JsonResponse
*/
public function relatedShowsAnime(Anime $anime): JsonResponse
public function relatedShows(Anime $anime): JsonResponse
{
// Get the related shows
$relations = $anime->getAnimeRelations();
Expand All @@ -109,7 +109,7 @@ public function relatedShowsAnime(Anime $anime): JsonResponse
* @param Anime $anime
* @return JsonResponse
*/
public function seasonsAnime(Anime $anime): JsonResponse
public function seasons(Anime $anime): JsonResponse
{
// Get the seasons
$seasons = $anime->getSeasons();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace App\Http\Resources;

use App\Models\ActorCharacterAnime;
use App\Models\AnimeCast;
use App\Enums\CastRole;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ActorCharacterAnimeResource extends JsonResource
class AnimeCastResource extends JsonResource
{
/**
* Transform the resource into an array.
Expand All @@ -17,15 +17,15 @@ class ActorCharacterAnimeResource extends JsonResource
*/
public function toArray($request): array
{
/** @var ActorCharacterAnime $actorCharacterAnime */
$actorCharacterAnime = $this->resource;
/** @var AnimeCast $animeCast */
$animeCast = $this->resource;

$resource = [
'id' => $actorCharacterAnime->id,
'id' => $animeCast->id,
'type' => 'cast',
'href' => route('api.anime.cast', $actorCharacterAnime, false),
'href' => route('api.anime.cast', $animeCast, false),
'attributes' => [
'role' => CastRole::getDescription($actorCharacterAnime->cast_role)
'role' => CastRole::getDescription($animeCast->role)
]
];

Expand All @@ -50,14 +50,12 @@ public function toArray($request): array
*/
protected function getActorsRelationship(): array
{
/** @var ActorCharacterAnime $actorCharacterAnime */
$actorCharacterAnime = $this->resource;

$actorCharacter = $actorCharacterAnime->actor_character;
/** @var AnimeCast $animeCast */
$animeCast = $this->resource;

return [
'actors' => [
'data' => ActorResource::collection([$actorCharacter->actor])
'data' => ActorResource::collection([$animeCast->actor])
]
];
}
Expand All @@ -69,14 +67,12 @@ protected function getActorsRelationship(): array
*/
protected function getCharactersRelationship(): array
{
/** @var ActorCharacterAnime $actorCharacterAnime */
$actorCharacterAnime = $this->resource;

$actorCharacter = $actorCharacterAnime->actor_character;
/** @var AnimeCast $animeCast */
$animeCast = $this->resource;

return [
'characters' => [
'data' => CharacterResourceBasic::collection([$actorCharacter->character])
'data' => CharacterResourceBasic::collection([$animeCast->character])
]
];
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/AnimeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function getCastRelationship(): array
return [
'cast' => [
'href' => route('api.anime.cast', $anime, false),
'data' => ActorCharacterAnimeResource::collection($anime->getActorCharacterAnime(Anime::MAXIMUM_RELATIONSHIPS_LIMIT))
'data' => AnimeCastResource::collection($anime->getCast(Anime::MAXIMUM_RELATIONSHIPS_LIMIT))
]
];
}
Expand Down
20 changes: 15 additions & 5 deletions app/Models/Actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Cache;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;

Expand Down Expand Up @@ -40,7 +40,7 @@ public function getFullNameAttribute(): string
*/
function anime(): HasManyDeep
{
return $this->hasManyDeep(Anime::class, [ActorCharacter::class, ActorCharacterAnime::class], ['actor_id', 'actor_character_id', 'id'], ['id', 'id', 'anime_id'])->distinct();
return $this->hasManyDeep(Anime::class, [AnimeCast::class], ['actor_id', 'id'], ['id', 'anime_id'])->distinct();
}

/**
Expand All @@ -63,11 +63,11 @@ public function getAnime(int $limit = null): Collection
/**
* Returns the characters the actor belongs to.
*
* @return BelongsToMany
* @return HasManyDeep
*/
function characters(): BelongsToMany
function characters(): HasManyDeep
{
return $this->belongsToMany(Character::class);
return $this->hasManyDeep(Character::class, [AnimeCast::class], ['actor_id', 'id'], ['id', 'character_id'])->distinct();
}

/**
Expand All @@ -86,4 +86,14 @@ public function getCharacters(int $limit = null): Collection
return $this->characters()->limit($limit)->get();
});
}

/**
* Returns the cast relationship the character has.
*
* @return HasMany
*/
public function cast(): HasMany
{
return $this->hasMany(AnimeCast::class);
}
}
54 changes: 0 additions & 54 deletions app/Models/ActorCharacter.php

This file was deleted.

49 changes: 11 additions & 38 deletions app/Models/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Anime extends KModel

// How long to cache certain responses
const CACHE_KEY_EXPLORE_SECONDS = 120 * 60;
const CACHE_KEY_ACTOR_CHARACTERS_SECONDS = 120 * 60;
const CACHE_KEY_ANIME_CAST_SECONDS = 120 * 60;
const CACHE_KEY_ACTORS_SECONDS = 120 * 60;
const CACHE_KEY_CHARACTERS_SECONDS = 120 * 60;
const CACHE_KEY_EPISODES_SECONDS = 120 * 60;
Expand Down Expand Up @@ -252,7 +252,7 @@ public function banner(): ?HasMany
*/
public function actors(): HasManyDeep
{
return $this->hasManyDeep(Actor::class, [ActorCharacterAnime::class, ActorCharacter::class], ['anime_id', 'id', 'id'], ['id', 'actor_character_id', 'actor_id'])->distinct();
return $this->hasManyDeep(Actor::class, [AnimeCast::class], ['anime_id', 'id'], ['id', 'actor_id'])->distinct();
}

/**
Expand All @@ -279,7 +279,7 @@ public function getActors(?int $limit = null): mixed
*/
public function characters(): HasManyDeep
{
return $this->hasManyDeep(Character::class, [ActorCharacterAnime::class, ActorCharacter::class], ['anime_id', 'id', 'id'], ['id', 'actor_character_id', 'character_id'])->distinct();
return $this->hasManyDeep(Character::class, [AnimeCast::class], ['anime_id', 'id'], ['id', 'character_id'])->distinct();
}

/**
Expand All @@ -300,56 +300,29 @@ public function getCharacters(?int $limit = null): mixed
}

/**
* Get the Anime's actor characters
*
* @return BelongsToMany
*/
public function actor_characters(): BelongsToMany
{
return $this->belongsToMany(ActorCharacter::class, ActorCharacterAnime::TABLE_NAME, 'anime_id', 'actor_character_id');
}

/**
* Retrieves the actor-characters for an Anime item in an array
*
* @param ?int $limit
* @return mixed
*/
public function getActorCharacters(?int $limit = null): mixed
{
// Find location of cached data
$cacheKey = self::cacheKey(['name' => 'anime.actor_characters', 'id' => $this->id, 'limit' => $limit]);

// Retrieve or save cached result
return Cache::remember($cacheKey, self::CACHE_KEY_ACTOR_CHARACTERS_SECONDS, function () use ($limit) {
return $this->actor_characters()->limit($limit)->get();
});
}

/**
* Get the Anime's actor-character-anime
* Get the Anime's cast
*
* @return HasMany
*/
public function actor_character_anime(): HasMany
public function cast(): HasMany
{
return $this->hasMany(ActorCharacterAnime::class);
return $this->hasMany(AnimeCast::class);
}

/**
* Retrieves the actor-characters-anime for an Anime item in an array
* Retrieves the cast for an Anime item in an array
*
* @param ?int $limit
* @return mixed
*/
public function getActorCharacterAnime(?int $limit = null): mixed
public function getCast(?int $limit = null): mixed
{
// Find location of cached data
$cacheKey = self::cacheKey(['name' => 'anime.actor_character_anime', 'id' => $this->id, 'limit' => $limit]);
$cacheKey = self::cacheKey(['name' => 'anime.cast', 'id' => $this->id, 'limit' => $limit]);

// Retrieve or save cached result
return Cache::remember($cacheKey, self::CACHE_KEY_ACTOR_CHARACTERS_SECONDS, function () use ($limit) {
return $this->actor_character_anime()->limit($limit)->get();
return Cache::remember($cacheKey, self::CACHE_KEY_ANIME_CAST_SECONDS, function () use ($limit) {
return $this->cast()->limit($limit)->get();
});
}

Expand Down
Loading

0 comments on commit 52ca148

Please sign in to comment.