Skip to content

Commit

Permalink
[Update] Season Resource
Browse files Browse the repository at this point in the history
- Added `ratingAverage` attribute
  • Loading branch information
kiritokatklian committed Aug 21, 2024
1 parent cbd68e0 commit 2c14101
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
10 changes: 8 additions & 2 deletions app/Http/Controllers/API/v1/SeasonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ public function details(Request $request, Season $season): JsonResponse
'anime' => function ($query) {
$query->withoutGlobalScopes();
},
'media'
'media',
'translations',
])
->loadCount(['episodes']);
->loadCount(['episodes'])
->loadAvg([
'episodesMediaStats as rating_average' => function ($query) {
$query->where('rating_average', '!=', 0);
}
], 'rating_average');

return JSONResult::success([
'data' => SeasonResource::collection([$season]),
Expand Down
21 changes: 11 additions & 10 deletions app/Http/Resources/SeasonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ class SeasonResource extends JsonResource
* Transform the resource into an array.
*
* @param Request $request
*
* @return array
*/
public function toArray(Request $request): array
{
$resource = SeasonResourceIdentity::make($this->resource)->toArray($request);
$resource = array_merge($resource, [
'attributes' => [
'poster' => ImageResource::make(
'attributes' => [
'poster' => ImageResource::make(
$this->resource->media->firstWhere('collection_name', '=', MediaCollection::Poster) ??
$this->resource->anime->media->firstWhere('collection_name', '=', MediaCollection::Poster)
),
'number' => $this->resource->number,
'title' => $this->resource->title,
'synopsis' => $this->resource->synopsis,
'episodeCount' => $this->resource->episodes_count,
'startedAt' => $this->resource->started_at?->timestamp,
'firstAired' => $this->resource->started_at?->timestamp,
'endedAt' => $this->resource->ended_at?->timestamp,
'number' => $this->resource->number,
'title' => $this->resource->title,
'synopsis' => $this->resource->synopsis,
'episodeCount' => $this->resource->episodes_count,
'ratingAverage' => round($this->resource->rating_average ?? 0, 1),
'startedAt' => $this->resource->started_at?->timestamp,
'firstAired' => $this->resource->started_at?->timestamp,
'endedAt' => $this->resource->ended_at?->timestamp,
]
]);

Expand All @@ -49,7 +51,6 @@ public function toArray(Request $request): array
return $resource;
}


/**
* Returns the user specific details for the resource.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Season/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getSeasonsProperty(): Collection|LengthAwarePaginator
])
->withCount(['episodes'])
->withAvg([
'episodesMediaStats' => function ($query) {
'episodesMediaStats as rating_average' => function ($query) {
$query->where('rating_average', '!=', 0);
}
], 'rating_average')
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/lockups/poster-lockup.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class="relative shrink-0 w-28 h-40 mr-2 rounded-lg overflow-hidden"

<div class="flex flex-wrap w-full justify-between">
<p class="text-sm opacity-75">{{ __('Score') }}</p>
<p class="text-sm">{{ number_shorten($season->episodes_media_stats_avg_rating_average, 1) }}</p>
<p class="text-sm">{{ number_shorten($season->rating_average ?? 0, 1) }}</p>
</div>
</div>
</div>
Expand Down

0 comments on commit 2c14101

Please sign in to comment.