first commit
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\TripResource;
|
||||
use App\Models\Trip;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TripController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of user's trips
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$trips = $request->user()
|
||||
->trips()
|
||||
->withCount('cities')
|
||||
->latest()
|
||||
->get();
|
||||
|
||||
return TripResource::collection($trips);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created trip
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'city_ids' => 'nullable|array',
|
||||
'city_ids.*' => 'exists:cities,id',
|
||||
]);
|
||||
|
||||
$trip = $request->user()->trips()->create([
|
||||
'name' => $validated['name'],
|
||||
'description' => $validated['description'] ?? null,
|
||||
]);
|
||||
|
||||
// Attach cities if provided
|
||||
if (!empty($validated['city_ids'])) {
|
||||
$cityData = [];
|
||||
foreach ($validated['city_ids'] as $index => $cityId) {
|
||||
$cityData[$cityId] = ['order' => $index];
|
||||
}
|
||||
$trip->cities()->attach($cityData);
|
||||
}
|
||||
|
||||
$trip->load('cities')->loadCount('cities');
|
||||
|
||||
return new TripResource($trip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified trip
|
||||
*/
|
||||
public function show(Request $request, Trip $trip)
|
||||
{
|
||||
// Check if trip belongs to user
|
||||
if ($trip->user_id !== $request->user()->id) {
|
||||
return response()->json([
|
||||
'message' => 'Unauthorized',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$trip->load('cities')->loadCount('cities');
|
||||
|
||||
return new TripResource($trip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified trip
|
||||
*/
|
||||
public function update(Request $request, Trip $trip)
|
||||
{
|
||||
// Check if trip belongs to user
|
||||
if ($trip->user_id !== $request->user()->id) {
|
||||
return response()->json([
|
||||
'message' => 'Unauthorized',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'sometimes|required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'city_ids' => 'nullable|array',
|
||||
'city_ids.*' => 'exists:cities,id',
|
||||
]);
|
||||
|
||||
$trip->update([
|
||||
'name' => $validated['name'] ?? $trip->name,
|
||||
'description' => $validated['description'] ?? $trip->description,
|
||||
]);
|
||||
|
||||
// Update cities if provided
|
||||
if (isset($validated['city_ids'])) {
|
||||
$cityData = [];
|
||||
foreach ($validated['city_ids'] as $index => $cityId) {
|
||||
$cityData[$cityId] = ['order' => $index];
|
||||
}
|
||||
$trip->cities()->sync($cityData);
|
||||
}
|
||||
|
||||
$trip->load('cities')->loadCount('cities');
|
||||
|
||||
return new TripResource($trip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified trip
|
||||
*/
|
||||
public function destroy(Request $request, Trip $trip)
|
||||
{
|
||||
// Check if trip belongs to user
|
||||
if ($trip->user_id !== $request->user()->id) {
|
||||
return response()->json([
|
||||
'message' => 'Unauthorized',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$trip->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Trip deleted successfully',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user