Skip to content

Commit

Permalink
feat: add search users endpoint #35
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrinsieboy authored Jan 19, 2024
2 parents 7a2fe13 + a07ca4a commit 5729b51
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
86 changes: 86 additions & 0 deletions src/app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Enums\ResponseStatus;
use App\Http\Requests\SearchUsersRequest;
use App\Models\User;
use App\Traits\CommonTrait;
use Inertia\Inertia;

class UserController extends Controller
{
use CommonTrait;

/**
* Display a listing of the resource.
*/
public function index()
{
// render page
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
// render page
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*/
public function show(User $user)
{
// render page
}

/**
* Update the specified resource in storage.
*/
public function update()
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy()
{
//
}

/**
* Search users
*/
public function search(SearchUsersRequest $request) {
$validated = $request->validated();
$search = $validated['search'];

$users = User::where('last_name', 'like', "%{$search}%")
->orWhere('first_name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%")
->orWhere('id', 'like', "%{$search}%")
->orWhereHas('libraryPasses', function($query) use ($search) {
$query->where('barcode', 'like', "%{$search}%");
})
->get();

return $this->CommonResponse(
ResponseStatus::success,
'Search results',
$users
);
}
}
30 changes: 30 additions & 0 deletions src/app/Http/Requests/SearchUsersRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

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

class SearchUsersRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
$user = auth()->user();
return $user->can('search', User::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'
];
}
}
7 changes: 6 additions & 1 deletion src/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
Expand All @@ -13,4 +13,9 @@ class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoles;
protected $guarded = [];

public function libraryPasses(): HasOne
{
return $this->hasOne(LibraryPass::class);
}
}
2 changes: 2 additions & 0 deletions src/routes/api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

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

Expand All @@ -16,3 +17,4 @@
*/

Route::post('/item/search', [ItemController::class, 'search']);
Route::post('/users/search', [UserController::class, 'search']);

0 comments on commit 5729b51

Please sign in to comment.