From 1d00f2bda43aefd7219598142f4b1d261d12f9bc Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Fri, 8 Nov 2024 22:03:38 -0500 Subject: [PATCH] Start working on repository classes for the Image class --- app/src/Bootstrap.php | 4 +- app/src/Domain/Entity/Image.php | 65 +++++++++++++++++++ .../Repository/Channel/ChannelRepository.php | 6 +- .../Channel/ChannelRepositoryInterface.php | 2 +- .../Repository/Image/ImageRepository.php | 62 ++++++++++++++++++ .../Image/ImageRepositoryInterface.php | 15 +++++ .../Domain/Repository/User/UserRepository.php | 15 ++++- .../User/UserRepositoryInterface.php | 6 +- app/src/Domain/RepositoryInterface.php | 16 +++++ ...baseConnection.php => DatabaseHandler.php} | 2 +- 10 files changed, 183 insertions(+), 10 deletions(-) create mode 100644 app/src/Domain/Entity/Image.php create mode 100644 app/src/Domain/Repository/Image/ImageRepository.php create mode 100644 app/src/Domain/Repository/Image/ImageRepositoryInterface.php create mode 100644 app/src/Domain/RepositoryInterface.php rename app/src/Infrastructure/Database/{DatabaseConnection.php => DatabaseHandler.php} (93%) diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index c29c6db..eb01175 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -38,7 +38,7 @@ use Slovocast\Domain\Repository\User\UserRepository; use Slovocast\Domain\Repository\User\UserRepositoryInterface; use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; -use Slovocast\Infrastructure\Database\DatabaseConnection; +use Slovocast\Infrastructure\Database\DatabaseHandler; use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface; use Slovocast\Infrastructure\User\BasicUserAuthorization; @@ -143,7 +143,7 @@ class Bootstrap */ DatabaseHandlerInterface::class => function (ContainerInterface $container) { $config = $container->get('config'); - return new DatabaseConnection( + return new DatabaseHandler( $config->get('database.host'), $config->get('database.username'), $config->get('database.password'), diff --git a/app/src/Domain/Entity/Image.php b/app/src/Domain/Entity/Image.php new file mode 100644 index 0000000..ea07b20 --- /dev/null +++ b/app/src/Domain/Entity/Image.php @@ -0,0 +1,65 @@ +url; + } + + public function getTitle(): string + { + return $this->title; + } + + public function getWidth(): int + { + return $this->width; + } + + public function getHeight(): int + { + return $this->height; + } + + /** + * @param array $props Each property name as the key, and then the value + */ + public static function fromArray(array $props): Image + { + $image = new self( + $props['url'], + $props['title'], + $props['height'], + $props['width'], + + ); + + if ($props['id']) { + $image->setId($props['id']); + } + + if ($props['updatedAt']) { + $image->setUpdatedAt($props['updatedAt']); + } + + if ($props['createdAt']) { + $image->setCreatedAt($props['createdAt']); + } + + return $image; + } +} diff --git a/app/src/Domain/Repository/Channel/ChannelRepository.php b/app/src/Domain/Repository/Channel/ChannelRepository.php index 82b1f81..7837337 100644 --- a/app/src/Domain/Repository/Channel/ChannelRepository.php +++ b/app/src/Domain/Repository/Channel/ChannelRepository.php @@ -98,7 +98,7 @@ class ChannelRepository implements ChannelRepositoryInterface /** * @throws Throwable */ - public function create(Channel $channel, User $owner): int + public function create(Channel $channel, User $owner): Channel { $query = " INSERT INTO channels (name, slug, description, link, @@ -118,7 +118,9 @@ class ChannelRepository implements ChannelRepositoryInterface $owner->getId() ]); - return $this->db->getConnection()->lastInsertId(); + $insertId = $this->db->getConnection()->lastInsertId(); + $channel->setId($insertId); + return $channel; } /** diff --git a/app/src/Domain/Repository/Channel/ChannelRepositoryInterface.php b/app/src/Domain/Repository/Channel/ChannelRepositoryInterface.php index 3e4929d..b54bb79 100644 --- a/app/src/Domain/Repository/Channel/ChannelRepositoryInterface.php +++ b/app/src/Domain/Repository/Channel/ChannelRepositoryInterface.php @@ -10,7 +10,7 @@ interface ChannelRepositoryInterface public function get(int $id): Channel; public function getFromUser(User $user): Channel; public function getFromSlug(string $slug): Channel; - public function create(Channel $channel): int; + public function create(Channel $channel, User $user): int; public function update(Channel $channel): bool; public function delete(Channel $channel): bool; } diff --git a/app/src/Domain/Repository/Image/ImageRepository.php b/app/src/Domain/Repository/Image/ImageRepository.php new file mode 100644 index 0000000..7ba6033 --- /dev/null +++ b/app/src/Domain/Repository/Image/ImageRepository.php @@ -0,0 +1,62 @@ +db->getConnection()->prepare($query); + $statement->execute([ ':id' => $id ]); + $results = $statement->fetch(\PDO::FETCH_ASSOC); + + if (!is_array($results) || !count($results)) { + throw new EntityNotFoundException("Unable to find image."); + } + + return Image::fromArray($results); + } + + public function getFromUrl(string $url): Image + { + + } + + public function create(Image $image): Image + { + + return $image; + } + + public function update(Image $image): bool + { + + } + + public function delete(Image $image): bool + { + return false; + } +} diff --git a/app/src/Domain/Repository/Image/ImageRepositoryInterface.php b/app/src/Domain/Repository/Image/ImageRepositoryInterface.php new file mode 100644 index 0000000..1eed9c0 --- /dev/null +++ b/app/src/Domain/Repository/Image/ImageRepositoryInterface.php @@ -0,0 +1,15 @@ +userFromQueryResults($results); } - public function create(User $user): bool + public function create(User $user): User { $query = "INSERT INTO users (email, password, name) VALUES (:email, :password, :name)"; @@ -76,7 +76,10 @@ class UserRepository implements UserRepositoryInterface ':name' => $user->getName(), ]); - return (bool) $results; + $insertId = $this->db->getConnection()->lastInsertId(); + $user->setId($insertId); + + return $user; } public function update(User $user): bool @@ -96,6 +99,14 @@ class UserRepository implements UserRepositoryInterface ]); } + /** + * @TODO Figure out soft/hard delete logic for users + */ + public function delete(User $user): bool + { + return false; + } + public function verifyPassword(string $email, string $password): bool { try { diff --git a/app/src/Domain/Repository/User/UserRepositoryInterface.php b/app/src/Domain/Repository/User/UserRepositoryInterface.php index 00b55f6..aba48cc 100644 --- a/app/src/Domain/Repository/User/UserRepositoryInterface.php +++ b/app/src/Domain/Repository/User/UserRepositoryInterface.php @@ -2,13 +2,15 @@ namespace Slovocast\Domain\Repository\User; +use Slovocast\Domain\RepositoryInterface; use Slovocast\Domain\Entity\User; -interface UserRepositoryInterface +interface UserRepositoryInterface extends RepositoryInterface { public function get(int $id): User; public function getFromEmail(string $email): User; - public function create(User $user): bool; + public function create(User $user): User; public function update(User $user): bool; + public function delete(User $user): bool; public function verifyPassword(string $email, string $password): bool; } diff --git a/app/src/Domain/RepositoryInterface.php b/app/src/Domain/RepositoryInterface.php new file mode 100644 index 0000000..68d3607 --- /dev/null +++ b/app/src/Domain/RepositoryInterface.php @@ -0,0 +1,16 @@ +