From 82101e7d1d26a9e73bea23be37aa034a5698adc2 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Thu, 11 Apr 2024 21:06:12 -0400 Subject: [PATCH] Update the formatting for SQL files, add changes to the channel repository --- app/src/domain/entity.ts | 10 +++-- .../domain/repository/channel-repository.ts | 39 +++++++++++++++++++ app/src/domain/repository/user-repository.ts | 11 +++--- sql/01-users.sql | 8 ++-- sql/02-images.sql | 10 ++--- sql/03-channels.sql | 14 +++---- sql/04-episodes.sql | 14 +++---- 7 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 app/src/domain/repository/channel-repository.ts diff --git a/app/src/domain/entity.ts b/app/src/domain/entity.ts index e4c64cb..1c827d4 100644 --- a/app/src/domain/entity.ts +++ b/app/src/domain/entity.ts @@ -2,11 +2,11 @@ export type EntityId = number|string; export class Entity { protected readonly id: EntityId; - protected model: T; + protected _model: T; public constructor(id: EntityId, model: T) { this.id = id; - this.model = model; + this._model = model; } public getId(): EntityId { @@ -14,6 +14,10 @@ export class Entity { } public getModel(): T { - return this.model; + return this._model; + } + + public get model(): T { + return this.getModel(); } } diff --git a/app/src/domain/repository/channel-repository.ts b/app/src/domain/repository/channel-repository.ts new file mode 100644 index 0000000..5798396 --- /dev/null +++ b/app/src/domain/repository/channel-repository.ts @@ -0,0 +1,39 @@ +import { type EntityId, Entity } from '@slovo/domain/entity'; +import { BaseRepository } from '@slovo/domain/repository'; +import { type Channel } from '@slovo/models/channel'; + +export class ChannelRepository extends BaseRepository { + public async get(id: EntityId): Promise> { + const conn = await this.pool.getConnection(); + const query = `SELECT * FROM Channels WHERE id = ?`; + const rows = await conn.query(query, [ id ]); + + if (!rows.length) { + throw Error("Unable to find Channel."); + } + + return new Entity(id, { + name: rows[0].name, + description: rows[0].description, + link: new URL(rows[0].link), + language: rows[0].language, + copyright: rows[0].copyright, + explicit: rows[0].explicit as boolean, + category: rows[0].category, + } as Channel); + } + + public async save(channel: Entity): Promise> { + + return channel; + } + + public async update(channel: Entity): Promise> { + + return channel; + } + + public async delete(channel: Entity): Promise { + return true; + } +} diff --git a/app/src/domain/repository/user-repository.ts b/app/src/domain/repository/user-repository.ts index e025c07..350b265 100644 --- a/app/src/domain/repository/user-repository.ts +++ b/app/src/domain/repository/user-repository.ts @@ -68,9 +68,9 @@ export class UserRepository extends BaseRepository { const query = `UPDATE user SET name = ?, email = ?, password = ? WHERE id = ?`; const results = await conn.query(query, [ - user.getModel().name, - user.getModel().email, - user.getModel().password, + user.model.name, + user.model.email, + user.model.password, user.getId() ]); @@ -82,9 +82,8 @@ export class UserRepository extends BaseRepository { return user; } + /// Don't delete users public async delete(user: Entity): Promise { - const conn: PoolConnection = await this.pool.getConnection(); - - await conn.release(); + return true; } } diff --git a/sql/01-users.sql b/sql/01-users.sql index 4f4ff9d..c5598e2 100644 --- a/sql/01-users.sql +++ b/sql/01-users.sql @@ -1,8 +1,8 @@ CREATE TABLE users ( - id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - email VARCHAR(255) NOT NULL, - password VARCHAR(255) NOT NULL, - name VARCHAR(255) NOT NULL, + id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + email VARCHAR(255) NOT NULL, + password VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, diff --git a/sql/02-images.sql b/sql/02-images.sql index 8365a7b..0070c78 100644 --- a/sql/02-images.sql +++ b/sql/02-images.sql @@ -1,9 +1,9 @@ CREATE TABLE images ( - id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - url TEXT NOT NULL, - title TEXT NULL, - width INT(11) UNSIGNED NULL, - height INT(11) UNSIGNED NULL, + id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + url TEXT NOT NULL, + title TEXT NULL, + width INT(11) UNSIGNED NULL, + height INT(11) UNSIGNED NULL, PRIMARY KEY(`id`), UNIQUE KEY(`url`) diff --git a/sql/03-channels.sql b/sql/03-channels.sql index 9895993..f5760ef 100644 --- a/sql/03-channels.sql +++ b/sql/03-channels.sql @@ -1,12 +1,12 @@ CREATE TABLE channels ( - id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - name VARCHAR(255) NOT NULL, + id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, description TEXT NOT NULL, - link VARCHAR(255) NULL, - language VARCHAR(2) NOT NULL, - copyright VARCHAR(255) NULL, - explicit BOOLEAN DEFAULT false, - category VARCHAR(255), + link VARCHAR(255) NULL, + language VARCHAR(2) NOT NULL, + copyright VARCHAR(255) NULL, + explicit BOOLEAN DEFAULT false, + category VARCHAR(255), owner_id INT(11) UNSIGNED NOT NULL, image_id INT(11) UNSIGNED NOT NULL, diff --git a/sql/04-episodes.sql b/sql/04-episodes.sql index eaacf3b..8bf55b4 100644 --- a/sql/04-episodes.sql +++ b/sql/04-episodes.sql @@ -1,15 +1,15 @@ CREATE TABLE episodes ( - id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - title VARCHAR(255) NOT NULL, - link VARCHAR(255) NOT NULL, - duration VARCHAR(127) NOT NULL, - length INT(11) UNSIGNED NOT NULL, + id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + title VARCHAR(255) NOT NULL, + link VARCHAR(255) NOT NULL, + duration VARCHAR(127) NOT NULL COMMENT "HH:MM:SS representation", + length INT(11) UNSIGNED NOT NULL COMMENT "Length in seconds", description TEXT NOT NULL, explicit BOOLEAN NOT NULL DEFAULT true, - channel_id INT(11) UNSIGNED NOT NULL, - image_id INT(11) UNSIGNED NOT NULL, + channel_id INT(11) UNSIGNED NOT NULL, + 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,