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', + ]; + } +}