Add more to the migrations...should probably test them. Add to the Channel repository.

This commit is contained in:
Dave Smith-Hayes 2024-06-27 22:29:12 -04:00
parent a0a0bfcb35
commit a7826d5056
8 changed files with 111 additions and 16 deletions

View File

@ -34,7 +34,7 @@ final class CreateImagesTable extends AbstractMigration
{
$table = $this->table('images')->addTimestamps();
$table->addColumn('url', 'string')
->addColumn('title', [ 'null' => false ])
->addColumn('title', 'string', [ 'null' => false ])
->addColumn('width', 'integer', [
'unsigned' => true,
'null' => true,

View File

@ -49,12 +49,17 @@ final class CreateChannelsTable extends AbstractMigration
->addColumn('slug', 'string')
->addColumn('description', 'string', [ 'null' => false ])
->addColumn('link', 'string')
->addColumn('language', 'string')
->addColumn('language', 'string', [
'default' => 'en',
'limit' => 2
])
->addColumn('copyright', 'string')
->addColumn('explicit', 'boolean', [ 'default' => false ])
->addColumn('category', 'string')
->addColumn('owner_id', 'integer')
->addColumn('image_id', 'integer')
->addColumn('image_id', 'integer', [
'null' => true,
'unsigned' => true
])
->addIndex([ 'name' ], [ 'unique' => true ])
->addForeignKey('owner_id', 'users')
->addForeignKey('image_id', 'images')

View File

@ -19,7 +19,7 @@ CREATE TABLE episodes (
image_id INT(11) UNSIGNED NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(`id`),
FOREIGN KEY(`channel_id`) REFERENCES channels(`id`),
@ -47,11 +47,14 @@ final class CreateEpisodesTable extends AbstractMigration
$table->addColumn('title', 'string', [ 'null' => false ])
->addColumn('link', 'string')
->addColumn('duration', 'string')
->addColumn('length', 'integer')
->addColumn('length', 'integer', [ 'unsigned' => true ])
->addColumn('description', 'text')
->addColumn('explicit', 'boolean', [ 'default' => false ])
->addColumn('channel_id', 'integer')
->addColumn('image_id', 'integer', [ 'null' => true ])
->addColumn('channel_id', 'integer', [ 'unsigned' => true ])
->addColumn('image_id', 'integer', [
'null' => true,
'unsigned' => true
])
->addForeignKey('channel_id', 'channels')
->addForeignKey('image_id', 'images');

View File

@ -14,7 +14,7 @@ CREATE TABLE `transactions` (
total INT(11) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
FOREIGN KEY (`channel_id`) REFERENCES channels(`id`)
@ -41,7 +41,7 @@ final class CreateTransactionsTable extends AbstractMigration
$table->addColumn('user_id', 'integer', [ 'null' => false ])
->addColumn('code', 'string')
->addColumn('type', 'string')
->addColumn('total', 'int', [ 'null' => false ])
->addColumn('total', 'integer', [ 'null' => false ])
->addForeignKey('user_id', 'user');
$table->create();

View File

@ -10,7 +10,7 @@ CREATE TABLE `categories` (
name VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(`id`),
UNIQUE KEY(`name`)
@ -35,7 +35,7 @@ final class CreateCategoriesTable extends AbstractMigration
$table = $this->table('categories')->addTimestamps();
$table->addColumn('name', 'string', [ 'null' => false ])
->addIndex('name', [ 'unique' => true ]);
->addIndex([ 'name' ], [ 'unique' => true ]);
$table->create();
}

View File

@ -7,9 +7,10 @@ use Slovocast\Domain\Entity;
class Channel
{
use Entity;
public function __construct(
private string $name,
private string $slug,
private string $description,
private ?string $link = '',
private ?string $language = '',
@ -22,11 +23,34 @@ class Channel
return $this->name;
}
public function setName(string $name): Channel
{
$this->name = $name;
return $this;
}
public function getSlug(): string
{
return $this->slug;
}
public function setSlug(string $slug): Channel
{
$this->slug = $slug;
return $this;
}
public function getDescription(): string
{
return $this->description;
}
public function setDescription(string $description): Channel
{
$this->description = $description;
return $this;
}
public function getLink(): string
{
return $this->link;
@ -79,6 +103,7 @@ class Channel
{
$channel = new self(
$props['name'],
$props['slug'] ?? '',
$props['description'] ?? '',
$props['link'] ?? '',
$props['language'] ?? '',

View File

@ -2,11 +2,15 @@
namespace Slovocast\Domain\Repository\Channel;
use DateTime;
use DateTimeImmutable;
use Exception;
use Slovocast\Domain\Repository\ChannelRepositoryInterface;
use Slovocast\Domain\Entity\Channel;
use Slovocast\Domain\Entity\User;
use Slovocast\Exception\EntityNotFoundException;
use React\Mysql\MysqlClient;
use Throwable;
use function React\Async\await;
class ChannelRepository implements ChannelRepositoryInterface
@ -16,16 +20,32 @@ class ChannelRepository implements ChannelRepositoryInterface
) { }
/**
* @param mixed[] $results Result Set from the Query
* @param array $results Result Set from the Query
* @return Channel
* @throws Exception
*/
protected function createChannelFromResults(array $results): Channel
{
return Channel::fromArray([
'name' => $results['name']
'id' => $results['id'],
'name' => $results['name'],
'slug' => $results['slug'],
'description' => $results['description'],
'link' => $results['link'],
'language' => $results['language'],
'copyright' => $results['copyright'],
'explicit' => $results['explicit'],
'createdAt' => new DateTimeImmutable($results['created_at']),
'updatedAt' => new DateTime($results['updated_at'])
]);
}
/**
* @param int $id
* @return Channel
* @throws EntityNotFoundException
* @throws Throwable
*/
public function get(int $id): Channel
{
$query = "SELECT * FROM channels WHERE id = ?";
@ -38,13 +58,55 @@ class ChannelRepository implements ChannelRepositoryInterface
return $this->createChannelFromResults($results->resultRows[0]);
}
/**
* @throws EntityNotFoundException
* @throws Throwable
*/
public function getFromUser(User $user): Channel
{
$query = "SELECT * FROM channels WHERE owner_id = ?";
$results = await($this->db->query($query, [ $user->getId() ]));
if (!count($results->resultRows)) {
throw new EntityNotFoundException("Unable to find Channel");
}
return $this->createChannelFromResults($results->resultRows[0]);
}
/**
* @throws EntityNotFoundException
* @throws Throwable
*/
public function getFromSlug(string $slug): Channel
{
$query = "SELECT * FROM channels WHERE slug = ?";
$results = await($this->db->query($query, [ $slug ]));
if (!count($results->resultRows)) {
throw new EntityNotFoundException("Unable to find Channel");
}
return $this->createChannelFromResults($results->resultRows[0]);
}
public function create(Channel $channel, User $owner): int
{
$query = "INSERT INTO channels
(name, slug, description, link, language, copyright, explicit, owner_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$results = await($this->db->query($query, [
$channel->getName(),
$channel->getSlug(),
$channel->getDescription(),
$channel->getLink(),
$channel->getLanguage(),
$channel->getCopyright(),
$channel->isExplicit(),
$owner->getId()
]));
return $results->insertId;
}
}

View File

@ -10,7 +10,7 @@ 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 create(Channel $channel): int;
public function update(Channel $channel): bool;
public function delete(Channel $channel): bool;
}