movie-night-api/app/Http/Resources/MovieListResource.php

43 lines
1.1 KiB
PHP
Raw Normal View History

2026-04-05 00:37:44 -05:00
<?php
namespace App\Http\Resources;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class MovieListResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$user_id = Auth::id();
return [
'id' => $this->id,
'name' => $this->name,
'is_public' => $this->is_public,
'owner' => $this->listOwner->username,
'role' => $this->getRole($this->owner, $user_id),
'collaborators' => $this->whenLoaded('collaborators', fn () => $this->collaborators->map(fn ($user) => [
'username' => $user->username,
'role' => $user->pivot->role,
])),
'movies' => $this->whenLoaded('movies'),
];
}
private function getRole(int $owner_id, int $user_id): ?string
{
if ($owner_id === $user_id) {
return 'owner';
}
return $this->getUserRole($user_id);
}
}