Files
crewli/api/app/Http/Controllers/Api/V1/Artist/TimetableMoveController.php
bert.hausmans 32da6b656d feat(timetable): six artist-domain controllers + RFC §6 routes
Six thin controllers under app/Http/Controllers/Api/V1/Artist/. Zero
business logic: every mutation routes through a service from
app/Services/Artist/. Authorization via Gate::authorize matching
PersonController convention (request authorize() returns true; gates
fire in the controller).

  ArtistController          — org-scoped CRUD + restore. Catches
                              DuplicateArtistException → 409 with
                              duplicate_artist_id so the dialog can
                              offer "use existing".
  GenreController           — org-scoped CRUD; catches GenreInUseException
                              → 409 with referencing_artists_count.
  ArtistEngagementController — event-scoped CRUD; catches
                              InvalidStatusTransitionException → 422
                              with a Dutch-readable message.
  StageController           — event-scoped CRUD + reorder + replaceDays;
                              catches StageDaysOrphanedPerformancesException
                              → 409 with the orphaned performance ids
                              and the removed event ids per RFC §10.5.
                              destroy returns the parked performance
                              count (cascade-park).
  PerformanceController     — event-scoped CRUD with index filters
                              `?day={subevent}` and `?stage_id=null`
                              (wachtrij). update is non-placement only.
  TimetableMoveController   — single __invoke for POST /timetable/move.
                              Catches VersionMismatchException → 409
                              with current_version + server_data per
                              RFC D14.

Routes wired into api/routes/api.php nested under the existing
organisations/{organisation}/events/{event} prefix group, matching
PersonController and ShiftController structure. The move endpoint
gets the new `idempotency.60s` middleware alias for R1. `stages/order`
and `stages/{stage}/days` registered before the apiResource so the
literal path wins over the wildcard.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 20:56:43 +02:00

84 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\V1\Artist;
use App\Exceptions\Artist\VersionMismatchException;
use App\Http\Controllers\Api\V1\Traits\VerifiesOrganisationEvent;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Artist\MoveTimetablePerformanceRequest;
use App\Http\Resources\Api\V1\Artist\PerformanceResource;
use App\Models\Event;
use App\Models\Organisation;
use App\Models\Performance;
use App\Models\Stage;
use App\Services\Artist\LaneCascadeService;
use Carbon\CarbonImmutable;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Gate;
final class TimetableMoveController extends Controller
{
use VerifiesOrganisationEvent;
public function __construct(
private readonly LaneCascadeService $service,
) {}
public function __invoke(
MoveTimetablePerformanceRequest $request,
Organisation $organisation,
Event $event,
): JsonResponse {
$this->verifyEventBelongsToOrganisation($organisation, $event);
$data = $request->validated();
$performance = Performance::query()->findOrFail($data['performance_id']);
Gate::authorize('move', [$performance, $event]);
$targetStage = isset($data['target_stage_id'])
? Stage::query()->find($data['target_stage_id'])
: null;
$start = isset($data['target_start_at'])
? CarbonImmutable::parse((string) $data['target_start_at'])
: null;
$end = isset($data['target_end_at'])
? CarbonImmutable::parse((string) $data['target_end_at'])
: null;
try {
$result = $this->service->move(
performance: $performance,
targetStage: $targetStage,
start: $start,
end: $end,
targetLane: isset($data['target_lane']) ? (int) $data['target_lane'] : null,
clientVersion: (int) $data['version'],
);
} catch (VersionMismatchException $e) {
$performance->refresh();
return $this->error('Version mismatch — performance was modified by another request.', 409, [
'conflict' => 'version_mismatch',
'current_version' => $e->currentVersion,
'client_version' => $e->clientVersion,
'server_data' => PerformanceResource::make(
$performance->load(['engagement.artist.defaultGenre', 'stage']),
)->toArray(request()),
]);
}
return $this->success([
'moved' => PerformanceResource::make(
$result->moved->load(['engagement.artist.defaultGenre', 'stage']),
),
'cascaded' => PerformanceResource::collection(
collect($result->cascaded)->each->load(['engagement.artist.defaultGenre', 'stage']),
),
]);
}
}