Add some methods for the image repository.
This commit is contained in:
parent
181b9cf375
commit
be4fd75afe
@ -48,6 +48,9 @@ export class UserController implements Controller {
|
||||
}
|
||||
|
||||
public async loginUser(req: Request, res: Response): Promise<Response> {
|
||||
// match the passwords
|
||||
//
|
||||
// set the JWT data
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@ -5,12 +5,53 @@ import type { PoolConnection } from 'mariadb';
|
||||
|
||||
export class ImageRepository extends BaseRepository<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> {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
export type Image = {
|
||||
url: URL,
|
||||
title?: string,
|
||||
title: string,
|
||||
width: number,
|
||||
height: number,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user