2025-09-28 13:31:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Infrastructure\Http\Controllers\API\PlannableItem;
|
|
|
|
|
|
|
|
|
|
use App\Infrastructure\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\PlannableItem;
|
|
|
|
|
use App\Models\Trip;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
|
|
|
|
|
class PlannableItemController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index(Trip $trip): JsonResponse
|
|
|
|
|
{
|
2025-09-30 08:29:17 +02:00
|
|
|
// Check if user owns the trip
|
|
|
|
|
if ($trip->created_by_user_id !== auth()->id()) {
|
|
|
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 13:31:43 +02:00
|
|
|
$plannableItems = $trip->plannableItems()
|
|
|
|
|
->with(['calendarSlots'])
|
|
|
|
|
->get();
|
|
|
|
|
|
2025-09-30 08:29:17 +02:00
|
|
|
return response()->json(['data' => $plannableItems]);
|
2025-09-28 13:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request, Trip $trip): JsonResponse
|
|
|
|
|
{
|
2025-09-30 08:29:17 +02:00
|
|
|
// Check if user owns the trip
|
|
|
|
|
if ($trip->created_by_user_id !== auth()->id()) {
|
|
|
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 13:31:43 +02:00
|
|
|
$validated = $request->validate([
|
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
|
'type' => 'required|in:hotel,restaurant,attraction,transport,activity',
|
|
|
|
|
'address' => 'nullable|string|max:255',
|
|
|
|
|
'notes' => 'nullable|string',
|
|
|
|
|
'metadata' => 'nullable|array',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$plannableItem = $trip->plannableItems()->create($validated);
|
|
|
|
|
|
2025-09-30 08:29:17 +02:00
|
|
|
return response()->json(['data' => $plannableItem], 201);
|
2025-09-28 13:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show(PlannableItem $plannableItem): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$plannableItem->load(['calendarSlots', 'trip']);
|
|
|
|
|
return response()->json($plannableItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, PlannableItem $plannableItem): JsonResponse
|
|
|
|
|
{
|
2025-09-30 08:29:17 +02:00
|
|
|
// Check if user owns the trip
|
|
|
|
|
if ($plannableItem->trip->created_by_user_id !== auth()->id()) {
|
|
|
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 13:31:43 +02:00
|
|
|
$validated = $request->validate([
|
|
|
|
|
'name' => 'sometimes|required|string|max:255',
|
|
|
|
|
'type' => 'sometimes|required|in:hotel,restaurant,attraction,transport,activity',
|
|
|
|
|
'address' => 'nullable|string|max:255',
|
|
|
|
|
'notes' => 'nullable|string',
|
|
|
|
|
'metadata' => 'nullable|array',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$plannableItem->update($validated);
|
|
|
|
|
|
2025-09-30 08:29:17 +02:00
|
|
|
return response()->json(['data' => $plannableItem]);
|
2025-09-28 13:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy(PlannableItem $plannableItem): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$plannableItem->delete();
|
|
|
|
|
|
|
|
|
|
return response()->json(null, 204);
|
|
|
|
|
}
|
|
|
|
|
}
|