2026-06-26 10:50:24 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Drawings Routes – CRUD for drawings
|
|
|
|
|
|
*/
|
|
|
|
|
|
import type { FastifyInstance } from 'fastify';
|
|
|
|
|
|
import type { DatabaseInterface, DBDrawing } from '../database/DatabaseInterface.js';
|
2026-07-04 16:43:55 +02:00
|
|
|
|
import { requireAuth } from '../auth/authMiddleware.js';
|
|
|
|
|
|
import type { AuthService } from '../auth/AuthService.js';
|
|
|
|
|
|
import { validateName, validateIdParam } from '../utils/validation.js';
|
2026-06-26 10:50:24 +02:00
|
|
|
|
|
2026-07-04 16:43:55 +02:00
|
|
|
|
export function registerDrawingRoutes(fastify: FastifyInstance, db: DatabaseInterface, authService: AuthService) {
|
2026-06-26 10:50:24 +02:00
|
|
|
|
// List drawings for a project
|
2026-07-04 16:43:55 +02:00
|
|
|
|
fastify.get('/api/projects/:projectId/drawings', async (request, reply) => {
|
|
|
|
|
|
if (!requireAuth(request, reply, authService)) return;
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const { projectId } = request.params as { projectId: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const idErr = validateIdParam(projectId, 'projectId');
|
|
|
|
|
|
if (idErr) return reply.code(400).send({ error: idErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
return db.listDrawings(projectId);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Get single drawing
|
|
|
|
|
|
fastify.get('/api/drawings/:id', async (request, reply) => {
|
2026-07-04 16:43:55 +02:00
|
|
|
|
if (!requireAuth(request, reply, authService)) return;
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const { id } = request.params as { id: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const idErr = validateIdParam(id, 'id');
|
|
|
|
|
|
if (idErr) return reply.code(400).send({ error: idErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const drawing = db.getDrawing(id);
|
|
|
|
|
|
if (!drawing) return reply.code(404).send({ error: 'Drawing not found' });
|
|
|
|
|
|
return drawing;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Create drawing
|
|
|
|
|
|
fastify.post('/api/projects/:projectId/drawings', async (request, reply) => {
|
2026-07-04 16:43:55 +02:00
|
|
|
|
if (!requireAuth(request, reply, authService)) return;
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const { projectId } = request.params as { projectId: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const idErr = validateIdParam(projectId, 'projectId');
|
|
|
|
|
|
if (idErr) return reply.code(400).send({ error: idErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const body = request.body as Partial<DBDrawing>;
|
2026-07-04 16:43:55 +02:00
|
|
|
|
if (body.name !== undefined) {
|
|
|
|
|
|
const nameErr = validateName(body.name);
|
|
|
|
|
|
if (nameErr) return reply.code(400).send({ error: nameErr });
|
|
|
|
|
|
}
|
2026-06-26 10:50:24 +02:00
|
|
|
|
return reply.code(201).send(db.createDrawing({ ...body, project_id: projectId }));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Update drawing
|
|
|
|
|
|
fastify.patch('/api/drawings/:id', async (request, reply) => {
|
2026-07-04 16:43:55 +02:00
|
|
|
|
if (!requireAuth(request, reply, authService)) return;
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const { id } = request.params as { id: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const idErr = validateIdParam(id, 'id');
|
|
|
|
|
|
if (idErr) return reply.code(400).send({ error: idErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const body = request.body as Partial<DBDrawing>;
|
2026-07-04 16:43:55 +02:00
|
|
|
|
if (body.name !== undefined) {
|
|
|
|
|
|
const nameErr = validateName(body.name);
|
|
|
|
|
|
if (nameErr) return reply.code(400).send({ error: nameErr });
|
|
|
|
|
|
}
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const updated = db.updateDrawing(id, body);
|
|
|
|
|
|
if (!updated) return reply.code(404).send({ error: 'Drawing not found' });
|
|
|
|
|
|
return updated;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Delete drawing
|
|
|
|
|
|
fastify.delete('/api/drawings/:id', async (request, reply) => {
|
2026-07-04 16:43:55 +02:00
|
|
|
|
if (!requireAuth(request, reply, authService)) return;
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const { id } = request.params as { id: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const idErr = validateIdParam(id, 'id');
|
|
|
|
|
|
if (idErr) return reply.code(400).send({ error: idErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const ok = db.deleteDrawing(id);
|
|
|
|
|
|
if (!ok) return reply.code(404).send({ error: 'Drawing not found' });
|
|
|
|
|
|
return reply.code(204).send();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|