From ef7817e6898ba3d5652014d34b97a06d547c7e9d Mon Sep 17 00:00:00 2001 From: Tim Slager Date: Mon, 15 Jan 2024 15:16:30 +0100 Subject: [PATCH] feature: added reservation search --- .../Controllers/ReservationController.php | 20 +++++++++++++ .../Requests/SearchReservationRequest.php | 30 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/app/Http/Requests/SearchReservationRequest.php diff --git a/src/app/Http/Controllers/ReservationController.php b/src/app/Http/Controllers/ReservationController.php index ea464ff..17e8c23 100644 --- a/src/app/Http/Controllers/ReservationController.php +++ b/src/app/Http/Controllers/ReservationController.php @@ -5,9 +5,13 @@ use App\Enums\ModifiedEnum; use App\Enums\ResponseStatus; use App\Http\Requests\ReserveRequest; +use App\Http\Requests\SearchReservationRequest; +use App\Models\Item; use App\Models\Reservation; +use App\Models\User; use App\Traits\CommonTrait; use Illuminate\Http\Request; +use Laravel\SerializableClosure\UnsignedSerializableClosure; class ReservationController extends Controller { @@ -98,4 +102,20 @@ public function destroy(Reservation $reservation) null, ); } + + public function search(SearchReservationRequest $request) + { + $validated = $request->validated(); + $search = $validated['search']; + + $item = Item::where('name', 'like', "%{$search}%")->orWhere('ISBN', 'like', "%{$search}%")->first(); + $user = User::where('last_name', 'like', "%{$search}%")->orWhere('email', 'like', "%{$search}%")->orWhere('zip_code', 'like', "%{$search}%")->first(); + + $reservations = Reservation::where('item_id', $item->id)->orWhere('user_id', $user->id)->get(); + + return $this->CommonResponse(ResponseStatus::success, + 'Reservation found', + ["reservations" => $reservations], + ); + } } diff --git a/src/app/Http/Requests/SearchReservationRequest.php b/src/app/Http/Requests/SearchReservationRequest.php new file mode 100644 index 0000000..678afc2 --- /dev/null +++ b/src/app/Http/Requests/SearchReservationRequest.php @@ -0,0 +1,30 @@ +user(); + return $user->can('search', Reservation::class); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + 'search' => 'required|string|min:3|max:255', + ]; + } +}