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 { return (await database.all('SELECT id, image_id, x, y, content, author_name, author_role, status, closure_reason, withdrawn_at, created_at FROM annotations WHERE image_id = ? ORDER BY id ASC', [imageId])).map(toAnnotation); }, async create(imageId: number, data: CreateAnnotationRequest): Promise { const id = await database.insertId('INSERT INTO annotations (image_id, x, y, content, author_name, author_role) VALUES (?, ?, ?, ?, ?, ?)', [imageId, data.x, data.y, data.content, data.author_name || '客户', data.author_role || 'client']); return toAnnotation((await database.one('SELECT id, image_id, x, y, content, author_name, author_role, status, closure_reason, withdrawn_at, created_at FROM annotations WHERE id = ?', [id]))!); }, async remove(id: number): Promise { return (await database.execute('DELETE FROM annotations WHERE id = ?', [id])).changes > 0; }, };