From 43f3bcc7f725d942678e24197c2c426f282c2868 Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:15:26 +0530 Subject: [PATCH 01/14] refactor: change code for improved readability and maintainability --- app/Models/User.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 8ceeafd..28bd537 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -12,9 +12,7 @@ * @OA\Schema( * title="User", * description="User model", - * @OA\Xml( - * name="User" - * ) + * @OA\Xml(name="User") * ) */ class User extends Authenticatable @@ -24,7 +22,7 @@ class User extends Authenticatable /** * The attributes that are mass assignable. * - * @var array + * @var array */ protected $fillable = [ 'name', @@ -35,7 +33,7 @@ class User extends Authenticatable /** * The attributes that should be hidden for serialization. * - * @var array + * @var array */ protected $hidden = [ 'password', From 1fd2e9edf63eb65aaa148e22f9f2a8c5b9ce8e82 Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:21:28 +0530 Subject: [PATCH 02/14] refactor: change code for improved readability and maintainability --- app/Models/User.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 28bd537..5a9efc4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -51,18 +51,18 @@ class User extends Authenticatable ]; /** - * User id + * User ID * * @OA\Property( - * property="id", - * description="User id", - * type="integer", - * example=1 + * property="id", + * description="User ID", + * type="integer", + * example=1 * ) * * @var int */ - private int $id; + protected $id; /** * User name From 3bfd39edc769bd65e9a4268f90885a05bc4226fc Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:24:03 +0530 Subject: [PATCH 03/14] refactor: change code for improved readability and maintainability --- app/Models/User.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 5a9efc4..055e309 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -68,29 +68,29 @@ class User extends Authenticatable * User name * * @OA\Property( - * property="name", - * description="User name", - * type="string", - * example="John Doe" + * property="name", + * description="User name", + * type="string", + * example="John Doe" * ) * * @var string */ - private string $name; + protected $name; /** * User email * * @OA\Property( - * property="email", - * description="User email", - * type="string", - * example="example@elevensoft.dev" + * property="email", + * description="User email", + * type="string", + * example="example@elevensoft.dev" * ) * * @var string */ - private string $email; + protected $email; /** * User verified at From 6c622eabee822d25b751ce552106a30127b8bd85 Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:26:10 +0530 Subject: [PATCH 04/14] refactor: Code for email verified at --- app/Models/User.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 055e309..7669c1a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -93,18 +93,18 @@ class User extends Authenticatable protected $email; /** - * User verified at + * User email verified at * * @OA\Property( - * property="email_verified_at", - * description="User email verified at", - * type="datetime", - * example="2021-01-01 00:00:00" + * property="email_verified_at", + * description="User email verified at", + * type="datetime", + * example="2021-01-01 00:00:00" * ) * - * @var Carbon + * @var Carbon|null */ - private Carbon $email_verified_at; + protected $email_verified_at; /** * User password From 7ac0305010b3f38cd13969ff66b2fb0db3cb8cbd Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:27:34 +0530 Subject: [PATCH 05/14] refactor: Code for password --- app/Models/User.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 7669c1a..18037c7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -110,15 +110,15 @@ class User extends Authenticatable * User password * * @OA\Property( - * property="password", - * description="User password", - * type="string", - * example="password" + * property="password", + * description="User password", + * type="string", + * example="password" * ) * * @var string */ - private string $password; + protected $password; /** * User remember token From 2e62de89865a956d500b7f7547aaea8187145b5f Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:30:08 +0530 Subject: [PATCH 06/14] refactor: code for remember_token --- app/Models/User.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 18037c7..4727e5c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -62,7 +62,7 @@ class User extends Authenticatable * * @var int */ - protected $id; + protected int $id; /** * User name @@ -76,7 +76,7 @@ class User extends Authenticatable * * @var string */ - protected $name; + protected string $name; /** * User email @@ -90,7 +90,7 @@ class User extends Authenticatable * * @var string */ - protected $email; + protected string $email; /** * User email verified at @@ -104,7 +104,7 @@ class User extends Authenticatable * * @var Carbon|null */ - protected $email_verified_at; + protected ?Carbon $email_verified_at; /** * User password @@ -124,15 +124,15 @@ class User extends Authenticatable * User remember token * * @OA\Property( - * property="remember_token", - * description="User remember token", - * type="string", - * example="token" + * property="remember_token", + * description="User remember token", + * type="string", + * example="token" * ) * - * @var string + * @var string|null */ - private string $remember_token; + protected ?string $remember_token; /** * User created at From b72f09081ed3037fb511ecc697b46ee20898e38a Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:31:45 +0530 Subject: [PATCH 07/14] refactor: code for created at --- app/Models/User.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 4727e5c..87e8384 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -138,15 +138,15 @@ class User extends Authenticatable * User created at * * @OA\Property( - * property="created_at", - * description="User created at", - * type="datetime", - * example="2021-01-01 00:00:00" + * property="created_at", + * description="User created at", + * type="datetime", + * example="2021-01-01 00:00:00" * ) * - * @var Carbon + * @var Carbon|null */ - private Carbon $created_at; + protected ?Carbon $created_at; /** * User updated at From 6aaadee2a30d98d43cad122d54d17be525285a1b Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:33:03 +0530 Subject: [PATCH 08/14] refactor: code for updated at --- app/Models/User.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 87e8384..ef9e3c4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -152,14 +152,14 @@ class User extends Authenticatable * User updated at * * @OA\Property( - * property="updated_at", - * description="User updated at", - * type="datetime", - * example="2021-01-01 00:00:00" + * property="updated_at", + * description="User updated at", + * type="datetime", + * example="2021-01-01 00:00:00" * ) * - * @var Carbon + * @var Carbon|null */ - private Carbon $updated_at; + protected ?Carbon $updated_at; } From 7a4a52333d4536c2b51341518febae0c4d92d3dd Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:35:17 +0530 Subject: [PATCH 09/14] refactor: code for index function --- app/Http/Controllers/UserController.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index f4a5826..b5d4b64 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,6 +4,7 @@ use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Http\Response; class UserController extends Controller { @@ -48,9 +49,14 @@ function __construct(User $user) * ) * ) */ - public function index(Request $request) + public function index() { - return $this->user->get(); + try { + $users = $this->user->all(); + return response()->json($users, Response::HTTP_OK); + } catch (\Exception $e) { + return response()->json(['error' => 'Failed to fetch users'], Response::HTTP_INTERNAL_SERVER_ERROR); + } } /** From 113060664a1f857d6a85a51352749e0267dc42fb Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:39:14 +0530 Subject: [PATCH 10/14] refactor: code for show function --- app/Http/Controllers/UserController.php | 30 ++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index b5d4b64..5232cb9 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -5,6 +5,7 @@ use App\Models\User; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Database\Eloquent\ModelNotFoundException; class UserController extends Controller { @@ -48,6 +49,8 @@ function __construct(User $user) * description="Forbidden" * ) * ) + * + * @return \Illuminate\Http\JsonResponse */ public function index() { @@ -60,9 +63,7 @@ public function index() } /** - * Show a specific user resource - * - * @return User + * Show a specific user resource. * * @OA\Get( * path="/users/{id}", @@ -78,6 +79,7 @@ public function index() * description="User ID", * required=true, * in="path", + * @OA\Schema(type="integer") * ), * @OA\Response( * response=200, @@ -91,12 +93,30 @@ public function index() * @OA\Response( * response=403, * description="Forbidden" + * ), + * @OA\Response( + * response=404, + * description="Not Found" + * ), + * @OA\Response( + * response=500, + * description="Internal Server Error" * ) * ) + * + * @param int $id + * @return \Illuminate\Http\JsonResponse */ - public function show(User $user) + public function show($id) { - return $user; + try { + $user = $this->user->findOrFail($id); + return response()->json($user, Response::HTTP_OK); + } catch (ModelNotFoundException $e) { + return response()->json(['error' => 'User not found'], Response::HTTP_NOT_FOUND); + } catch (\Exception $e) { + return response()->json(['error' => 'Failed to fetch user'], Response::HTTP_INTERNAL_SERVER_ERROR); + } } /** From d8f33d5f9f260709a2682d9d96a34a24fb9bdf76 Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:44:47 +0530 Subject: [PATCH 11/14] refactor: code for store function --- app/Http/Controllers/UserController.php | 43 ++++++++++++----- app/Http/Requests/StoreUserRequest.php | 62 +++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 app/Http/Requests/StoreUserRequest.php diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 5232cb9..e421b08 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Http\Requests\StoreUserRequest; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Http\Response; @@ -122,8 +123,6 @@ public function show($id) /** * Store a newly created user in storage. * - * @return User - * * @OA\Post( * path="/users", * operationId="storeUser", @@ -135,32 +134,52 @@ public function show($id) * }, * @OA\RequestBody( * required=true, - * @OA\JsonContent(ref="#/components/schemas/User") + * @OA\JsonContent( + * ref="#/components/schemas/User" + * ) * ), * @OA\Response( - * response=200, - * description="Successful operation", + * response=201, + * description="User successfully created", * @OA\JsonContent(ref="#/components/schemas/User") * ), * @OA\Response( + * response=400, + * description="Validation Error", + * @OA\JsonContent( + * @OA\Property(property="errors", type="object", example={"name": ["The name field is required."]}) + * ) + * ), + * @OA\Response( * response=401, * description="Unauthenticated", * ), * @OA\Response( * response=403, * description="Forbidden" + * ), + * @OA\Response( + * response=500, + * description="Internal Server Error" * ) * ) + * + * @param \App\Http\Requests\StoreUserRequest $request + * @return \Illuminate\Http\JsonResponse */ - public function store(Request $request) + public function store(StoreUserRequest $request) { - $data = $request->only([ - 'name', - 'email', - 'password', - ]); + try { + $user = $this->user->create([ + 'name' => $request->input('name'), + 'email' => $request->input('email'), + 'password' => bcrypt($request->input('password')), + ]); - return $this->user->create($data); + return response()->json($user, Response::HTTP_CREATED); + } catch (\Exception $e) { + return response()->json(['error' => 'Failed to create user'], Response::HTTP_INTERNAL_SERVER_ERROR); + } } /** diff --git a/app/Http/Requests/StoreUserRequest.php b/app/Http/Requests/StoreUserRequest.php new file mode 100644 index 0000000..0ff47dd --- /dev/null +++ b/app/Http/Requests/StoreUserRequest.php @@ -0,0 +1,62 @@ + 'required|string|max:255', + 'email' => 'required|string|email|max:255|unique:users', + 'password' => 'required|string|min:8', + ]; + } + + /** + * Get custom attributes for validator errors. + * + * @return array + */ + public function attributes() + { + return [ + 'name' => 'user name', + 'email' => 'email address', + 'password' => 'password', + ]; + } + + /** + * Get custom messages for validator errors. + * + * @return array + */ + public function messages() + { + return [ + 'name.required' => 'The user name is required.', + 'email.required' => 'The email address is required.', + 'email.unique' => 'The email address is already in use.', + 'password.required' => 'The password is required.', + ]; + } +} \ No newline at end of file From dc0b30ff4b4dd0a9600a6915809c80e29dfdadd5 Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:48:39 +0530 Subject: [PATCH 12/14] refactor: code for destroy function --- app/Http/Controllers/UserController.php | 54 ++++++++++++++++++++----- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index e421b08..3b8b1a3 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -170,10 +170,14 @@ public function show($id) public function store(StoreUserRequest $request) { try { + // Extract the required fields from the request + $data = $request->only(['name', 'email', 'password']); + + // Create a new user with the provided data $user = $this->user->create([ - 'name' => $request->input('name'), - 'email' => $request->input('email'), - 'password' => bcrypt($request->input('password')), + 'name' => $data['name'], + 'email' => $data['email'], + 'password' => bcrypt($data['password']), ]); return response()->json($user, Response::HTTP_CREATED); @@ -235,9 +239,7 @@ public function update(Request $request, User $user) } /** - * Remove a specific user resource - * - * @return User + * Remove a specific user resource. * * @OA\Delete( * path="/users/{id}", @@ -253,27 +255,57 @@ public function update(Request $request, User $user) * description="User ID", * required=true, * in="path", + * @OA\Schema(type="integer") * ), * @OA\Response( * response=200, * description="Successful operation", - * @OA\JsonContent(ref="#/components/schemas/User") + * @OA\JsonContent( + * @OA\Property(property="message", type="string", example="User successfully deleted") + * ) + * ), + * @OA\Response( + * response=404, + * description="User not found", + * @OA\JsonContent( + * @OA\Property(property="error", type="string", example="User not found") + * ) * ), * @OA\Response( * response=401, - * description="Unauthenticated", + * description="Unauthenticated" * ), * @OA\Response( * response=403, * description="Forbidden" + * ), + * @OA\Response( + * response=500, + * description="Internal Server Error" * ) * ) + * + * @param int $id + * @return \Illuminate\Http\JsonResponse */ - public function destroy(User $user) + public function destroy($id) { - $user->delete(); + try { + // Attempt to find the user by ID + $user = $this->user->findOrFail($id); - return $user; + // Delete the user + $user->delete(); + + // Return a JSON response indicating successful deletion + return response()->json(['message' => 'User successfully deleted'], Response::HTTP_OK); + } catch (ModelNotFoundException $e) { + // Return a JSON response if the user is not found + return response()->json(['error' => 'User not found'], Response::HTTP_NOT_FOUND); + } catch (\Exception $e) { + // Return a JSON response for other errors + return response()->json(['error' => 'Failed to delete user'], Response::HTTP_INTERNAL_SERVER_ERROR); + } } } From 98aff188ed03d65b8ce56b50f3d2abc1302e4573 Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:51:59 +0530 Subject: [PATCH 13/14] refactor: code for update function --- app/Http/Controllers/UserController.php | 68 ++++++++++++++++++++----- app/Http/Requests/StoreUserRequest.php | 2 +- app/Http/Requests/UpdateUserRequest.php | 64 +++++++++++++++++++++++ 3 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 app/Http/Requests/UpdateUserRequest.php diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 3b8b1a3..4e12841 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Http\Requests\StoreUserRequest; +use App\Http\Requests\UpdateUserRequest; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Http\Response; @@ -187,9 +188,7 @@ public function store(StoreUserRequest $request) } /** - * Update a specific user resource - * - * @return User + * Update a specific user resource. * * @OA\Put( * path="/users/{id}", @@ -205,10 +204,13 @@ public function store(StoreUserRequest $request) * description="User ID", * required=true, * in="path", + * @OA\Schema(type="integer") * ), * @OA\RequestBody( * required=true, - * @OA\JsonContent(ref="#/components/schemas/User") + * @OA\JsonContent( + * ref="#/components/schemas/User" + * ) * ), * @OA\Response( * response=200, @@ -216,26 +218,66 @@ public function store(StoreUserRequest $request) * @OA\JsonContent(ref="#/components/schemas/User") * ), * @OA\Response( + * response=400, + * description="Validation Error", + * @OA\JsonContent( + * @OA\Property(property="errors", type="object", example={"email": ["The email field must be a valid email address."]}) + * ) + * ), + * @OA\Response( * response=401, - * description="Unauthenticated", + * description="Unauthenticated" * ), * @OA\Response( * response=403, * description="Forbidden" + * ), + * @OA\Response( + * response=404, + * description="User not found", + * @OA\JsonContent( + * @OA\Property(property="error", type="string", example="User not found") + * ) + * ), + * @OA\Response( + * response=500, + * description="Internal Server Error" * ) * ) + * + * @param \App\Http\Requests\UpdateUserRequest $request + * @param int $id + * @return \Illuminate\Http\JsonResponse */ - public function update(Request $request, User $user) + public function update(UpdateUserRequest $request, $id) { - $data = $request->only([ - 'name', - 'email', - 'password', - ]); + try { + // Retrieve the user by ID + $user = $this->user->findOrFail($id); - $user->update($data); + // Extract data from the request + $data = $request->only(['name', 'email', 'password']); + + // Check if the password is provided and needs to be hashed + if ($request->has('password')) { + $data['password'] = bcrypt($data['password']); + } else { + // Remove the password key if not provided + unset($data['password']); + } + + // Update the user with the validated data + $user->update($data); - return $user; + // Return the updated user + return response()->json($user, Response::HTTP_OK); + } catch (ModelNotFoundException $e) { + // Return a JSON response if the user is not found + return response()->json(['error' => 'User not found'], Response::HTTP_NOT_FOUND); + } catch (\Exception $e) { + // Return a JSON response for other errors + return response()->json(['error' => 'Failed to update user'], Response::HTTP_INTERNAL_SERVER_ERROR); + } } /** diff --git a/app/Http/Requests/StoreUserRequest.php b/app/Http/Requests/StoreUserRequest.php index 0ff47dd..568df18 100644 --- a/app/Http/Requests/StoreUserRequest.php +++ b/app/Http/Requests/StoreUserRequest.php @@ -59,4 +59,4 @@ public function messages() 'password.required' => 'The password is required.', ]; } -} \ No newline at end of file +} diff --git a/app/Http/Requests/UpdateUserRequest.php b/app/Http/Requests/UpdateUserRequest.php new file mode 100644 index 0000000..45a45a7 --- /dev/null +++ b/app/Http/Requests/UpdateUserRequest.php @@ -0,0 +1,64 @@ +route('id'); + return [ + 'name' => 'nullable|string|max:255', + 'email' => 'nullable|email|unique:users,email,' . $userId, + 'password' => 'nullable|string|min:8', + ]; + } + + + /** + * Get custom attributes for validator errors. + * + * @return array + */ + public function attributes() + { + return [ + 'name' => 'user name', + 'email' => 'email address', + 'password' => 'password', + ]; + } + + /** + * Get custom messages for validator errors. + * + * @return array + */ + public function messages() + { + return [ + 'name.required' => 'The user name is required.', + 'email.required' => 'The email address is required.', + 'email.unique' => 'The email address is already in use.', + 'password.required' => 'The password is required.', + ]; + } +} From 6ac80c593fd2f203b2b30080cb6f3e3f5b6eab38 Mon Sep 17 00:00:00 2001 From: 1986webdeveloper <1986webdeveloper@gmail.com> Date: Fri, 26 Jul 2024 14:55:34 +0530 Subject: [PATCH 14/14] Created ANSWER.md file --- ANSWER.md | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 ANSWER.md diff --git a/ANSWER.md b/ANSWER.md new file mode 100644 index 0000000..b78f161 --- /dev/null +++ b/ANSWER.md @@ -0,0 +1,107 @@ +# Code Refactor + +## Overview + +This document details the recent enhancements and updates made to the API endpoints, including validation, error handling, HTTP status codes, and OpenAPI annotations. + +## User Controller + +### Index Function + +#### Changes and Enhancements + +- *Error Handling:* + - Wrapped the user fetching logic in a `try-catch` block to handle exceptions. + - Returns an appropriate error message for better error handling. + +- *HTTP Status Codes:* + - Used `Response::HTTP_*` constants for readability and standard compliance. + +### Show Function + +#### Error Handling + +- *Exception Handling:* + - Wrapped the user fetching logic in a `try-catch` block. + - Handles specific exceptions (e.g., `ModelNotFoundException` for non-existent users) and general exceptions. + +#### HTTP Status Codes + +- Used Response: Used Response::HTTP_* constants for readability and standard compliance + +#### Validation + +- Ensured the id parameter is validated by trying to find the user and handling the ModelNotFoundException. + +## Store Function + +### FormRequest Class (StoreUserRequest) + +- *Validation Handling:* + - Utilizes `StoreUserRequest` to automatically handle validation. + - Returns a `400 Bad Request` response with validation errors if validation fails. + +- *User Creation:* + - Creates a user if validation passes. + - Returns a `201 Created` response upon successful creation. + +- *Error Handling:* + - Catches unexpected errors and returns a `500 Internal Server Error` response. + +## Update Function + +#### Validation Handling + +- *UpdateUserRequest:* + - Automatically validates incoming data. + - Updates only the provided fields. If the password is not included, it remains unchanged. + +#### Error Handling + +- *Exception Handling:* + - Catches exceptions and returns appropriate HTTP responses. + +#### Swagger/OpenAPI Annotations + +- *@OA\Put Annotation:* + - Defines the endpoint, operation, request body, and responses. + +- *Response Codes:* + - Includes common responses: `200` for success, `400` for validation errors, `404` for not found, and `500` for internal server errors. + +## Destroy Function + +#### Finding the User + +- *User Retrieval:* + - Utilizes `$this->user->findOrFail($id)` to retrieve the user by ID or throw a `ModelNotFoundException`. + +#### Deleting the User + +- *Deletion Logic:* + - Deletes the user if found. + +#### Exception Handling + +- *ModelNotFoundException:* + - Specifically catches cases where the user is not found. + +- *General Exception:* + - Catches any other unexpected errors. + +## User Model + +### Property Changes + +- *Removed Private Properties:* + - Changed private properties to protected to leverage Eloquent’s default behavior. + +### Updated Docblocks + +- *Docblock Adjustments:* + - Updated descriptions and types to align with actual usage. + +### Nullability + +- *Nullable Properties:* + - Added `?` to nullable `Carbon` properties where appropriate. \ No newline at end of file