Skip to content

Commit

Permalink
add reservation controller
Browse files Browse the repository at this point in the history
  • Loading branch information
stazrouti committed Sep 19, 2024
1 parent 8f943a8 commit 88b20d5
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions rental-car-backend/app/Http/Controllers/ReservationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Reservation;

class ReservationController extends Controller
{
public function index()
{
$reservations = Reservation::all();
return response()->json($reservations);
}

public function store(Request $request)
{
$validated = $request->validate([
'pickup_date' => 'required|date',
'dropoff_date' => 'required|date|after_or_equal:pickup_date',
'pickup_location' => 'required|string|max:255',
'dropoff_location' => 'required|string|max:255',
'car_id' => 'required|exists:cars,id',
'first_name' => 'required|string|max:100',
'last_name' => 'required|string|max:100',
'phone_number' => 'required|string|max:20',
'email' => 'required|email|max:255',
'address' => 'required|string',
]);

$reservation = Reservation::create($validated);

return response()->json($reservation, 201);
}

public function show(Reservation $reservation)
{
return response()->json($reservation);
}

public function update(Request $request, Reservation $reservation)
{
$validated = $request->validate([
'pickup_date' => 'required|date',
'dropoff_date' => 'required|date|after_or_equal:pickup_date',
'pickup_location' => 'required|string|max:255',
'dropoff_location' => 'required|string|max:255',
'car_id' => 'required|exists:cars,id',
'first_name' => 'required|string|max:100',
'last_name' => 'required|string|max:100',
'phone_number' => 'required|string|max:20',
'email' => 'required|email|max:255',
'address' => 'required|string',
]);

$reservation->update($validated);

return response()->json($reservation);
}

public function destroy(Reservation $reservation)
{
$reservation->delete();

return response()->json(null, 204);
}
}

0 comments on commit 88b20d5

Please sign in to comment.