From 2defd89e9643a88b3b3b3c75de846f4c218014a3 Mon Sep 17 00:00:00 2001 From: Chris Friemann Date: Fri, 19 Jan 2024 09:28:38 +0100 Subject: [PATCH 1/4] feat: add post to API --- src/routes/api.php | 2 ++ 1 file changed, 2 insertions(+) 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 @@ Date: Fri, 19 Jan 2024 09:28:49 +0100 Subject: [PATCH 2/4] feat: add UserController --- src/app/Http/Controllers/UserController.php | 83 +++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/app/Http/Controllers/UserController.php diff --git a/src/app/Http/Controllers/UserController.php b/src/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..d854c93 --- /dev/null +++ b/src/app/Http/Controllers/UserController.php @@ -0,0 +1,83 @@ +validated(); + $search = $validated['search']; + + $users = User::where('last_name', 'like', "%{$search}%") + ->orWhere('first_name', 'like', "%{$search}%") + ->orWhere('email', 'like', "%{$search}%") + ->get(); + + + return $this->CommonResponse( + ResponseStatus::success, + 'Search results', + $users + ); + } +} From 4e1628a8537db149535d96f8d1edb224f0a9312b Mon Sep 17 00:00:00 2001 From: Chris Friemann Date: Fri, 19 Jan 2024 09:28:59 +0100 Subject: [PATCH 3/4] feat: add SearchUserRequest --- src/app/Http/Requests/SearchUsersRequest.php | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/app/Http/Requests/SearchUsersRequest.php 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' + ]; + } +} From a07ca4a25b373442d28c0b6dac2ad4042f86dea0 Mon Sep 17 00:00:00 2001 From: Chris Friemann Date: Fri, 19 Jan 2024 10:02:55 +0100 Subject: [PATCH 4/4] feat: search for id or librarypass barcode --- src/app/Http/Controllers/UserController.php | 7 +++++-- src/app/Models/User.php | 7 ++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/app/Http/Controllers/UserController.php b/src/app/Http/Controllers/UserController.php index d854c93..c11b1c0 100644 --- a/src/app/Http/Controllers/UserController.php +++ b/src/app/Http/Controllers/UserController.php @@ -71,8 +71,11 @@ public function search(SearchUsersRequest $request) { $users = User::where('last_name', 'like', "%{$search}%") ->orWhere('first_name', 'like', "%{$search}%") ->orWhere('email', 'like', "%{$search}%") - ->get(); - + ->orWhere('id', 'like', "%{$search}%") + ->orWhereHas('libraryPasses', function($query) use ($search) { + $query->where('barcode', 'like', "%{$search}%"); + }) + ->get(); return $this->CommonResponse( ResponseStatus::success, 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); + } }