Add some methods for the image repository.

This commit is contained in:
Dave Smith-Hayes 2024-04-08 22:52:54 -04:00
parent 181b9cf375
commit be4fd75afe
3 changed files with 47 additions and 3 deletions

View File

@ -48,6 +48,9 @@ export class UserController implements Controller {
} }
public async loginUser(req: Request, res: Response): Promise<Response> { public async loginUser(req: Request, res: Response): Promise<Response> {
// match the passwords
//
// set the JWT data
return res; return res;
} }

View File

@ -5,12 +5,53 @@ import type { PoolConnection } from 'mariadb';
export class ImageRepository extends BaseRepository<Image> { export class ImageRepository extends BaseRepository<Image> {
public async get(id: EntityId): Promise<Entity<Image>> { public async get(id: EntityId): Promise<Entity<Image>> {
const conn = await this.pool.getConnection(); const conn: PoolConnection = await this.pool.getConnection();
await conn.release(); const query = `SELECT * FROM images WHERE id = ?`;
const rows = await conn.query(query, [ id ]);
if (!rows.length) {
throw Error("Unable to find Image.");
} }
public async create(image: Image): Promise<Entity<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 save(image: Image): Promise<Entity<Image>> {
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<Image>): Promise<Entity<Image>> {
}
public async delete(image: Entity<Image>): Promise<boolean> {
} }
} }

View File

@ -1,6 +1,6 @@
export type Image = { export type Image = {
url: URL, url: URL,
title?: string, title: string,
width: number, width: number,
height: number, height: number,
}; };