diff --git a/app/src/Domain/Entity/Episode.php b/app/src/Domain/Entity/Episode.php index 8001934..fb58b58 100644 --- a/app/src/Domain/Entity/Episode.php +++ b/app/src/Domain/Entity/Episode.php @@ -44,6 +44,21 @@ class Episode return $this->duration; } + public function getDescription(): string + { + return $this->description; + } + + public function getPublishedDate(): DateTimeImmutable + { + return $this->publishedDate; + } + + public function getEpisodeType(): string + { + return $this->episodeType; + } + public function getSerialNumber(): int { return $this->serialNumber; @@ -79,7 +94,7 @@ class Episode $props['serialNumber'], (bool) $props['explicit'], $props['publishedDate'], - $props['episdoeType'] + $props['episodeType'] ); if ($props['id']) { diff --git a/app/src/Domain/Repository/Channel/ChannelRepository.php b/app/src/Domain/Repository/Channel/ChannelRepository.php index 7837337..3e56378 100644 --- a/app/src/Domain/Repository/Channel/ChannelRepository.php +++ b/app/src/Domain/Repository/Channel/ChannelRepository.php @@ -120,10 +120,16 @@ class ChannelRepository implements ChannelRepositoryInterface $insertId = $this->db->getConnection()->lastInsertId(); $channel->setId($insertId); + $channel->setCreatedAt(new DateTimeImmutable()); + $channel->setUpdatedAt(new DateTime()); return $channel; } /** + * There are side-effects to this method, where the given Channel entity + * will have its `updatedAt` property set if the update in the database + * is successful. + * * @param Channel $channel * @return bool * @throws Throwable @@ -142,7 +148,7 @@ class ChannelRepository implements ChannelRepositoryInterface "; $statement = $this->db->getConnection()->prepare($query); - return $statement->execute([ + $result = $statement->execute([ $channel->getName(), $channel->getSlug(), $channel->getDescription(), @@ -152,6 +158,12 @@ class ChannelRepository implements ChannelRepositoryInterface $channel->isExplicit(), $channel->getId(), ]); + + if ($result == true) { + $channel->setUpdatedAt(new DateTime()); + } + + return $result; } /** diff --git a/app/src/Domain/Repository/Episode/EpisodeRespository.php b/app/src/Domain/Repository/Episode/EpisodeRespository.php index 1b1a692..2536a13 100644 --- a/app/src/Domain/Repository/Episode/EpisodeRespository.php +++ b/app/src/Domain/Repository/Episode/EpisodeRespository.php @@ -5,6 +5,7 @@ namespace Slovocast\Domain\Repository\Episode; use DateTimeImmutable; use DateTime; use Slovocast\Domain\Entity\Episode; +use Slovocast\Domain\Entity\Channel; use Slovocast\Domain\Repository\Episode\EpisodeRepositoryInterface; use Slovocast\Exception\EntityNotFoundException; use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; @@ -30,9 +31,56 @@ class EpisodeRepository implements EpisodeRepositoryInterface $results = $statement->fetch(\PDO::FETCH_ASSOC); if (!is_array($results) || !count($results)) { - throw new EntityNotFoundException("Unable to find Episode."); + throw new EntityNotFoundException("Unable to find episode."); } + $episode = $this->getEpisodeFromResults($results); + return $episode; + } + public function getFromChannel(Channel $channel): array + { + $query = "SELECT * FROM episodes WHERE channel_id = :channelId"; + $statement = $this->db->getConnection()->query($query); + $results = $statement->fetch(\PDO::FETCH_ASSOC); + + if (!is_array($results) || !count($results)) { + throw new EntityNotFoundException("Unable to find any episodes."); + } + + return array_map(fn($r) => $this->getEpisodeFromResults($r), $results); + } + + public function create(Episode $episode): Episode + { + $query = "INSERT INTO episodes ( + title, link, length, description, serialNumber, + explicit, publishedDate, episodeType + ) VALUE ( + :title, :link, :length, :description, :serialNumber, + :explicit, :publishedDate, :episodeType + )"; + + $statement = $this->db->getConnection()->prepare($query); + $results = $statement->execute([ + ':title' => $episode->getTitle(), + ':link' => $episode->getLink(), + ':length' => $episode->getLength(), + ':description' => $episode->getDescription(), + ':serialNumber' => $episode->getSerialNumber(), + ':explicit' => $episode->isExplicit(), + ':publishedDate' => $episode->getPublishedDate(), + ':episodeType' => $episode->getEpisodeType(), + ]); + + return $episode; + } + + /** + * @TODO Implement a plan for deleting + */ + public function delete(Episode $episode): bool + { + return false; } } diff --git a/app/src/Domain/Repository/Image/ImageRepository.php b/app/src/Domain/Repository/Image/ImageRepository.php index 08e6aaf..f79cf09 100644 --- a/app/src/Domain/Repository/Image/ImageRepository.php +++ b/app/src/Domain/Repository/Image/ImageRepository.php @@ -59,7 +59,8 @@ class ImageRepository implements ImageRepositoryInterface images (url, title, width, height) VALUES (:url, :title, :width, :height)"; - $this->db->getConnection()->exec($query, [ + $statement = $this->db->getConnection()->prepare($query); + $statement->execute([ ':url' => $image->getUrl(), ':title' => $image->getTitle(), ':width' => $image->getWidth(), @@ -68,6 +69,8 @@ class ImageRepository implements ImageRepositoryInterface $insertId = $this->db->getConnection()->lastInsertId(); $image->setId($insertId); + $image->setCreatedAt(new DateTimeImmutable()); + $image->setUpdatedAt(new DateTime()); return $image; } @@ -79,10 +82,10 @@ class ImageRepository implements ImageRepositoryInterface title = ?, width = ?, height = ? - WHERE id = ? - "; + WHERE id = ?"; - $results = $this->db->getConnection()->exec($query, [ + $statement = $this->db->getConnection()->prepare($query); + $results = $statement->execute([ $image->getUrl(), $image->getTitle(), $image->getWidth(), @@ -90,6 +93,10 @@ class ImageRepository implements ImageRepositoryInterface $image->getId(), ]); + if ($results == true) { + $image->setUpdatedAt(new DateTime()); + } + return $results; } diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index 4dd43fe..b486278 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -70,7 +70,8 @@ class UserRepository implements UserRepositoryInterface $query = "INSERT INTO users (email, password, name) VALUES (:email, :password, :name)"; - $results = $this->db->getConnection()->exec($query, [ + $statement = $this->db->getConnection()->prepare($query); + $statement->execute([ ':email' => $user->getEmail(), ':password' => $this->userAuth->hash($user->getPassword()), ':name' => $user->getName(),