Update all the PDO calls, flesh out more of the Episode repository.

This commit is contained in:
Dave Smith-Hayes 2024-11-15 02:04:10 +00:00
parent 20a9edf770
commit cbde3e2fbf
5 changed files with 91 additions and 8 deletions

View File

@ -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']) {

View File

@ -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;
}
/**

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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(),