From fd03e51652458eab47c8e1b2d1ed728f0f55287f Mon Sep 17 00:00:00 2001 From: Tim Slager Date: Mon, 15 Jan 2024 13:08:43 +0100 Subject: [PATCH] item search endpoint --- src/app/Http/Controllers/ItemController.php | 18 +++++++++++++ src/app/Http/Requests/SearchItemRequest.php | 30 +++++++++++++++++++++ src/routes/api.php | 5 ++-- 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/app/Http/Requests/SearchItemRequest.php diff --git a/src/app/Http/Controllers/ItemController.php b/src/app/Http/Controllers/ItemController.php index 5994b9d..648b41d 100644 --- a/src/app/Http/Controllers/ItemController.php +++ b/src/app/Http/Controllers/ItemController.php @@ -5,6 +5,7 @@ use App\Enums\ModifiedEnum; use App\Enums\ResponseStatus; use App\Http\Requests\ItemRequest; +use App\Http\Requests\SearchItemRequest; use App\Models\Item; use App\Traits\CommonTrait; @@ -95,4 +96,21 @@ public function destroy(Item $item) null, ); } + + public function search(SearchItemRequest $request) { + $validated = $request->validated(); + $search = $validated['search']; + + $items = Item::where('name', 'like', "%{$search}%") + ->orWhere('description', 'like', "%{$search}%") + ->orWhere('category', 'like', "%{$search}%") + ->orWhere('ISBN', 'like', "%{$search}%") + ->get(); + + return $this->CommonResponse( + ResponseStatus::success, + 'Search results', + $items + ); + } } diff --git a/src/app/Http/Requests/SearchItemRequest.php b/src/app/Http/Requests/SearchItemRequest.php new file mode 100644 index 0000000..36fcb14 --- /dev/null +++ b/src/app/Http/Requests/SearchItemRequest.php @@ -0,0 +1,30 @@ +user(); + return $user->can('search', Item::class); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + 'search' => 'required|string|max:255' + ]; + } +} diff --git a/src/routes/api.php b/src/routes/api.php index 35f3522..21be859 100644 --- a/src/routes/api.php +++ b/src/routes/api.php @@ -1,5 +1,6 @@ get('/user', function (Request $request) { - return $request->user(); -}); \ No newline at end of file +Route::post('/item/search', [ItemController::class, 'search']);