From be4fd75afed2f4f1041934628e8ed1dbe87ad165 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 8 Apr 2024 22:52:54 -0400 Subject: [PATCH] Add some methods for the image repository. --- app/src/api/user-controller.ts | 3 ++ app/src/domain/repository/image-repository.ts | 45 ++++++++++++++++++- app/src/models/image.ts | 2 +- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/app/src/api/user-controller.ts b/app/src/api/user-controller.ts index 2feeb3d..17a9016 100644 --- a/app/src/api/user-controller.ts +++ b/app/src/api/user-controller.ts @@ -48,6 +48,9 @@ export class UserController implements Controller { } public async loginUser(req: Request, res: Response): Promise { + // match the passwords + // + // set the JWT data return res; } diff --git a/app/src/domain/repository/image-repository.ts b/app/src/domain/repository/image-repository.ts index e7db633..258d470 100644 --- a/app/src/domain/repository/image-repository.ts +++ b/app/src/domain/repository/image-repository.ts @@ -5,12 +5,53 @@ import type { PoolConnection } from 'mariadb'; export class ImageRepository extends BaseRepository { public async get(id: EntityId): Promise> { - const conn = await this.pool.getConnection(); + const conn: PoolConnection = await this.pool.getConnection(); + + const query = `SELECT * FROM images WHERE id = ?`; + const rows = await conn.query(query, [ id ]); + + if (!rows.length) { + throw Error("Unable to find Image."); + } + + const row = rows.shift(); await conn.release(); + return new Entity(row.id, { + url: new URL(row.url), + title: row.title, + width: row.width, + height: row.height + } as Image); } - public async create(image: Image): Promise> { + public async save(image: Image): Promise> { + const conn: PoolConnection = await this.pool.getConnection(); + + const query = ` + INSERT INTO images (url, title, width, height) + VALUES (?, ?, ?, ?) + `; + const rows = await conn.query(query, [ + image.url.toString(), + image.title, + image.width, + image.height + ]); + + if (!rows.insertId) { + throw Error("Unable to create Image"); + } + + await conn.release(); + return new Entity(rows.insertId, image); + } + + public async update(image: Entity): Promise> { + + } + + public async delete(image: Entity): Promise { } } diff --git a/app/src/models/image.ts b/app/src/models/image.ts index 2426101..47a8147 100644 --- a/app/src/models/image.ts +++ b/app/src/models/image.ts @@ -1,6 +1,6 @@ export type Image = { url: URL, - title?: string, + title: string, width: number, height: number, };