Skip to content

refactor: change code for improved readability and maintainability #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

L5_SWAGGER_CONST_HOST=${APP_URL}/api
L5_SWAGGER_USE_ABSOLUTE_PATH=true
L5_FORMAT_TO_USE_FOR_DOCS=json
L5_SWAGGER_GENERATE_ALWAYS=true
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
```markdown
# Eleven Soft Backend Refactoring Test

Welcome to the Eleven Soft Backend Refactoring Test repository! This Laravel-based project is designed to evaluate your backend skills, particularly in refactoring and improving the existing codebase.
Expand All @@ -16,7 +17,7 @@ Ensure that you have the following prerequisites installed on your local machine

### Installation and Setup

Ensure that docker is running on your local machine.
Ensure that Docker is running on your local machine.

1. Clone this repository to your local machine:

Expand All @@ -42,11 +43,11 @@ Ensure that docker is running on your local machine.
make up
```

The application will be accessible at `http://localhost:8000`.
The application will be accessible at `http://localhost:8080`.

### Documentation

The API is documented using Swagger. Ensure that your refactoring maintains or improves the clarity of the API documentation. The Swagger documentation can be accessed at `http://localhost:8000/api/documentation`.
The API is documented using Swagger. Ensure that your refactoring maintains or improves the clarity of the API documentation. The Swagger documentation can be accessed at `http://localhost:8080/api/documentation`.

### Useful Makefile Targets

Expand Down Expand Up @@ -93,8 +94,7 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
---

### Commit Message Format
Each commit message consists of a **header**, a **body** and a **footer**. The header has a special
format that includes a **type** and a **subject**:
Each commit message consists of a **header**, a **body** and a **footer**. The header has a special format that includes a **type** and a **subject**:

```
<type>: <subject>
Expand All @@ -106,8 +106,7 @@ format that includes a **type** and a **subject**:

The **header** is mandatory.

Any line of the commit message cannot be longer 100 characters! This allows the message to be easier
to read on GitHub as well as in various git tools.
Any line of the commit message cannot be longer than 100 characters! This allows the message to be easier to read on GitHub as well as in various git tools.

The footer should contain a [closing reference to an issue](https://help.github.com/articles/closing-issues-via-commit-messages/) if any.

Expand All @@ -116,6 +115,7 @@ Samples: (even more [samples](https://github.com/angular/angular/commits/main))
```
docs(changelog): update changelog to beta.5
```

```
fix(release): need to depend on latest rxjs and zone.js

Expand All @@ -138,7 +138,6 @@ Must be one of the following:
* **style**: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
* **test**: Adding missing tests or correcting existing tests


### Subject
The subject contains a succinct description of the change:

Expand All @@ -151,12 +150,11 @@ Just as in the **subject**, use the imperative, present tense: "change" not "cha
The body should include the motivation for the change and contrast this with previous behavior.

### Footer
The footer should contain any information about **Breaking Changes** and is also the place to
reference GitHub issues that this commit **Closes**.
The footer should contain any information about **Breaking Changes** and is also the place to reference GitHub issues that this commit **Closes**.

**Breaking Changes** should start with the word `BREAKING CHANGE:` with a space or two newlines. The rest of the commit message is then used for this.

A detailed explanation can be found in this [document][commit-message-format].

[commit-message-format]: https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#

