92 lines
3.3 KiB
PHP
92 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Enums\ShiftAssignmentStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\V1\AssignShiftRequest;
|
|
use App\Http\Requests\Api\V1\StoreShiftRequest;
|
|
use App\Http\Requests\Api\V1\UpdateShiftRequest;
|
|
use App\Http\Resources\Api\V1\ShiftAssignmentResource;
|
|
use App\Http\Resources\Api\V1\ShiftResource;
|
|
use App\Models\Event;
|
|
use App\Models\FestivalSection;
|
|
use App\Models\Person;
|
|
use App\Models\Shift;
|
|
use App\Services\ShiftAssignmentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
final class ShiftController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly ShiftAssignmentService $shiftAssignmentService,
|
|
) {}
|
|
|
|
public function index(Event $event, FestivalSection $section): AnonymousResourceCollection
|
|
{
|
|
Gate::authorize('viewAny', [Shift::class, $event]);
|
|
|
|
$shifts = $section->shifts()
|
|
->with(['timeSlot', 'location'])
|
|
->withCount(['shiftAssignments as filled_slots' => fn ($q) => $q->where('status', ShiftAssignmentStatus::APPROVED)])
|
|
->get();
|
|
|
|
return ShiftResource::collection($shifts);
|
|
}
|
|
|
|
public function store(StoreShiftRequest $request, Event $event, FestivalSection $section): JsonResponse
|
|
{
|
|
Gate::authorize('create', [Shift::class, $event]);
|
|
|
|
$shift = $section->shifts()->create($request->validated());
|
|
$shift->load(['timeSlot', 'location']);
|
|
|
|
return $this->created(new ShiftResource($shift));
|
|
}
|
|
|
|
public function update(UpdateShiftRequest $request, Event $event, FestivalSection $section, Shift $shift): JsonResponse
|
|
{
|
|
Gate::authorize('update', [$shift, $event, $section]);
|
|
|
|
$shift->update($request->validated());
|
|
$shift->load(['timeSlot', 'location']);
|
|
|
|
return $this->success(new ShiftResource($shift->fresh()->load(['timeSlot', 'location'])));
|
|
}
|
|
|
|
public function destroy(Event $event, FestivalSection $section, Shift $shift): JsonResponse
|
|
{
|
|
Gate::authorize('delete', [$shift, $event, $section]);
|
|
|
|
$shift->delete();
|
|
|
|
return response()->json(null, 204);
|
|
}
|
|
|
|
public function assign(AssignShiftRequest $request, Event $event, FestivalSection $section, Shift $shift): JsonResponse
|
|
{
|
|
Gate::authorize('assign', [$shift, $event, $section]);
|
|
|
|
$festivalEventId = $event->parent_event_id ?? $event->id;
|
|
$person = Person::where('event_id', $festivalEventId)->findOrFail($request->validated('person_id'));
|
|
$assignment = $this->shiftAssignmentService->assign($shift, $person, $request->user());
|
|
|
|
return $this->created(new ShiftAssignmentResource($assignment));
|
|
}
|
|
|
|
public function claim(AssignShiftRequest $request, Event $event, FestivalSection $section, Shift $shift): JsonResponse
|
|
{
|
|
Gate::authorize('claim', [$shift, $event, $section]);
|
|
|
|
$festivalEventId = $event->parent_event_id ?? $event->id;
|
|
$person = Person::where('event_id', $festivalEventId)->findOrFail($request->validated('person_id'));
|
|
$assignment = $this->shiftAssignmentService->claim($shift, $person);
|
|
|
|
return $this->created(new ShiftAssignmentResource($assignment));
|
|
}
|
|
}
|