updated controllers to use resources

This commit is contained in:
Edward Tirado Jr 2026-04-05 00:37:44 -05:00
parent 0787b75780
commit 19f81a0024
6 changed files with 79 additions and 18 deletions

View file

@ -0,0 +1,42 @@
<?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);
}
}