-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user): add UserController for managing user operations (#73)
Introduce the `UserController` to handle the following user-related functionalities: - Retrieve all users - Display details of a specific user - Create a new user - Update existing user information - Delete a user --- **Needs**: - #74 **Closes**: - Close #10 - Close #11 - Close #12 - Close #13 - Close #14 --------- Signed-off-by: Valentin Sickert <[email protected]>
- Loading branch information
Showing
4 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Responses\ApiSuccessResponse; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class UserController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
*/ | ||
public function index() | ||
{ | ||
$users = User::paginate( | ||
request()->has('size') ? request()->size : 15 | ||
); | ||
|
||
return new ApiSuccessResponse($users); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
$validated = $request->validate([ | ||
'name' => 'required|string|max:255', | ||
'email' => 'required|email|unique:users,email', | ||
'password' => 'required|string|min:8|confirmed', | ||
]); | ||
|
||
$user = User::create($validated); | ||
|
||
return new ApiSuccessResponse($user, Response::HTTP_CREATED); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show(User $user) | ||
{ | ||
return new ApiSuccessResponse($user); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(Request $request, User $user) | ||
{ | ||
$validated = $request->validate([ | ||
'name' => 'required|string', | ||
'email' => 'required|email|unique:users,email', | ||
'password' => 'sometimes|required|min:8|confirmed', | ||
]); | ||
|
||
$user->update($validated); | ||
|
||
return new ApiSuccessResponse($user); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy(User $user) | ||
{ | ||
$user->delete(); | ||
|
||
return new ApiSuccessResponse("User deleted successfully."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
use App\Http\Controllers\UserController; | ||
use Illuminate\Support\Facades\Route; | ||
|
||
Route::group(['middleware' => 'auth:sanctum'], function () { | ||
Route::get('/users', [UserController::class, 'index'])->name('api.v1.users.index'); | ||
Route::post('/users', [UserController::class, 'store'])->name('api.v1.users.store'); | ||
Route::get('/users/{user}', [UserController::class, 'show'])->name('api.v1.users.show'); | ||
Route::put('/users/{user}', [UserController::class, 'update'])->name('api.v1.users.update'); | ||
Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('api.v1.users.destroy'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Http\Controllers; | ||
|
||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Laravel\Sanctum\Sanctum; | ||
use Tests\TestCase; | ||
|
||
class UserControllerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** | ||
* Test the index method. | ||
*/ | ||
public function test_users_index(): void | ||
{ | ||
// Create some dummy users | ||
User::factory()->count(10)->create(); | ||
|
||
Sanctum::actingAs( | ||
User::factory()->create() | ||
); | ||
|
||
// Send a GET request to the index endpoint | ||
$response = $this->get('/api/v1/users'); | ||
|
||
// Assert that the response has a successful status code | ||
$response->assertStatus(200); | ||
|
||
// Assert that the response contains the paginated users | ||
$response->assertJsonFragment(User::paginate(15)->toArray()); | ||
} | ||
|
||
/** | ||
* Test the store method. | ||
*/ | ||
public function test_create_user(): void | ||
{ | ||
Sanctum::actingAs( | ||
User::factory()->create() | ||
); | ||
|
||
$userData = [ | ||
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
'password' => 'password', | ||
'password_confirmation' => 'password', | ||
]; | ||
|
||
// Send a POST request to the store endpoint | ||
$response = $this->post('/api/v1/users', $userData); | ||
|
||
// Assert that the response has a successful status code | ||
$response->assertStatus(201); | ||
|
||
// Assert that the database has the user | ||
$this->assertDatabaseHas('users', [ | ||
'name' => $userData['name'], | ||
'email' => $userData['email'], | ||
]); | ||
|
||
// Assert that the response contains the user data | ||
$response->assertJsonFragment([ | ||
'name' => $userData['name'], | ||
'email' => $userData['email'], | ||
]); | ||
} | ||
|
||
/** | ||
* Test the show method. | ||
*/ | ||
public function test_users_show(): void | ||
{ | ||
Sanctum::actingAs( | ||
User::factory()->create() | ||
); | ||
|
||
$user = User::factory()->create(); | ||
|
||
// Send a GET request to the show endpoint | ||
$response = $this->get('/api/v1/users/' . $user->id); | ||
|
||
// Assert that the response has a successful status code | ||
$response->assertStatus(200); | ||
|
||
// Assert that the response contains the user data | ||
$response->assertJsonFragment($user->toArray()); | ||
} | ||
|
||
/** | ||
* Test the update method. | ||
*/ | ||
public function test_users_update(): void | ||
{ | ||
Sanctum::actingAs( | ||
User::factory()->create() | ||
); | ||
|
||
$user = User::factory()->create(); | ||
|
||
$userData = [ | ||
'name' => 'Updated Name', | ||
'email' => '[email protected]', | ||
]; | ||
|
||
// Send a PATCH request to the update endpoint | ||
$response = $this->put('/api/v1/users/' . $user->id, $userData); | ||
|
||
// Assert that the response has a successful status code | ||
$response->assertStatus(200); | ||
|
||
$this->assertDatabaseHas('users', [ | ||
'name' => $userData['name'], | ||
'email' => $userData['email'], | ||
]); | ||
|
||
// Assert that the response contains the updated user data | ||
$response->assertJsonFragment($userData); | ||
} | ||
|
||
/** | ||
* Test the destroy method. | ||
*/ | ||
public function test_users_destroy(): void | ||
{ | ||
Sanctum::actingAs( | ||
User::factory()->create() | ||
); | ||
|
||
$user = User::factory()->create(); | ||
|
||
// Send a DELETE request to the destroy endpoint | ||
$response = $this->delete('/api/v1/users/' . $user->id); | ||
|
||
// Assert that the response has a successful status code | ||
$response->assertStatus(200); | ||
|
||
// Assert that the response contains the success message | ||
$response->assertJsonFragment(['data' => 'User deleted successfully.']); | ||
} | ||
} |