From 16216442435b3297a28c4790350a21cbc13bb5bd Mon Sep 17 00:00:00 2001 From: Priceless-P Date: Thu, 17 Nov 2022 11:12:33 +0100 Subject: [PATCH] feat: add get user by Id in issue #18' --- .../Http/Controllers/User/UserController.php | 28 +++++++++++++++++-- backend/routes/api.php | 5 ++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/backend/app/Http/Controllers/User/UserController.php b/backend/app/Http/Controllers/User/UserController.php index eee9c603..3700abcb 100644 --- a/backend/app/Http/Controllers/User/UserController.php +++ b/backend/app/Http/Controllers/User/UserController.php @@ -2,12 +2,36 @@ namespace App\Http\Controllers\User; -use App\Http\Controllers\Controller; use App\Models\User; +use function response; use App\Services\UserService; +use Illuminate\Http\JsonResponse; +use App\Http\Controllers\Controller; + class UserController extends Controller { + public function getUserById($user_id): JsonResponse + { + if (User::where('id', $user_id)->exists()) + { + $user = User::find($user_id); + return response()->json([ + 'error' => false, + 'message' => "User Found", + 'data' => $user + ]); + } + else + { + return response()->json([ + 'error' => false, + 'message' => "User does not exist", + 'data' => null + ], 404); + } + } + public function getVerifiedUserById($userId) { $verified_user = UserService::getVerifiedUser($userId); @@ -24,4 +48,4 @@ public function getVerifiedUserById($userId) 'user' => $verified_user ], 200); } -} \ No newline at end of file +} diff --git a/backend/routes/api.php b/backend/routes/api.php index 2458ce0a..bad954c0 100644 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -25,9 +25,10 @@ // other route functions here Route::get("/test", function(){ // execute the function - return sendResponse(false, 200, "Test case pass", null); + return sendResponse(false, 200, "Test case pass", null); }); Route::prefix("users")->group(function(){ + Route::get('{id}', [UserController::class, 'getUserById']); Route::get('verified/{userId}', [UserController::class, 'getVerifiedUserById']); -}); \ No newline at end of file +});