-
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.
Added a new system API to create a user
- Loading branch information
1 parent
c558140
commit f4adb54
Showing
16 changed files
with
360 additions
and
66 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/EconumoBundle/Application/System/Assembler/CreateUserV1ResultAssembler.php
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EconumoBundle\Application\System\Assembler; | ||
|
||
use App\EconumoBundle\Application\System\Dto\CreateUserV1ResultDto; | ||
use App\EconumoBundle\Domain\Entity\User; | ||
|
||
readonly class CreateUserV1ResultAssembler | ||
{ | ||
public function assemble(User $user): CreateUserV1ResultDto | ||
{ | ||
$result = new CreateUserV1ResultDto(); | ||
$result->id = $user->getId()->getValue(); | ||
|
||
return $result; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/EconumoBundle/Application/System/Dto/CreateUserV1RequestDto.php
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EconumoBundle\Application\System\Dto; | ||
|
||
use OpenApi\Annotations as OA; | ||
|
||
/** | ||
* @OA\Schema( | ||
* required={"email", "name", "password"} | ||
* ) | ||
*/ | ||
class CreateUserV1RequestDto | ||
{ | ||
/** | ||
* @OA\Property(example="[email protected]") | ||
*/ | ||
public string $email; | ||
|
||
/** | ||
* @OA\Property(example="John") | ||
*/ | ||
public string $name; | ||
|
||
/** | ||
* @OA\Property(example="qwerty123") | ||
*/ | ||
public string $password; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/EconumoBundle/Application/System/Dto/CreateUserV1ResultDto.php
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EconumoBundle\Application\System\Dto; | ||
|
||
use OpenApi\Annotations as OA; | ||
|
||
/** | ||
* @OA\Schema( | ||
* required={"id"} | ||
* ) | ||
*/ | ||
class CreateUserV1ResultDto | ||
{ | ||
/** | ||
* @OA\Property(example="2acbfdcf-ff94-4c07-9819-7d3f1b8d20ce") | ||
*/ | ||
public string $id; | ||
} |
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EconumoBundle\Application\System; | ||
|
||
use App\EconumoBundle\Application\Exception\ValidationException; | ||
use App\EconumoBundle\Application\System\Dto\CreateUserV1RequestDto; | ||
use App\EconumoBundle\Application\System\Dto\CreateUserV1ResultDto; | ||
use App\EconumoBundle\Application\System\Assembler\CreateUserV1ResultAssembler; | ||
use App\EconumoBundle\Domain\Entity\ValueObject\Email; | ||
use App\EconumoBundle\Domain\Exception\UserRegisteredException; | ||
use App\EconumoBundle\Domain\Service\EmailServiceInterface; | ||
use App\EconumoBundle\Domain\Service\Translation\TranslationServiceInterface; | ||
use App\EconumoBundle\Domain\Service\UserServiceInterface; | ||
|
||
readonly class UserService | ||
{ | ||
public function __construct( | ||
private CreateUserV1ResultAssembler $createUserV1ResultAssembler, | ||
private UserServiceInterface $userService, | ||
private EmailServiceInterface $emailService, | ||
private TranslationServiceInterface $translationService, | ||
) { | ||
} | ||
|
||
public function createUser( | ||
string $baseUrl, | ||
CreateUserV1RequestDto $dto | ||
): CreateUserV1ResultDto { | ||
$email = new Email($dto->email); | ||
$password = $dto->password; | ||
$name = $dto->name; | ||
try { | ||
$user = $this->userService->register($email, $password, $name); | ||
$this->emailService->sendWelcomeEmailWithPassword($email, $password, $baseUrl); | ||
} catch (UserRegisteredException $userRegisteredException) { | ||
throw new ValidationException( | ||
$this->translationService->trans('user.user.already_exists'), | ||
400, | ||
$userRegisteredException | ||
); | ||
} | ||
|
||
return $this->createUserV1ResultAssembler->assemble($user); | ||
} | ||
} |
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
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
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
66 changes: 66 additions & 0 deletions
66
src/EconumoBundle/UI/Controller/Api/System/User/CreateUserV1Controller.php
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EconumoBundle\UI\Controller\Api\System\User; | ||
|
||
use App\EconumoBundle\Application\System\UserService; | ||
use App\EconumoBundle\Application\System\Dto\CreateUserV1RequestDto; | ||
use App\EconumoBundle\UI\Controller\Api\System\User\Validation\CreateUserV1Form; | ||
use App\EconumoBundle\Application\Exception\ValidationException; | ||
use App\EconumoBundle\UI\Middleware\ProtectSystemApi\SystemApiInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use App\EconumoBundle\UI\Service\Validator\ValidatorInterface; | ||
use App\EconumoBundle\UI\Service\Response\ResponseFactory; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Nelmio\ApiDocBundle\Annotation\Model; | ||
use OpenApi\Annotations as OA; | ||
|
||
class CreateUserV1Controller extends AbstractController implements SystemApiInterface | ||
{ | ||
public function __construct(private readonly UserService $userService, private readonly ValidatorInterface $validator) | ||
{ | ||
} | ||
|
||
/** | ||
* Create user | ||
* | ||
* @OA\Tag(name="System"), | ||
* @OA\RequestBody(@OA\JsonContent(ref=@Model(type=\App\EconumoBundle\Application\System\Dto\CreateUserV1RequestDto::class))), | ||
* @OA\Response( | ||
* response=200, | ||
* description="OK", | ||
* @OA\JsonContent( | ||
* type="object", | ||
* allOf={ | ||
* @OA\Schema(ref="#/components/schemas/JsonResponseOk"), | ||
* @OA\Schema( | ||
* @OA\Property( | ||
* property="data", | ||
* ref=@Model(type=\App\EconumoBundle\Application\System\Dto\CreateUserV1ResultDto::class) | ||
* ) | ||
* ) | ||
* } | ||
* ) | ||
* ), | ||
* @OA\Response(response=400, description="Bad Request", @OA\JsonContent(ref="#/components/schemas/JsonResponseError")), | ||
* @OA\Response(response=401, description="Unauthorized", @OA\JsonContent(ref="#/components/schemas/JsonResponseUnauthorized")), | ||
* @OA\Response(response=500, description="Internal Server Error", @OA\JsonContent(ref="#/components/schemas/JsonResponseException")), | ||
* | ||
* | ||
* @return Response | ||
* @throws ValidationException | ||
*/ | ||
#[Route(path: '/api/v1/system/create-user', methods: ['POST'])] | ||
public function __invoke(Request $request): Response | ||
{ | ||
$dto = new CreateUserV1RequestDto(); | ||
$this->validator->validate(CreateUserV1Form::class, $request->request->all(), $dto); | ||
$baseUrl = $request->getSchemeAndHttpHost(); | ||
$result = $this->userService->createUser($baseUrl, $dto); | ||
|
||
return ResponseFactory::createOkResponse($request, $result); | ||
} | ||
} |
Oops, something went wrong.