Skip to content

A self-hosted media server and file management system

2.8 KB
Raw Blame
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SeriesResource extends JsonResource {
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array {
        $library = $this->relationLoaded('folder') && $this->folder?->relationLoaded('category') ? $this->folder->category : null;

        return [
            'id' => $this->id,
            'uuid' => $this->uuid,
            'folder_id' => $this->folder_id,
            'editor_id' => $this->editor_id,
            'title' => $this->title ?? $this->folder?->name,
            'description' => $this->description,
            'studio' => $this->studio,
            'rating' => $this->rating,
            'seasons' => $this->seasons,
            'episodes' => $this->episodes,
            'films' => $this->films,
            'avg_intro_duration' => $this->avg_intro_duration,
            'folder_tags' => $this->whenLoaded('folderTags', fn () => FolderTagResource::collection($this->folderTags), []),
            'started_at' => $this->started_at,
            'ended_at' => $this->ended_at,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'edited_at' => $this->edited_at,
            'downloads_enabled' => $this->downloads_enabled,

            'thumbnail_url' => $this->primary_poster_id ? null : $this->thumbnail_url,
            'poster_image' => $this->whenLoaded('primaryPoster', fn () => $this->primaryPoster ? new ImageResource($this->primaryPoster) : null),
            'banner_image' => $this->whenLoaded('primaryBanner', fn () => $this->primaryBanner ? new ImageResource($this->primaryBanner) : null),
            'primary_banner_id' => $this->primary_banner_id,
            'images' => $this->whenLoaded('images', fn () => ImageResource::collection($this->images), []),

            'related_series' => $this->when(
                $this->relationLoaded('directRelations') && $this->relationLoaded('inverseRelations'),
                fn () => $this->relatedSeriesPayload(),
                [],
            ),

            'library_id' => $library?->id,
            'library_name' => $library?->name,
        ];
    }

    public function relatedSeriesPayload(): array {
        // should not lazy load
        return $this->allRelations()
            ->filter(fn ($r) => $r['series']->folder)
            ->map(function ($r) {
                $series = $r['series'];
                $series->folder->setRelation('series', $series);

                return [
                    'type' => $r['type']->value,
                    'label' => $r['type']->label(),
                    'folder' => new FolderResource($series->folder),
                ];
            })
            ->values()
            ->all();
    }
}