diff --git a/src/app/Http/Controllers/UserController.php b/src/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..c11b1c0 --- /dev/null +++ b/src/app/Http/Controllers/UserController.php @@ -0,0 +1,86 @@ +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 + ); + } +} diff --git a/src/app/Http/Requests/SearchUsersRequest.php b/src/app/Http/Requests/SearchUsersRequest.php new file mode 100644 index 0000000..e684f31 --- /dev/null +++ b/src/app/Http/Requests/SearchUsersRequest.php @@ -0,0 +1,30 @@ +user(); + return $user->can('search', User::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/app/Models/User.php b/src/app/Models/User.php index 569f29f..abbb268 100644 --- a/src/app/Models/User.php +++ b/src/app/Models/User.php @@ -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; @@ -13,4 +13,9 @@ class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, HasRoles; protected $guarded = []; + + public function libraryPasses(): HasOne + { + return $this->hasOne(LibraryPass::class); + } } diff --git a/src/routes/api.php b/src/routes/api.php index 21be859..d6d7d4d 100644 --- a/src/routes/api.php +++ b/src/routes/api.php @@ -1,6 +1,7 @@