Flesh out some of the User repository methods.

This commit is contained in:
Dave Smith-Hayes 2024-05-23 21:33:33 -04:00
parent b908dc6ea3
commit e2a3c95f99

View File

@ -3,6 +3,7 @@
namespace Slovocast\Domain\Repository; namespace Slovocast\Domain\Repository;
use Slovocast\Infrastructure\DatabaseConnectionInterface; use Slovocast\Infrastructure\DatabaseConnectionInterface;
use Slovocast\Infrastructure\User\UserAuthorizationInterface;
use Slovocast\Exception\EntityNotFoundException; use Slovocast\Exception\EntityNotFoundException;
use Slovocast\Domain\Entity\User; use Slovocast\Domain\Entity\User;
use PDO; use PDO;
@ -10,7 +11,8 @@ use PDO;
class UserRepository implements UserRepositoryInterface class UserRepository implements UserRepositoryInterface
{ {
public function __construct( public function __construct(
private DatabaseConnectionInterface $database private DatabaseConnectionInterface $database,
private UserAuthorizationInterface $userAuth
) {} ) {}
/** /**
@ -56,11 +58,10 @@ class UserRepository implements UserRepositoryInterface
public function get(int $id): User public function get(int $id): User
{ {
$query = "SELECT * FROM users WHERE id = :id LIMIT 1"; $query = "SELECT * FROM users WHERE id = :id LIMIT 1";
$connection = $this->database->getConnection();
$userData = $this->queryForUser( $userData = $this->queryForUser(
$query, $query,
[ 'id' => $id ], [ 'id' => $id ],
$connection $this->database->getConnection()
); );
return $this->userFromQueryResults($userData); return $this->userFromQueryResults($userData);
} }
@ -68,12 +69,30 @@ class UserRepository implements UserRepositoryInterface
public function getFromEmail(string $email): User public function getFromEmail(string $email): User
{ {
$query = "SELECT * FROM users WHERE email = :email LIMIT 1"; $query = "SELECT * FROM users WHERE email = :email LIMIT 1";
$connection = $this->database->getConnection();
$userData = $this->queryForUser( $userData = $this->queryForUser(
$query, $query,
[ 'email' => $email ], [ 'email' => $email ],
$connection $this->database->getConnection()
); );
return $this->userFromQueryResults($userData); return $this->userFromQueryResults($userData);
} }
public function save(User $user): bool
{
$connection = $this->database->getConnection();
$query = "INSERT INTO users (email, password, name)
VALUES (:email, :password, :name)";
$statement = $connection->prepare($query);
return $statement->execute([
'email' => $user->getEmail(),
'name'=> $user->getName(),
'password' => $this->userAuth->hash($user->getPassword())
]);
}
public function update(User $user): bool
{
}
} }