From d99d893394ff34d052c498d3a570845d9bb26925 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Sun, 17 May 2026 21:50:47 +0300 Subject: [PATCH] add postgres user repository --- backend/app/User/PostgresUserRepository.php | 53 +++++++++++++++++++++ backend/config/container.php | 6 ++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 backend/app/User/PostgresUserRepository.php diff --git a/backend/app/User/PostgresUserRepository.php b/backend/app/User/PostgresUserRepository.php new file mode 100644 index 0000000..e0989ee --- /dev/null +++ b/backend/app/User/PostgresUserRepository.php @@ -0,0 +1,53 @@ + $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, + ); + } +} diff --git a/backend/config/container.php b/backend/config/container.php index d01341b..8bdab3f 100644 --- a/backend/config/container.php +++ b/backend/config/container.php @@ -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(),