Update the ChannelRepository to use the PDO
This commit is contained in:
parent
d360cceee3
commit
0705d15cd2
@ -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
|
||||
$query = "
|
||||
INSERT INTO channels (name, slug, description, link,
|
||||
language, copyright, explicit, owner_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
SQL;
|
||||
";
|
||||
|
||||
$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,7 +129,7 @@ SQL;
|
||||
*/
|
||||
public function update(Channel $channel): bool
|
||||
{
|
||||
$query = <<<SQL
|
||||
$query = "
|
||||
UPDATE channels SET name = ?,
|
||||
slug = ?,
|
||||
description = ?,
|
||||
@ -131,9 +138,10 @@ UPDATE channels SET name = ?,
|
||||
copyright = ?,
|
||||
explicit = ?
|
||||
WHERE id = ?;
|
||||
SQL;
|
||||
";
|
||||
|
||||
$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;
|
||||
|
Loading…
Reference in New Issue
Block a user