Skip to content

Commit

Permalink
item search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtendo12 committed Jan 15, 2024
1 parent a287236 commit fd03e51
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
);
}
}
30 changes: 30 additions & 0 deletions src/app/Http/Requests/SearchItemRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use App\Models\Item;
use Illuminate\Foundation\Http\FormRequest;

class SearchItemRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
$user = auth()->user();
return $user->can('search', Item::class);
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'search' => 'required|string|max:255'
];
}
}
5 changes: 2 additions & 3 deletions src/routes/api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\ItemController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Expand All @@ -14,6 +15,4 @@
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/item/search', [ItemController::class, 'search']);

0 comments on commit fd03e51

Please sign in to comment.