Add more to the migrations...should probably test them. Add to the Channel repository.
This commit is contained in:
parent
a0a0bfcb35
commit
a7826d5056
@ -34,7 +34,7 @@ final class CreateImagesTable extends AbstractMigration
|
|||||||
{
|
{
|
||||||
$table = $this->table('images')->addTimestamps();
|
$table = $this->table('images')->addTimestamps();
|
||||||
$table->addColumn('url', 'string')
|
$table->addColumn('url', 'string')
|
||||||
->addColumn('title', [ 'null' => false ])
|
->addColumn('title', 'string', [ 'null' => false ])
|
||||||
->addColumn('width', 'integer', [
|
->addColumn('width', 'integer', [
|
||||||
'unsigned' => true,
|
'unsigned' => true,
|
||||||
'null' => true,
|
'null' => true,
|
||||||
|
@ -49,12 +49,17 @@ final class CreateChannelsTable extends AbstractMigration
|
|||||||
->addColumn('slug', 'string')
|
->addColumn('slug', 'string')
|
||||||
->addColumn('description', 'string', [ 'null' => false ])
|
->addColumn('description', 'string', [ 'null' => false ])
|
||||||
->addColumn('link', 'string')
|
->addColumn('link', 'string')
|
||||||
->addColumn('language', 'string')
|
->addColumn('language', 'string', [
|
||||||
|
'default' => 'en',
|
||||||
|
'limit' => 2
|
||||||
|
])
|
||||||
->addColumn('copyright', 'string')
|
->addColumn('copyright', 'string')
|
||||||
->addColumn('explicit', 'boolean', [ 'default' => false ])
|
->addColumn('explicit', 'boolean', [ 'default' => false ])
|
||||||
->addColumn('category', 'string')
|
|
||||||
->addColumn('owner_id', 'integer')
|
->addColumn('owner_id', 'integer')
|
||||||
->addColumn('image_id', 'integer')
|
->addColumn('image_id', 'integer', [
|
||||||
|
'null' => true,
|
||||||
|
'unsigned' => true
|
||||||
|
])
|
||||||
->addIndex([ 'name' ], [ 'unique' => true ])
|
->addIndex([ 'name' ], [ 'unique' => true ])
|
||||||
->addForeignKey('owner_id', 'users')
|
->addForeignKey('owner_id', 'users')
|
||||||
->addForeignKey('image_id', 'images')
|
->addForeignKey('image_id', 'images')
|
||||||
|
@ -19,7 +19,7 @@ CREATE TABLE episodes (
|
|||||||
image_id INT(11) UNSIGNED NOT NULL,
|
image_id INT(11) UNSIGNED NOT NULL,
|
||||||
|
|
||||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
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`),
|
PRIMARY KEY(`id`),
|
||||||
FOREIGN KEY(`channel_id`) REFERENCES channels(`id`),
|
FOREIGN KEY(`channel_id`) REFERENCES channels(`id`),
|
||||||
@ -47,11 +47,14 @@ final class CreateEpisodesTable extends AbstractMigration
|
|||||||
$table->addColumn('title', 'string', [ 'null' => false ])
|
$table->addColumn('title', 'string', [ 'null' => false ])
|
||||||
->addColumn('link', 'string')
|
->addColumn('link', 'string')
|
||||||
->addColumn('duration', 'string')
|
->addColumn('duration', 'string')
|
||||||
->addColumn('length', 'integer')
|
->addColumn('length', 'integer', [ 'unsigned' => true ])
|
||||||
->addColumn('description', 'text')
|
->addColumn('description', 'text')
|
||||||
->addColumn('explicit', 'boolean', [ 'default' => false ])
|
->addColumn('explicit', 'boolean', [ 'default' => false ])
|
||||||
->addColumn('channel_id', 'integer')
|
->addColumn('channel_id', 'integer', [ 'unsigned' => true ])
|
||||||
->addColumn('image_id', 'integer', [ 'null' => true ])
|
->addColumn('image_id', 'integer', [
|
||||||
|
'null' => true,
|
||||||
|
'unsigned' => true
|
||||||
|
])
|
||||||
->addForeignKey('channel_id', 'channels')
|
->addForeignKey('channel_id', 'channels')
|
||||||
->addForeignKey('image_id', 'images');
|
->addForeignKey('image_id', 'images');
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ CREATE TABLE `transactions` (
|
|||||||
total INT(11) NOT NULL,
|
total INT(11) NOT NULL,
|
||||||
|
|
||||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
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`),
|
PRIMARY KEY (`id`),
|
||||||
FOREIGN KEY (`channel_id`) REFERENCES channels(`id`)
|
FOREIGN KEY (`channel_id`) REFERENCES channels(`id`)
|
||||||
@ -41,7 +41,7 @@ final class CreateTransactionsTable extends AbstractMigration
|
|||||||
$table->addColumn('user_id', 'integer', [ 'null' => false ])
|
$table->addColumn('user_id', 'integer', [ 'null' => false ])
|
||||||
->addColumn('code', 'string')
|
->addColumn('code', 'string')
|
||||||
->addColumn('type', 'string')
|
->addColumn('type', 'string')
|
||||||
->addColumn('total', 'int', [ 'null' => false ])
|
->addColumn('total', 'integer', [ 'null' => false ])
|
||||||
->addForeignKey('user_id', 'user');
|
->addForeignKey('user_id', 'user');
|
||||||
|
|
||||||
$table->create();
|
$table->create();
|
||||||
|
@ -10,7 +10,7 @@ CREATE TABLE `categories` (
|
|||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
|
|
||||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
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`),
|
PRIMARY KEY(`id`),
|
||||||
UNIQUE KEY(`name`)
|
UNIQUE KEY(`name`)
|
||||||
@ -35,7 +35,7 @@ final class CreateCategoriesTable extends AbstractMigration
|
|||||||
$table = $this->table('categories')->addTimestamps();
|
$table = $this->table('categories')->addTimestamps();
|
||||||
|
|
||||||
$table->addColumn('name', 'string', [ 'null' => false ])
|
$table->addColumn('name', 'string', [ 'null' => false ])
|
||||||
->addIndex('name', [ 'unique' => true ]);
|
->addIndex([ 'name' ], [ 'unique' => true ]);
|
||||||
|
|
||||||
$table->create();
|
$table->create();
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ use Slovocast\Domain\Entity;
|
|||||||
class Channel
|
class Channel
|
||||||
{
|
{
|
||||||
use Entity;
|
use Entity;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private string $name,
|
private string $name,
|
||||||
|
private string $slug,
|
||||||
private string $description,
|
private string $description,
|
||||||
private ?string $link = '',
|
private ?string $link = '',
|
||||||
private ?string $language = '',
|
private ?string $language = '',
|
||||||
@ -22,11 +23,34 @@ class Channel
|
|||||||
return $this->name;
|
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
|
public function getDescription(): string
|
||||||
{
|
{
|
||||||
return $this->description;
|
return $this->description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setDescription(string $description): Channel
|
||||||
|
{
|
||||||
|
$this->description = $description;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getLink(): string
|
public function getLink(): string
|
||||||
{
|
{
|
||||||
return $this->link;
|
return $this->link;
|
||||||
@ -79,6 +103,7 @@ class Channel
|
|||||||
{
|
{
|
||||||
$channel = new self(
|
$channel = new self(
|
||||||
$props['name'],
|
$props['name'],
|
||||||
|
$props['slug'] ?? '',
|
||||||
$props['description'] ?? '',
|
$props['description'] ?? '',
|
||||||
$props['link'] ?? '',
|
$props['link'] ?? '',
|
||||||
$props['language'] ?? '',
|
$props['language'] ?? '',
|
||||||
|
@ -2,11 +2,15 @@
|
|||||||
|
|
||||||
namespace Slovocast\Domain\Repository\Channel;
|
namespace Slovocast\Domain\Repository\Channel;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use Exception;
|
||||||
use Slovocast\Domain\Repository\ChannelRepositoryInterface;
|
use Slovocast\Domain\Repository\ChannelRepositoryInterface;
|
||||||
use Slovocast\Domain\Entity\Channel;
|
use Slovocast\Domain\Entity\Channel;
|
||||||
use Slovocast\Domain\Entity\User;
|
use Slovocast\Domain\Entity\User;
|
||||||
use Slovocast\Exception\EntityNotFoundException;
|
use Slovocast\Exception\EntityNotFoundException;
|
||||||
use React\Mysql\MysqlClient;
|
use React\Mysql\MysqlClient;
|
||||||
|
use Throwable;
|
||||||
use function React\Async\await;
|
use function React\Async\await;
|
||||||
|
|
||||||
class ChannelRepository implements ChannelRepositoryInterface
|
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
|
* @return Channel
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function createChannelFromResults(array $results): Channel
|
protected function createChannelFromResults(array $results): Channel
|
||||||
{
|
{
|
||||||
return Channel::fromArray([
|
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
|
public function get(int $id): Channel
|
||||||
{
|
{
|
||||||
$query = "SELECT * FROM channels WHERE id = ?";
|
$query = "SELECT * FROM channels WHERE id = ?";
|
||||||
@ -38,13 +58,55 @@ class ChannelRepository implements ChannelRepositoryInterface
|
|||||||
return $this->createChannelFromResults($results->resultRows[0]);
|
return $this->createChannelFromResults($results->resultRows[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws EntityNotFoundException
|
||||||
|
* @throws Throwable
|
||||||
|
*/
|
||||||
public function getFromUser(User $user): Channel
|
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
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ interface ChannelRepositoryInterface
|
|||||||
public function get(int $id): Channel;
|
public function get(int $id): Channel;
|
||||||
public function getFromUser(User $user): Channel;
|
public function getFromUser(User $user): Channel;
|
||||||
public function getFromSlug(string $slug): 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 update(Channel $channel): bool;
|
||||||
public function delete(Channel $channel): bool;
|
public function delete(Channel $channel): bool;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user