- 实现用户登录、登出、密码修改等认证功能 - 添加会话管理和权限控制中间件 - 创建图片批注组件和相关API路由 - 实现批注的增删改查功能 - 添加Docker和Git忽略配置文件 - 创建系统架构文档和开发约定说明 - 集成认证模块到前端应用路由中
40 lines
2.4 KiB
TypeScript
40 lines
2.4 KiB
TypeScript
import { Router, type Response, type NextFunction } from 'express';
|
|
import { imagesRepository } from '../repositories/imagesRepository.js';
|
|
import { annotationsRepository } from '../repositories/annotationsRepository.js';
|
|
import { canWriteProject, requireWriter, type AuthRequest } from '../auth.js';
|
|
import { database } from '../database.js';
|
|
|
|
const router = Router();
|
|
|
|
async function imageProjectId(imageId: number): Promise<number | undefined> {
|
|
return (await database.one<{ project_id: number }>('SELECT c.project_id FROM images i JOIN notes n ON n.id = i.note_id JOIN collections c ON c.id = n.collection_id WHERE i.id = ?', [imageId]))?.project_id;
|
|
}
|
|
|
|
router.get('/:imageId/annotations', requireWriter, async (req: AuthRequest, res: Response, next: NextFunction) => {
|
|
try {
|
|
const imageId = Number(req.params.imageId);
|
|
if (!Number.isFinite(imageId)) { res.status(400).json({ error: '无效的图片 ID' }); return; }
|
|
const projectId = await imageProjectId(imageId);
|
|
if (!projectId) { res.status(404).json({ error: '图片不存在' }); return; }
|
|
if (!await canWriteProject(req, projectId)) { res.status(403).json({ error: '无权查看该图片' }); return; }
|
|
res.json(await annotationsRepository.listByImage(imageId));
|
|
} catch (error) { next(error); }
|
|
});
|
|
|
|
router.post('/:imageId/annotations', requireWriter, async (req: AuthRequest, res: Response, next: NextFunction) => {
|
|
try {
|
|
const imageId = Number(req.params.imageId);
|
|
if (!Number.isFinite(imageId)) { res.status(400).json({ error: '无效的图片 ID' }); return; }
|
|
if (!await imagesRepository.findById(imageId)) { res.status(404).json({ error: '图片不存在' }); return; }
|
|
const projectId = await imageProjectId(imageId);
|
|
if (!projectId || !await canWriteProject(req, projectId)) { res.status(403).json({ error: '无权操作该图片' }); return; }
|
|
const { x, y } = req.body ?? {};
|
|
const content = String(req.body?.content ?? '').trim();
|
|
if (typeof x !== 'number' || typeof y !== 'number' || x < 0 || x > 1 || y < 0 || y > 1) { res.status(400).json({ error: '批注坐标无效' }); return; }
|
|
if (!content) { res.status(400).json({ error: '批注内容不能为空' }); return; }
|
|
res.status(201).json(await annotationsRepository.create(imageId, { x, y, content, author_name: req.authUser?.display_name || 'API' }));
|
|
} catch (error) { next(error); }
|
|
});
|
|
|
|
export default router;
|