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