From 0825c098e5d2d2cc7def83cbdab643c0a156c2bc Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 18 Nov 2024 02:54:45 +0000 Subject: [PATCH] Updated the episode repository, look for any other instances of the insert method from the database handler. --- ...deRespository.php => EpisodeRepository.php} | 0 .../Domain/Repository/User/UserRepository.php | 18 ++++++++++++------ .../Api/Database/DatabaseHandlerInterface.php | 2 +- .../Database/DatabaseHandler.php | 2 +- .../Domain/Repository/UserRepositoryTest.php | 8 ++------ 5 files changed, 16 insertions(+), 14 deletions(-) rename app/src/Domain/Repository/Episode/{EpisodeRespository.php => EpisodeRepository.php} (100%) diff --git a/app/src/Domain/Repository/Episode/EpisodeRespository.php b/app/src/Domain/Repository/Episode/EpisodeRepository.php similarity index 100% rename from app/src/Domain/Repository/Episode/EpisodeRespository.php rename to app/src/Domain/Repository/Episode/EpisodeRepository.php diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index 2e90a03..6c26412 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -14,6 +14,12 @@ class UserRepository implements UserRepositoryInterface const CREATE_QUERY = "INSERT INTO users (email, password, name) VALUES (:email, :password, :name)"; + const UPDATE_QUERY = "UPDATE users + SET email = :email, + name = :name, + password = :password + WHERE id = :id"; + public function __construct( private DatabaseHandlerInterface $db, private UserAuthorizationInterface $userAuth @@ -70,7 +76,7 @@ class UserRepository implements UserRepositoryInterface public function create(User $user): bool { - $results = $this->db->insert(self::CREATE_QUERY, [ + $results = $this->db->execute(self::CREATE_QUERY, [ ':email' => $user->getEmail(), ':password' => $this->userAuth->hash($user->getPassword()), ':name' => $user->getName(), @@ -94,11 +100,11 @@ class UserRepository implements UserRepositoryInterface password = :password WHERE id = :id"; - $results = (bool) $this->db->query($query, [ - $user->getEmail(), - $user->getName(), - $this->userAuth->hash($user->getPassword()), - $user->getId() + $results = $this->db->execute(self::UPDATE_QUERY, [ + ':email' => $user->getEmail(), + ':name' => $user->getName(), + ':password' => $this->userAuth->hash($user->getPassword()), + ':id' => $user->getId() ]); if ($results == true) { diff --git a/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php index 4a87162..d14bfeb 100644 --- a/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php +++ b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php @@ -29,5 +29,5 @@ interface DatabaseHandlerInterface * @param array $params Either a list of params, or the KV of params * @return bool True if successful */ - public function insert(string $query, array $params = []): bool; + public function execute(string $query, array $params = []): bool; } diff --git a/app/src/Infrastructure/Database/DatabaseHandler.php b/app/src/Infrastructure/Database/DatabaseHandler.php index 8f3f9cc..7e0b5c5 100644 --- a/app/src/Infrastructure/Database/DatabaseHandler.php +++ b/app/src/Infrastructure/Database/DatabaseHandler.php @@ -49,7 +49,7 @@ class DatabaseHandler implements DatabaseHandlerInterface return $statement->fetchAll($params); } - public function insert(string $query, array $params = []): bool + public function execute(string $query, array $params = []): bool { $statement = $this->database->prepare($query); return $statement->execute($params); diff --git a/app/tests/Domain/Repository/UserRepositoryTest.php b/app/tests/Domain/Repository/UserRepositoryTest.php index 6a69703..ee27165 100644 --- a/app/tests/Domain/Repository/UserRepositoryTest.php +++ b/app/tests/Domain/Repository/UserRepositoryTest.php @@ -17,8 +17,8 @@ class UserRepositoryTest extends TestCase $databaseHandler = $this->prophesize(DatabaseHandlerInterface::class); $databaseHandler - ->insert(Argument::type('string'), Argument::type('array')) - ->willReturn(true); + ->execute(Argument::type('string'), Argument::type('array')) + ->willReturn(true, true); $databaseHandler ->getConnection() @@ -29,10 +29,6 @@ class UserRepositoryTest extends TestCase } }); - $databaseHandler - ->query(Argument::type('string'), Argument::type('array')) - ->willReturn([true]); - $userRepository = new UserRepository( $databaseHandler->reveal(), new BasicUserAuthorization()