add postgres user repository
This commit is contained in:
parent
6fbc1fb4f5
commit
d99d893394
2 changed files with 58 additions and 1 deletions
53
backend/app/User/PostgresUserRepository.php
Normal file
53
backend/app/User/PostgresUserRepository.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
use App\Database\UserModel;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
|
||||
class PostgresUserRepository implements UserRepository
|
||||
{
|
||||
public function create(CreateUserDto $dto): User
|
||||
{
|
||||
$record = UserModel::create([
|
||||
'email' => $dto->email->value(),
|
||||
'password_hash' => $dto->passwordHash,
|
||||
]);
|
||||
|
||||
return new User(
|
||||
id: $record->id,
|
||||
email: new EmailAddress($record->email),
|
||||
passwordHash: $record->password_hash,
|
||||
);
|
||||
}
|
||||
|
||||
public function findByEmail(EmailAddress $email): ?User
|
||||
{
|
||||
$record = UserModel::where('email', $email->value())->first();
|
||||
|
||||
if ($record === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new User(
|
||||
id: $record->id,
|
||||
email: new EmailAddress($record->email),
|
||||
passwordHash: $record->password_hash,
|
||||
);
|
||||
}
|
||||
|
||||
public function find(int $id): ?User
|
||||
{
|
||||
$record = UserModel::find($id);
|
||||
|
||||
if ($record === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new User(
|
||||
id: $record->id,
|
||||
email: new EmailAddress($record->email),
|
||||
passwordHash: $record->password_hash,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -11,8 +11,9 @@ use App\Auth\UseCases\CreateSession\CreateSession;
|
|||
use App\Auth\UseCases\Logout\Logout;
|
||||
use App\Controllers\AuthController;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
use App\User\PostgresUserRepository;
|
||||
use App\User\UserRepository;
|
||||
use DI\ContainerBuilder;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
$builder = new ContainerBuilder();
|
||||
|
||||
|
|
@ -23,6 +24,9 @@ $builder->addDefinitions([
|
|||
TokenGenerator::class => DI\create(RandomTokenGenerator::class),
|
||||
Clock::class => DI\create(SystemClock::class),
|
||||
|
||||
// Repositories
|
||||
UserRepository::class => DI\create(PostgresUserRepository::class),
|
||||
|
||||
// Use cases
|
||||
AuthenticateUser::class => DI\autowire(),
|
||||
CreateSession::class => DI\autowire(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue