Skip to content

Commit c23ae42

Browse files
committed
ajustes
1 parent b0fd607 commit c23ae42

File tree

16 files changed

+272
-526
lines changed

16 files changed

+272
-526
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ yarn-error.log
1818
/.idea
1919
/.vscode
2020
/db-data-projetc-xxxx
21+
/logs
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\User;
4+
5+
use App\Http\Controllers\Controller;
6+
use Core\Generics\Outputs\OutputError;
7+
use Core\Modules\User\Create\CreateUserUseCase;
8+
use Core\Modules\User\Create\Inputs\CreateUserInput;
9+
use Core\Modules\User\Create\outputs\CreateUserOutput;
10+
use Illuminate\Http\Request;
11+
use Infra\Persistence\User\Command\CreateUserCommand;
12+
13+
class CreateUser extends Controller
14+
{
15+
public function __invoke(Request $request)
16+
{
17+
$input = new CreateUserInput(
18+
name: 'name 4',
19+
email: 'email 4',
20+
password: 'password',
21+
age: 17
22+
);
23+
24+
$useCase = new CreateUserUseCase(
25+
new CreateUserCommand()
26+
);
27+
$useCase->execute($input);
28+
$output = $useCase->getOutput();
29+
30+
/** @var CreateUserOutput $output */
31+
if ($output->status->statusCode === 201) {
32+
return response()->json(
33+
[
34+
'message' => $output->status->message,
35+
'data' => $output->userEntity->name
36+
],
37+
$output->status->statusCode
38+
);
39+
}
40+
41+
if ($output->status->statusCode === 500) {
42+
return response()->json(
43+
[
44+
'message' => $output->status->message,
45+
'data' => $output->status->message
46+
],
47+
$output->status->statusCode
48+
);
49+
}
50+
51+
/** @var OutputError $output */
52+
return response()->json(
53+
[
54+
'message' => $output->status->message,
55+
'data' => $output->message
56+
],
57+
$output->status->statusCode
58+
);
59+
}
60+
}

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"autoload": {
2323
"psr-4": {
2424
"App\\": "app/",
25+
"Core\\": "core/",
26+
"Infra\\": "infra/",
2527
"Database\\Factories\\": "database/factories/",
2628
"Database\\Seeders\\": "database/seeders/"
2729
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Core\Generics\Outputs;
4+
5+
interface GenericOutput
6+
{
7+
public function getMessages(): string;
8+
9+
public function getStatus(): OutputStatus;
10+
}

core/Generics/Outputs/OutputError.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Core\Generics\Outputs;
4+
5+
readonly class OutputError implements GenericOutput
6+
{
7+
8+
public function __construct(public OutputStatus $status, public string $message)
9+
{
10+
}
11+
12+
public function getMessages(): string
13+
{
14+
return $this->message;
15+
}
16+
17+
public function getStatus(): OutputStatus
18+
{
19+
return $this->status;
20+
}
21+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Core\Generics\Outputs;
4+
5+
class OutputStatus
6+
{
7+
public function __construct(
8+
public readonly int $statusCode,
9+
public readonly string $message
10+
) {
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Core\Modules\User\Commons\Entities;
4+
5+
use Core\Modules\User\Create\Exceptions\InvalidAgeException;
6+
7+
class UserEntity
8+
{
9+
/**
10+
* @throws InvalidAgeException
11+
*/
12+
public function __construct(
13+
public string $name,
14+
public string $email,
15+
public string $password,
16+
public int $age
17+
) {
18+
$this->validateAge();
19+
}
20+
21+
public function validateAge(): void
22+
{
23+
if ($this->age < 18) {
24+
throw new InvalidAgeException('Idade inválida');
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Core\Modules\User\Create;
4+
5+
use Core\Generics\Outputs\GenericOutput;
6+
use Core\Generics\Outputs\OutputError;
7+
use Core\Generics\Outputs\OutputStatus;
8+
use Core\Modules\User\Commons\Entities\UserEntity;
9+
use Core\Modules\User\Create\Exceptions\InvalidAgeException;
10+
use Core\Modules\User\Create\Gateways\CreateUserInterface;
11+
use Core\Modules\User\Create\Inputs\CreateUserInput;
12+
use Core\Modules\User\Create\outputs\CreateUserOutput;
13+
use Exception;
14+
15+
class CreateUserUseCase
16+
{
17+
private GenericOutput $output;
18+
19+
public function __construct(
20+
private CreateUserInterface $createUserInterface
21+
) {
22+
}
23+
24+
public function execute(CreateUserInput $createUserInput): void
25+
{
26+
try {
27+
$userEntity = new UserEntity(
28+
$createUserInput->name,
29+
$createUserInput->email,
30+
$createUserInput->password,
31+
$createUserInput->age
32+
);
33+
34+
$userEntity = $this->createUserInterface->create($userEntity);
35+
36+
$this->output = new CreateUserOutput(
37+
new OutputStatus(201, 'Ok'),
38+
$userEntity
39+
);
40+
} catch (InvalidAgeException $e) {
41+
$this->output = new OutputError(
42+
new OutputStatus(400, 'Bad Request'),
43+
$e->getMessage()
44+
);
45+
} catch (Exception $e) {
46+
$this->output = new OutputError(
47+
new OutputStatus(500, 'Internal Server Error'),
48+
$e->getMessage()
49+
);
50+
}
51+
}
52+
53+
public function getOutput(): GenericOutput
54+
{
55+
return $this->output;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Core\Modules\User\Create\Exceptions;
4+
5+
use Exception;
6+
7+
class InvalidAgeException extends Exception
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Core\Modules\User\Create\Gateways;
4+
5+
use Core\Modules\User\Commons\Entities\UserEntity;
6+
7+
interface CreateUserInterface
8+
{
9+
public function create(UserEntity $userEntity): UserEntity;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Core\Modules\User\Create\Inputs;
4+
5+
readonly class CreateUserInput
6+
{
7+
public function __construct(
8+
public string $name,
9+
public string $email,
10+
public string $password,
11+
public int $age
12+
) {
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Core\Modules\User\Create\outputs;
4+
5+
use Core\Generics\Outputs\GenericOutput;
6+
use Core\Generics\Outputs\OutputStatus;
7+
use Core\Modules\User\Commons\Entities\UserEntity;
8+
9+
readonly class CreateUserOutput implements GenericOutput
10+
{
11+
public function __construct(public OutputStatus $status, public UserEntity $userEntity)
12+
{
13+
}
14+
15+
public function getMessages(): string
16+
{
17+
return $this->status->message;
18+
}
19+
20+
public function getStatus(): OutputStatus
21+
{
22+
return $this->status;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Infra\Persistence\User\Command;
4+
5+
use App\Models\User;
6+
use Core\Modules\User\Commons\Entities\UserEntity;
7+
use Core\Modules\User\Create\Gateways\CreateUserInterface;
8+
9+
class CreateUserCommand implements CreateUserInterface
10+
{
11+
12+
public function create(UserEntity $userEntity): UserEntity
13+
{
14+
$userModel = new User();
15+
$userModel->name = $userEntity->name;
16+
$userModel->email = $userEntity->email;
17+
$userModel->password = $userEntity->password;
18+
$userModel->save();
19+
20+
return $userEntity;
21+
}
22+
}

logs/nginx/access.log

-12
This file was deleted.

0 commit comments

Comments
 (0)