17 lines
1.2 KiB
TypeScript
17 lines
1.2 KiB
TypeScript
|
|
import { database } from '../database.js';
|
||
|
|
import type { Annotation, CreateAnnotationRequest } from '../../shared/types.js';
|
||
|
|
|
||
|
|
type AnnotationRow = Annotation & { id: number | string; image_id: number | string };
|
||
|
|
function toAnnotation(row: AnnotationRow): Annotation { return { ...row, id: Number(row.id), image_id: Number(row.image_id) }; }
|
||
|
|
|
||
|
|
export const annotationsRepository = {
|
||
|
|
async listByImage(imageId: number): Promise<Annotation[]> {
|
||
|
|
return (await database.all<AnnotationRow>('SELECT id, image_id, x, y, content, author_name, status, created_at FROM annotations WHERE image_id = ? ORDER BY id ASC', [imageId])).map(toAnnotation);
|
||
|
|
},
|
||
|
|
async create(imageId: number, data: CreateAnnotationRequest): Promise<Annotation> {
|
||
|
|
const id = await database.insertId('INSERT INTO annotations (image_id, x, y, content, author_name) VALUES (?, ?, ?, ?, ?)', [imageId, data.x, data.y, data.content, data.author_name || '客户']);
|
||
|
|
return toAnnotation((await database.one<AnnotationRow>('SELECT id, image_id, x, y, content, author_name, status, created_at FROM annotations WHERE id = ?', [id]))!);
|
||
|
|
},
|
||
|
|
async remove(id: number): Promise<boolean> { return (await database.execute('DELETE FROM annotations WHERE id = ?', [id])).changes > 0; },
|
||
|
|
};
|