```
111 changes: 77 additions & 34 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

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\JsonResponse;

class UserController extends Controller
{
private User $user;

function __construct(User $user)
public function __construct(User $user)
{
$this->user = $user;
}

/**
* Display a listing of the resource.
*
* @return Response
* @return JsonResponse
*
* @OA\Get(
* path="/users",
Expand Down Expand Up @@ -48,15 +50,17 @@ function __construct(User $user)
* )
* )
*/
public function index(Request $request)
public function index(): JsonResponse
{
return $this->user->get();
$users = $this->user->all();
return response()->json($users, 200);
}

/**
* Show a specific user resource
* Show a specific user resource.
*
* @return User
* @param int $id
* @return JsonResponse
*
* @OA\Get(
* path="/users/{id}",
Expand Down Expand Up @@ -85,18 +89,29 @@ public function index(Request $request)
* @OA\Response(
* response=403,
* description="Forbidden"
* ),
* @OA\Response(
* response=404,
* description="Not Found"
* )
* )
*/
public function show(User $user)
public function show(int $id): JsonResponse
{
return $user;
$user = $this->user->find($id);

if (!$user) {
return response()->json(['error' => 'User not found'], 404);
}

return response()->json($user, 200);
}

/**
* Store a newly created user in storage.
*
* @return User
* @param StoreUserRequest $request
* @return JsonResponse
*
* @OA\Post(
* path="/users",
Expand All @@ -112,11 +127,15 @@ public function show(User $user)
* @OA\JsonContent(ref="#/components/schemas/User")
* ),
* @OA\Response(
* response=200,
* response=201,
* description="Successful operation",
* @OA\JsonContent(ref="#/components/schemas/User")
* ),
* @OA\Response(
* response=400,
* description="Bad Request"
* ),
* @OA\Response(
* response=401,
* description="Unauthenticated",
* ),
Expand All @@ -126,21 +145,22 @@ public function show(User $user)
* )
* )
*/
public function store(Request $request)
public function store(StoreUserRequest $request): JsonResponse
{
$data = $request->only([
'name',
'email',
'password',
]);
$data = $request->validated();
$data['password'] = bcrypt($data['password']);

$user = $this->user->create($data);

return $this->user->create($data);
return response()->json($user, 201);
}

/**
* Update a specific user resource
* Update a specific user resource.
*
* @return User
* @param UpdateUserRequest $request
* @param int $id
* @return JsonResponse
*
* @OA\Put(
* path="/users/{id}",
Expand All @@ -167,32 +187,47 @@ public function store(Request $request)
* @OA\JsonContent(ref="#/components/schemas/User")
* ),
* @OA\Response(
* response=400,
* description="Bad Request"
* ),
* @OA\Response(
* response=401,
* description="Unauthenticated",
* ),
* @OA\Response(
* response=403,
* description="Forbidden"
* ),
* @OA\Response(
* response=404,
* description="Not Found"
* )
* )
*/
public function update(Request $request, User $user)
public function update(UpdateUserRequest $request, int $id): JsonResponse
{
$data = $request->only([
'name',
'email',
'password',
]);
$user = $this->user->find($id);

if (!$user) {
return response()->json(['error' => 'User not found'], 404);
}

$data = $request->validated();

if (isset($data['password'])) {
$data['password'] = bcrypt($data['password']);
}

$user->update($data);

return $user;
return response()->json($user, 200);
}

/**
* Remove a specific user resource
* Remove a specific user resource.
*
* @return User
* @param int $id
* @return JsonResponse
*
* @OA\Delete(
* path="/users/{id}",
Expand All @@ -210,9 +245,8 @@ public function update(Request $request, User $user)
* in="path",
* ),
* @OA\Response(
* response=200,
* response=204,
* description="Successful operation",
* @OA\JsonContent(ref="#/components/schemas/User")
* ),
* @OA\Response(
* response=401,
Expand All @@ -221,14 +255,23 @@ public function update(Request $request, User $user)
* @OA\Response(
* response=403,
* description="Forbidden"
* ),
* @OA\Response(
* response=404,
* description="Not Found"
* )
* )
*/
public function destroy(User $user)
public function destroy(int $id): JsonResponse
{
$user = $this->user->find($id);

if (!$user) {
return response()->json(['error' => 'User not found'], 404);
}

$user->delete();

return $user;
return response()->json(null, 204);
}
}

30 changes: 30 additions & 0 deletions app/Http/Requests/StoreUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8',
];
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/UpdateUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$id = $this->route('id');

return [
'name' => 'sometimes|required|string|max:255',
'email' => 'sometimes|required|string|email|max:255|unique:users,email,' . $id,
'password' => 'sometimes|required|string|min:8',
];
}
}
Loading