Skip to content

Commit

Permalink
Merge pull request #147
Browse files Browse the repository at this point in the history
[Improvement] Anime Studio
  • Loading branch information
kiritokatklian authored May 15, 2021
2 parents 14c6649 + ca7cff7 commit 292c9f0
Show file tree
Hide file tree
Showing 44 changed files with 728 additions and 457 deletions.
2 changes: 1 addition & 1 deletion app/Http/Resources/AnimeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
]
];
}
Expand Down
55 changes: 55 additions & 0 deletions app/Http/Resources/AnimeStudioResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Resources;

use App\Models\AnimeStudio;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class AnimeStudioResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
*/
public function toArray($request): array
{
/** @var AnimeStudio $animeStudio */
$animeStudio = $this->resource;

$resource = [
'id' => $animeStudio->id,
'type' => 'studios',
'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),
]
];
}
}
3 changes: 2 additions & 1 deletion app/Http/Resources/StudioResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ protected function getAnimeRelationship(?Anime $excludingAnime = null): array
$studio = $this->resource;

$whereRules = [];
if ($excludingAnime)
if ($excludingAnime) {
array_push($whereRules, ['animes.id', '!=', $excludingAnime->id]);
}

return [
'shows' => [
Expand Down
27 changes: 27 additions & 0 deletions app/Models/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
9 changes: 7 additions & 2 deletions app/Models/AnimeStudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AnimeStudio extends Model
class AnimeStudio extends KModel
{
// Table name
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.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Studio.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function anime(): BelongsToMany
public function getAnime(array $where = []): Collection
{
// Find location of cached data
$cacheKey = self::cacheKey(['name' => 'studios', 'id' => $this->id, 'where' => $where]);
$cacheKey = self::cacheKey(['name' => 'studios.anime', 'id' => $this->id, 'where' => $where]);

// Retrieve or save cached result
$animeInfo = Cache::remember($cacheKey, self::CACHE_KEY_ANIME_SECONDS, function () use ($where) {
Expand Down
8 changes: 4 additions & 4 deletions app/Nova/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Anime extends Resource
*
* @var string
*/
public static string $model = 'App\Models\Anime';
public static string $model = \App\Models\Anime::class;

/**
* The single value that should be used to represent the resource when being displayed.
Expand Down Expand Up @@ -204,8 +204,7 @@ public function fields(Request $request): array

HasMany::make('Staff', 'staff', AnimeStaff::class),

BelongsToMany::make('Studios')
->searchable(),
HasMany::make('Studios', 'anime_studios', AnimeStudio::class),

BelongsToMany::make('Moderators', 'moderators', User::class)
// @TODO
Expand All @@ -229,8 +228,9 @@ public function title(): string
{
$animeName = $this->title;

if (!is_string($animeName) || !strlen($animeName))
if (!is_string($animeName) || !strlen($animeName)) {
$animeName = 'No Anime title';
}

return $animeName . ' (ID: ' . $this->id . ')';
}
Expand Down
2 changes: 1 addition & 1 deletion app/Nova/AnimeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AnimeImage extends Resource
*
* @var string
*/
public static string $model = 'App\Models\AnimeImages';
public static string $model = \App\Models\AnimeImages::class;

/**
* The single value that should be used to represent the resource when being displayed.
Expand Down
2 changes: 1 addition & 1 deletion app/Nova/AnimeSong.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AnimeSong extends Resource
* @var array
*/
public static $search = [
'id',
'id', 'type', 'position', 'episodes'
];

/**
Expand Down
24 changes: 18 additions & 6 deletions app/Nova/AnimeStaff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Nova;

use Illuminate\Database\Query\Builder;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -79,17 +80,28 @@ public function fields(Request $request): array
*/
protected static function afterValidation(NovaRequest $request, $validator)
{
$resourceID = $request->resourceId;
$anime = $request->post('anime');
$person = $request->post('person');

$unique = Rule::unique(\App\Models\AnimeStaff::TABLE_NAME, 'person_id')->where(function ($query) use($anime, $person) {
return $query->where('anime_id', $anime)->where('person_id', $person);
$staffRole = $request->post('staff_role');

$unique = Rule::unique(\App\Models\AnimeStaff::TABLE_NAME, 'staff_role_id')->where(function ($query) use($resourceID, $anime, $person, $staffRole) {
if ($resourceID) {
$query->whereNotIn('id', [$resourceID]);
}

return $query
->where([
['anime_id', $anime],
['person_id', $person],
['staff_role_id', $staffRole]
]);
});

$uniqueValidator = Validator::make($request->only('person'), [
'person' => [$unique],
$uniqueValidator = Validator::make($request->only('staff_role'), [
'staff_role' => [$unique],
], [
'person' => __('validation.unique')
'staff_role' => __('validation.unique')
]);

$uniqueValidator->validate();
Expand Down
56 changes: 55 additions & 1 deletion app/Nova/AnimeStudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
namespace App\Nova;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;
use Validator;

class AnimeStudio extends Resource
{
Expand All @@ -13,7 +18,7 @@ class AnimeStudio extends Resource
*
* @var string
*/
public static string $model = 'App\Models\AnimeStudio';
public static string $model = \App\Models\AnimeStudio::class;

/**
* The single value that should be used to represent the resource when being displayed.
Expand Down Expand Up @@ -57,9 +62,58 @@ public function fields(Request $request): array
BelongsTo::make('Studio')
->sortable()
->searchable(),

Boolean::make('Is Licensor')
->sortable()
->help('The studio is responsible for licensing the anime.')
->required(),

Boolean::make('Is Producer')
->sortable()
->help('The studio is responsible for producing the anime. Usually sponsors.')
->required(),

Boolean::make('Is Studio')
->sortable()
->help('The studio responsible for creating (drawing) the anime.')
->required(),
];
}

/**
* Handle any post-validation processing.
*
* @param NovaRequest $request
* @param \Illuminate\Validation\Validator $validator
* @return void
* @throws ValidationException
*/
protected static function afterValidation(NovaRequest $request, $validator)
{
$resourceID = $request->resourceId;
$anime = $request->post('anime');
$studio = $request->post('studio');

$unique = Rule::unique(\App\Models\AnimeStudio::TABLE_NAME, 'studio_id')->where(function ($query) use($resourceID, $anime, $studio) {
if ($resourceID) {
$query->whereNotIn('id', [$resourceID]);
}

return $query->where([
['anime_id', $anime],
['studio_id', $studio]
]);
});

$uniqueValidator = Validator::make($request->only('studio'), [
'studio' => [$unique],
], [
'studio' => __('validation.unique')
]);

$uniqueValidator->validate();
}

/**
* Get the cards available for the request.
*
Expand Down
37 changes: 18 additions & 19 deletions app/Nova/AppTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AppTheme extends Resource
*
* @var string
*/
public static string $model = 'App\Models\AppTheme';
public static string $model = \App\Models\AppTheme::class;

/**
* The single value that should be used to represent the resource when being displayed.
Expand All @@ -25,13 +25,6 @@ class AppTheme extends Resource
*/
public static $title = 'id';

/**
* The logical group associated with the resource.
*
* @var string
*/
public static $group = 'Cosmetics';

/**
* The columns that should be searched.
*
Expand All @@ -41,6 +34,13 @@ class AppTheme extends Resource
'id', 'name'
];

/**
* The logical group associated with the resource.
*
* @var string
*/
public static $group = 'Cosmetics';

/**
* Get the fields displayed by the resource.
*
Expand Down Expand Up @@ -178,6 +178,16 @@ public function fields(Request $request): array
];
}

/**
* Get the value that should be displayed to represent the resource.
*
* @return string
*/
public function title(): string
{
return 'Name: "' . $this->name . '" (ID: ' . $this->id . ')';
}

/**
* Get the cards available for the request.
*
Expand Down Expand Up @@ -221,17 +231,6 @@ public function actions(Request $request): array
{
return [];
}

/**
* Get the value that should be displayed to represent the resource.
*
* @return string
*/
public function title(): string
{
return 'Name: "' . $this->name . '" (ID: ' . $this->id . ')';
}

/**
* Returns the user-friendly display name of the resource.
*
Expand Down
Loading

0 comments on commit 292c9f0

Please sign in to comment.