Skip to content

Commit

Permalink
[39] - Añadir endpoint create users a main
Browse files Browse the repository at this point in the history
  • Loading branch information
eduezponda committed Jun 2, 2024
1 parent df3eed1 commit c24ee1d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
42 changes: 42 additions & 0 deletions app/Infrastructure/CreateUser/AnalyticsCreateUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Infrastructure\CreateUser;

use App\Exceptions\ConflictException;
use App\Exceptions\InternalServerErrorException;
use App\Infrastructure\Controllers\Controller;
use App\Services\CreateUserManager;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AnalyticsCreateUserController extends Controller
{
private CreateUserManager $createUserManager;

public function __construct(CreateUserManager $createUserManager)
{
$this->createUserManager = $createUserManager;
}
public function __invoke(Request $request): JsonResponse
{
$username = $request->input('username');
$password = $request->input('password');

if (empty($username) || empty($password)) {
return response()->json(
['error' => 'Los parámetros (username y password) no fueron proporcionados.'],
Response::HTTP_BAD_REQUEST
);
}

try {
$createUserMessage = $this->createUserManager->getCreateUserMessage($username, $password);
return response()->json($createUserMessage, Response::HTTP_CREATED);
} catch (ConflictException $e) {
return response()->json(['error' => $e->getMessage()], Response::HTTP_CONFLICT);
} catch (InternalServerErrorException $e) {
return response()->json(['error' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}
6 changes: 4 additions & 2 deletions app/Services/CreateUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
class CreateUserManager
{
private DBClient $dbClient;
private const CREATE_USER_MESSAGE = 'Usuario creado correctamente';
private const CONFLICT_ERROR_MESSAGE = 'El nombre de usuario ya está en uso.';

public function __construct(DBClient $dbClient)
{
Expand All @@ -23,14 +25,14 @@ public function getCreateUserMessage($username, $password): array
$userExists = $this->dbClient->checkIfUsernameExists($username);

if ($userExists) {
throw new ConflictException("El nombre de usuario ya está en uso.");
throw new ConflictException(self::CONFLICT_ERROR_MESSAGE);
}

$this->dbClient->createUser($username, $password);

return [
"username" => $username,
"message" => "Usuario creado correctamente"
"message" => self::CREATE_USER_MESSAGE
];
}
}
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
Route::post('/analytics/follow', AnalyticsFollowStreamerController::class);
Route::get('/analytics/follow', AnalyticsFollowStreamerController::class);
Route::delete('/analytics/unfollow', AnalyticsUnfollowController::class);
Route::get('/analytics/users', AnalyticsCreateUserController::class);
Route::post('/analytics/users', AnalyticsCreateUserController::class);
2 changes: 0 additions & 2 deletions tests/Unit/Services/CreateUserManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class CreateUserManagerTest extends TestCase
private const INTERNAL_SERVER_ERROR_MESSAGE = 'Error del servidor al crear el usuario.';
private const CONFLICT_ERROR_MESSAGE = 'El nombre de usuario ya está en uso.';
private const CREATE_USER_MESSAGE = 'Usuario creado correctamente';
private const BAD_REQUEST_ERROR_MESSAGE = 'Los parámetros (' . self::USERNAME . ' y ' . self::PASSWORD .
') no fueron proporcionados.';

protected function setUp(): void
{
Expand Down

0 comments on commit c24ee1d

Please sign in to comment.