From a7826d5056abca5129d6aa94b8d2acc183e937c5 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Thu, 27 Jun 2024 22:29:12 -0400 Subject: [PATCH] Add more to the migrations...should probably test them. Add to the Channel repository. --- .../20240624022427_create_images.php | 2 +- .../20240624023258_create_channels_table.php | 11 +++- .../20240627230956_create_episodes_table.php | 11 ++-- ...240627231445_create_transactions_table.php | 4 +- ...20240627231953_create_categories_table.php | 4 +- app/src/Domain/Entity/Channel.php | 27 +++++++- .../Repository/Channel/ChannelRepository.php | 66 ++++++++++++++++++- .../Channel/ChannelRepositoryInterface.php | 2 +- 8 files changed, 111 insertions(+), 16 deletions(-) diff --git a/app/db/migrations/20240624022427_create_images.php b/app/db/migrations/20240624022427_create_images.php index 824d951..1c87be6 100644 --- a/app/db/migrations/20240624022427_create_images.php +++ b/app/db/migrations/20240624022427_create_images.php @@ -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, diff --git a/app/db/migrations/20240624023258_create_channels_table.php b/app/db/migrations/20240624023258_create_channels_table.php index 8041593..7045e71 100644 --- a/app/db/migrations/20240624023258_create_channels_table.php +++ b/app/db/migrations/20240624023258_create_channels_table.php @@ -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') diff --git a/app/db/migrations/20240627230956_create_episodes_table.php b/app/db/migrations/20240627230956_create_episodes_table.php index 728ed3f..401ae48 100644 --- a/app/db/migrations/20240627230956_create_episodes_table.php +++ b/app/db/migrations/20240627230956_create_episodes_table.php @@ -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'); diff --git a/app/db/migrations/20240627231445_create_transactions_table.php b/app/db/migrations/20240627231445_create_transactions_table.php index a8cd3eb..3cda19b 100644 --- a/app/db/migrations/20240627231445_create_transactions_table.php +++ b/app/db/migrations/20240627231445_create_transactions_table.php @@ -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(); diff --git a/app/db/migrations/20240627231953_create_categories_table.php b/app/db/migrations/20240627231953_create_categories_table.php index fc4e932..87f8b1a 100644 --- a/app/db/migrations/20240627231953_create_categories_table.php +++ b/app/db/migrations/20240627231953_create_categories_table.php @@ -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(); } diff --git a/app/src/Domain/Entity/Channel.php b/app/src/Domain/Entity/Channel.php index ab44ee4..700e5a7 100644 --- a/app/src/Domain/Entity/Channel.php +++ b/app/src/Domain/Entity/Channel.php @@ -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'] ?? '', diff --git a/app/src/Domain/Repository/Channel/ChannelRepository.php b/app/src/Domain/Repository/Channel/ChannelRepository.php index caac622..fa6b199 100644 --- a/app/src/Domain/Repository/Channel/ChannelRepository.php +++ b/app/src/Domain/Repository/Channel/ChannelRepository.php @@ -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; } } diff --git a/app/src/Domain/Repository/Channel/ChannelRepositoryInterface.php b/app/src/Domain/Repository/Channel/ChannelRepositoryInterface.php index d024208..3e4929d 100644 --- a/app/src/Domain/Repository/Channel/ChannelRepositoryInterface.php +++ b/app/src/Domain/Repository/Channel/ChannelRepositoryInterface.php @@ -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; }