Skip to content

Commit a6d31ca

Browse files
author
Guilherme
committed
feat: adding database and endpoint insert
1 parent b52d17c commit a6d31ca

File tree

13 files changed

+90
-33
lines changed

13 files changed

+90
-33
lines changed

Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
up:
2-
./vendor/bin/sail up --build
2+
./vendor/bin/sail up -d
33
down:
44
./vendor/bin/sail down
55

@@ -8,9 +8,11 @@ ssh:
88

99
optimize:
1010
docker-compose exec laravel.test php artisan optimize
11+
docker-compose exec laravel.test php artisan config:cache
12+
docker-compose exec laravel.test php artisan config:clear
1113

1214
refresh:
13-
docker-compose exec laravel.test php artisan migrate:fresh --seed
15+
docker-compose exec laravel.test php artisan migrate
1416

1517
test:
1618
docker-compose exec laravel.test php artisan test

app/Application/User/Create/CreateUserCommand.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
class CreateUserCommand
88
{
99
public function __construct(
10-
public readonly string $name
10+
public readonly string $name,
11+
public readonly string $email,
12+
public readonly string $password
1113
) {
1214
}
1315
}

app/Application/User/Create/CreateUserCommandHandler.php

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public function __construct(
1717
public function handle(CreateUserCommand $command): void
1818
{
1919
$user = User::create($command->name);
20-
21-
dd($user);
2220
$this->users->create($user);
2321
}
2422
}

app/Domain/User/User.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public function __construct(
1212
public readonly string $id,
1313
public readonly string $name,
1414
public readonly string $email,
15+
public readonly string $password,
1516
public readonly DateTimeImmutable $createdAt,
1617
) {
1718
}
@@ -20,16 +21,19 @@ public static function create(string $name): self
2021
{
2122
return new self(
2223
id: '',
23-
name: '',
24-
email: '',
24+
name: $name,
25+
26+
password: '123',
2527
createdAt: new DateTimeImmutable('2023-09-09 00:15:00'),
2628
);
2729
}
2830

2931
public function toArray(): array
3032
{
3133
return [
32-
'name' => $this->name
34+
'name' => $this->name,
35+
'email' => $this->email,
36+
'password' => $this->password,
3337
];
3438
}
3539

@@ -40,6 +44,7 @@ public static function fromArray(
4044
id: $data['id'],
4145
name: $data['name'],
4246
email: $data['email'],
47+
password: $data['password'],
4348
createdAt: $data['createdAt'],
4449
);
4550
}

app/Domain/User/Users.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ interface Users
88
{
99
public function get(string $id): User;
1010

11-
public function create(User $payment): void;
11+
public function create(User $user): void;
1212
}

app/Infrastructure/Database/EloquentUsers.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public function get(string $id): User
3333

3434
public function create(User $user): void
3535
{
36-
$this->model->save($user->toArray());
36+
$this->model->create($user->toArray());
3737
}
3838
}

app/Infrastructure/Database/Models/UserModel.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@
66

77
use Illuminate\Database\Eloquent\Factories\HasFactory;
88
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Notifications\Notifiable;
10+
use Laravel\Sanctum\HasApiTokens;
911

1012
class UserModel extends Model
1113
{
12-
use HasFactory;
14+
use HasApiTokens, HasFactory, Notifiable;
15+
16+
protected $table = 'users';
1317

1418
protected $fillable = [
15-
'id',
1619
'name',
1720
'email',
21+
'password',
22+
];
23+
24+
protected $hidden = [
25+
'password',
26+
'remember_token',
27+
];
28+
29+
protected $casts = [
30+
'email_verified_at' => 'datetime',
31+
'password' => 'hashed',
1832
];
1933
}

app/Presenter/Console/Kernel.php

-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,5 @@ protected function schedule(Schedule $schedule): void
2323
protected function commands(): void
2424
{
2525
$this->load(__DIR__ . '/Commands');
26-
27-
require base_path('routes/console.php');
2826
}
2927
}

app/Presenter/Http/Routes/v1/user.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
use App\Presenter\Http\User\CreateUserController;
5+
use App\Presenter\Http\User\Create\CreateUserController;
66
use Illuminate\Support\Facades\Route;
77

88
Route::prefix('/user')->group(function () {
9-
Route::get('', CreateUserController::class);
9+
Route::post('', CreateUserController::class);
1010
});

app/Presenter/Http/User/CreateUserController.php renamed to app/Presenter/Http/User/Create/CreateUserController.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Presenter\Http\User;
5+
namespace App\Presenter\Http\User\Create;
66

77
use App\Application\User\Create\CreateUserCommand;
88
use App\Application\User\Create\CreateUserCommandHandler;
@@ -15,10 +15,10 @@ public function __construct(
1515
) {
1616
}
1717

18-
public function __invoke(): Response
19-
{
18+
public function __invoke(CreateUserRequest $request): Response
19+
{
20+
dd($request->toCommand());
2021
$this->createHandler->handle(new CreateUserCommand("Name"));
21-
22-
return new Response("aaaaa", Response::HTTP_CREATED);
22+
return new Response(null, Response::HTTP_CREATED);
2323
}
2424
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Presenter\Http\User\Create;
4+
5+
use App\Application\User\Create\CreateUserCommand;
6+
use Illuminate\Foundation\Http\FormRequest;
7+
8+
class CreateUserRequest extends FormRequest
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*/
13+
public function authorize(): bool
14+
{
15+
return true;
16+
}
17+
18+
/**
19+
* Get the validation rules that apply to the request.
20+
*
21+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
22+
*/
23+
public function rules(): array
24+
{
25+
return [
26+
'name' => 'required',
27+
'email' => 'required',
28+
'password' => 'required',
29+
];
30+
}
31+
32+
public function toCommand(): CreateUserCommand
33+
{
34+
return new CreateUserCommand(
35+
...$this->safe()->all()
36+
);
37+
}
38+
}

app/Presenter/Providers/BroadcastServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public function boot(): void
1616
{
1717
Broadcast::routes();
1818

19-
require base_path('routes/channels.php');
19+
require base_path('config/channels.php');
2020
}
2121
}

composer.lock

+11-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)