Update the ChannelRepository to use the PDO

This commit is contained in:
Dave Smith-Hayes 2024-11-04 21:29:20 -05:00
parent d360cceee3
commit 0705d15cd2

View File

@ -5,18 +5,18 @@ namespace Slovocast\Domain\Repository\Channel;
use DateTime; use DateTime;
use DateTimeImmutable; use DateTimeImmutable;
use Exception; use Exception;
use Slovocast\Domain\Repository\ChannelRepositoryInterface; use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface;
use Slovocast\Domain\Entity\Channel; use Slovocast\Domain\Entity\Channel;
use Slovocast\Domain\Entity\User; use Slovocast\Domain\Entity\User;
use Slovocast\Exception\EntityNotFoundException; use Slovocast\Exception\EntityNotFoundException;
use React\Mysql\MysqlClient; use Slovocast\Infrastructure\Api\Database\DatabaseConnectionInterface;
use Throwable; use Throwable;
use function React\Async\await; use function React\Async\await;
class ChannelRepository implements ChannelRepositoryInterface class ChannelRepository implements ChannelRepositoryInterface
{ {
public function __construct( public function __construct(
protected MysqlClient $db protected DatabaseConnectionInterface $db
) { } ) { }
/** /**
@ -48,14 +48,16 @@ class ChannelRepository implements ChannelRepositoryInterface
*/ */
public function get(int $id): Channel public function get(int $id): Channel
{ {
$query = "SELECT * FROM channels WHERE id = ?"; $query = "SELECT * FROM channels WHERE id = :id";
$results = await($this->db->query($query, [ $id ])); $statement = $this->db->getConnection()->prepare($query);
$statement->execute([ ':id' => $id ]);
$results = $statement->fetch(\PDO::FETCH_ASSOC);
if (!count($results->resultRows)) { if (!count($results)) {
throw new EntityNotFoundException("Unable to find Channel"); throw new EntityNotFoundException("Unable to find Channel");
} }
return $this->createChannelFromResults($results->resultRows[0]); return $this->createChannelFromResults($results);
} }
/** /**
@ -65,13 +67,15 @@ class ChannelRepository implements ChannelRepositoryInterface
public function getFromUser(User $user): Channel public function getFromUser(User $user): Channel
{ {
$query = "SELECT * FROM channels WHERE owner_id = ?"; $query = "SELECT * FROM channels WHERE owner_id = ?";
$results = await($this->db->query($query, [ $user->getId() ])); $statement = this->db->getConnection()->prepare($query);
$statement->execute([ ':id' => $user->getId() ]);
$results = $statement->fetch(\PDO::FETCH_ASSOC);
if (!count($results->resultRows)) { if (!count($results)) {
throw new EntityNotFoundException("Unable to find Channel"); throw new EntityNotFoundException("Unable to find Channel");
} }
return $this->createChannelFromResults($results->resultRows[0]); return $this->createChannelFromResults($results);
} }
/** /**
@ -80,14 +84,16 @@ class ChannelRepository implements ChannelRepositoryInterface
*/ */
public function getFromSlug(string $slug): Channel public function getFromSlug(string $slug): Channel
{ {
$query = "SELECT * FROM channels WHERE slug = ?"; $query = "SELECT * FROM channels WHERE slug = :slug";
$results = await($this->db->query($query, [ $slug ])); $statement = $this->db->getConnection()->prepare($query);
$statement->execute([ ':slug' => $slug ]);
$results = $statement->fetch(\PDO::FETCH_ASSOC);
if (!count($results->resultRows)) { if (!count($results)) {
throw new EntityNotFoundException("Unable to find Channel"); throw new EntityNotFoundException("Unable to find Channel");
} }
return $this->createChannelFromResults($results->resultRows[0]); return $this->createChannelFromResults($results);
} }
/** /**
@ -95,13 +101,14 @@ class ChannelRepository implements ChannelRepositoryInterface
*/ */
public function create(Channel $channel, User $owner): int public function create(Channel $channel, User $owner): int
{ {
$query = <<<SQL $query = "
INSERT INTO channels (name, slug, description, link, INSERT INTO channels (name, slug, description, link,
language, copyright, explicit, owner_id) language, copyright, explicit, owner_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
SQL; ";
$results = await($this->db->query($query, [ $statement = $this->db->getConnection()->prepare($query);
$statement->execute([
$channel->getName(), $channel->getName(),
$channel->getSlug(), $channel->getSlug(),
$channel->getDescription(), $channel->getDescription(),
@ -110,9 +117,9 @@ SQL;
$channel->getCopyright(), $channel->getCopyright(),
$channel->isExplicit(), $channel->isExplicit(),
$owner->getId() $owner->getId()
])); ]);
return $results->insertId; return $this->db->getConnection()->lastInsertId();
} }
/** /**
@ -122,7 +129,7 @@ SQL;
*/ */
public function update(Channel $channel): bool public function update(Channel $channel): bool
{ {
$query = <<<SQL $query = "
UPDATE channels SET name = ?, UPDATE channels SET name = ?,
slug = ?, slug = ?,
description = ?, description = ?,
@ -131,9 +138,10 @@ UPDATE channels SET name = ?,
copyright = ?, copyright = ?,
explicit = ? explicit = ?
WHERE id = ?; WHERE id = ?;
SQL; ";
$results = await($this->db->query($query, [ $statement = $this->db->getConnection()->prepare($query);
return $statement->execute([
$channel->getName(), $channel->getName(),
$channel->getSlug(), $channel->getSlug(),
$channel->getDescription(), $channel->getDescription(),
@ -142,11 +150,12 @@ SQL;
$channel->getCopyright(), $channel->getCopyright(),
$channel->isExplicit(), $channel->isExplicit(),
$channel->getId(), $channel->getId(),
])); ]);
return (bool) $results->affectedRows;
} }
/**
* Don't delete for now.
*/
public function delete(Channel $channel): bool public function delete(Channel $channel): bool
{ {
return false; return false;