2025-09-28 13:31:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Infrastructure\Http\Controllers\API\PlannedItem;
|
|
|
|
|
|
2025-10-06 14:21:47 +02:00
|
|
|
use App\Domain\PlannedItem\Actions\CreatePlannedItemAction;
|
|
|
|
|
use App\Domain\PlannedItem\Actions\UpdatePlannedItemAction;
|
|
|
|
|
use App\Domain\PlannedItem\Actions\DeletePlannedItemAction;
|
|
|
|
|
use App\Domain\PlannedItem\Policies\PlannedItemPolicy;
|
2025-09-28 13:31:43 +02:00
|
|
|
use App\Infrastructure\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\PlannedItem;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2025-10-06 14:21:47 +02:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2025-09-28 13:31:43 +02:00
|
|
|
|
|
|
|
|
class PlannedItemController extends Controller
|
|
|
|
|
{
|
2025-10-06 14:21:47 +02:00
|
|
|
public function __construct(
|
|
|
|
|
private CreatePlannedItemAction $createAction,
|
|
|
|
|
private UpdatePlannedItemAction $updateAction,
|
|
|
|
|
private DeletePlannedItemAction $deleteAction,
|
|
|
|
|
private PlannedItemPolicy $policy
|
|
|
|
|
) {}
|
|
|
|
|
|
2025-09-28 13:31:43 +02:00
|
|
|
public function store(Request $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'plannable_item_id' => 'required|exists:plannable_items,id',
|
2025-10-06 14:21:47 +02:00
|
|
|
'trip_id' => 'required|exists:trips,id',
|
|
|
|
|
'start_datetime' => 'required|date',
|
|
|
|
|
'end_datetime' => 'required|date|after:start_datetime',
|
|
|
|
|
'calendar_slot_id' => 'sometimes|exists:calendar_slots,id',
|
2025-09-28 13:31:43 +02:00
|
|
|
'sort_order' => 'nullable|integer',
|
|
|
|
|
]);
|
|
|
|
|
|
2025-10-06 14:21:47 +02:00
|
|
|
// Check authorization using Policy
|
|
|
|
|
if (!$this->policy->create(auth()->user(), $validated['trip_id'])) {
|
|
|
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If calendar_slot_id is provided, use existing flow (backward compatibility)
|
|
|
|
|
if (isset($validated['calendar_slot_id'])) {
|
|
|
|
|
$plannedItem = $this->createAction->executeFromSlot($validated);
|
|
|
|
|
return response()->json($plannedItem, 201);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New flow: Create CalendarSlot from datetime using Action
|
|
|
|
|
try {
|
|
|
|
|
$calendarSlot = $this->createAction->execute($validated);
|
|
|
|
|
return response()->json($calendarSlot, 201);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
|
|
|
}
|
2025-09-28 13:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, PlannedItem $plannedItem): JsonResponse
|
|
|
|
|
{
|
2025-10-06 14:21:47 +02:00
|
|
|
// Check authorization using Policy
|
|
|
|
|
if (!$this->policy->update(auth()->user(), $plannedItem)) {
|
|
|
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 13:31:43 +02:00
|
|
|
$validated = $request->validate([
|
|
|
|
|
'calendar_slot_id' => 'sometimes|required|exists:calendar_slots,id',
|
|
|
|
|
'sort_order' => 'nullable|integer',
|
|
|
|
|
]);
|
|
|
|
|
|
2025-10-06 14:21:47 +02:00
|
|
|
// If changing calendar slot, verify user owns the new slot's trip
|
|
|
|
|
if (isset($validated['calendar_slot_id'])) {
|
|
|
|
|
if (!$this->policy->moveToSlot(auth()->user(), $validated['calendar_slot_id'])) {
|
|
|
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute update using Action
|
|
|
|
|
$updatedItem = $this->updateAction->execute($plannedItem, $validated);
|
2025-09-28 13:31:43 +02:00
|
|
|
|
2025-10-06 14:21:47 +02:00
|
|
|
return response()->json($updatedItem);
|
2025-09-28 13:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy(PlannedItem $plannedItem): JsonResponse
|
|
|
|
|
{
|
2025-10-06 14:21:47 +02:00
|
|
|
// Check authorization using Policy
|
|
|
|
|
if (!$this->policy->delete(auth()->user(), $plannedItem)) {
|
|
|
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute delete using Action
|
|
|
|
|
$this->deleteAction->execute($plannedItem);
|
2025-09-28 13:31:43 +02:00
|
|
|
|
|
|
|
|
return response()->json(null, 204);
|
|
|
|
|
}
|
|
|
|
|
}
|