Add the Channel repository class.

This commit is contained in:
Dave Smith-Hayes 2024-06-27 20:05:41 -04:00
parent 037cf674a8
commit 74b303fb2d
5 changed files with 68 additions and 5 deletions

View File

@ -46,8 +46,9 @@ final class CreateChannelsTable extends AbstractMigration
$table = $this->table('channels')->addTimestamps();
$table->addColumn('name', 'string')
->addColumn('slug', 'string')
->addColumn('description', 'string', [ 'null' => false ])
->addColumn('links', 'string')
->addColumn('link', 'string')
->addColumn('language', 'string')
->addColumn('copyright', 'string')
->addColumn('explicit', 'boolean', [ 'default' => false ])

View File

@ -0,0 +1,46 @@
<?php
namespace Slovocast\Domain\Repository\Channel;
use Slovocast\Domain\Repository\ChannelRepositoryInterface;
use Slovocast\Domain\Entity\Channel;
use Slovocast\Domain\Entity\User;
use Slovocast\Exception\EntityNotFoundException;
use React\Mysql\MysqlClient;
use function React\Async\await;
class ChannelRepository implements ChannelRepositoryInterface
{
public function __construct(
protected MysqlClient $db
) { }
protected function createChannelFromResults(array $results): Channel
{
return Channel::fromArray([
'name' => $results['name']
]);
}
public function get(int $id): Channel
{
$query = "SELECT * FROM channels WHERE id = ?";
$results = await($this->db->query($query, [ $id ]));
if (!count($results->resultRows)) {
throw new EntityNotFoundException("Unable to find Channel");
}
return $this->createChannelFromResults($results->resultRows[0]);
}
public function getFromUser(User $user): Channel
{
}
public function getFromSlug(string $slug): Channel
{
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Slovocast\Domain\Repository\Channel;
use Slovocast\Domain\Entity\Channel;
use Slovocast\Domain\Entity\User;
interface ChannelRepositoryInterface
{
public function get(int $id): Channel;
public function getFromUser(User $user): Channel;
public function getFromSlug(string $slug): Channel;
public function create(Channel $channel): bool;
public function update(Channel $channel): bool;
public function delete(Channel $channel): bool;
}

View File

@ -56,8 +56,8 @@ class UserRepository implements UserRepositoryInterface
$query = "SELECT * FROM users WHERE email = ? LIMIT 1";
$results = await($this->db->query($query, [ $email ]));
if (count($results->resultRows)) {
throw new EntityNotFoundException("Entity does not exist.");
if (!count($results->resultRows)) {
throw new EntityNotFoundException("Unable to find User");
}
return $this->userFromQueryResults($results->resultRows[0]);
@ -67,7 +67,7 @@ class UserRepository implements UserRepositoryInterface
* @param User $user
* @return bool True if the User is saved.
*/
public function save(User $user): bool
public function create(User $user): bool
{
$query = "INSERT INTO users (email, password, name)
VALUES (?, ?, ?)";

View File

@ -8,7 +8,7 @@ interface UserRepositoryInterface
{
public function get(int $id): User;
public function getFromEmail(string $email): User;
public function save(User $user): bool;
public function create(User $user): bool;
public function update(User $user): bool;
public function verifyPassword(string $email, string $password): bool;
}