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']);