46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
|
|
/**
|
|||
|
|
* Drawings Routes – CRUD for drawings
|
|||
|
|
*/
|
|||
|
|
import type { FastifyInstance } from 'fastify';
|
|||
|
|
import type { DatabaseInterface, DBDrawing } from '../database/DatabaseInterface.js';
|
|||
|
|
|
|||
|
|
export function registerDrawingRoutes(fastify: FastifyInstance, db: DatabaseInterface) {
|
|||
|
|
// List drawings for a project
|
|||
|
|
fastify.get('/api/projects/:projectId/drawings', async (request) => {
|
|||
|
|
const { projectId } = request.params as { projectId: string };
|
|||
|
|
return db.listDrawings(projectId);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Get single drawing
|
|||
|
|
fastify.get('/api/drawings/:id', async (request, reply) => {
|
|||
|
|
const { id } = request.params as { id: string };
|
|||
|
|
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) => {
|
|||
|
|
const { projectId } = request.params as { projectId: string };
|
|||
|
|
const body = request.body as Partial<DBDrawing>;
|
|||
|
|
return reply.code(201).send(db.createDrawing({ ...body, project_id: projectId }));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Update drawing
|
|||
|
|
fastify.patch('/api/drawings/:id', async (request, reply) => {
|
|||
|
|
const { id } = request.params as { id: string };
|
|||
|
|
const body = request.body as Partial<DBDrawing>;
|
|||
|
|
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) => {
|
|||
|
|
const { id } = request.params as { id: string };
|
|||
|
|
const ok = db.deleteDrawing(id);
|
|||
|
|
if (!ok) return reply.code(404).send({ error: 'Drawing not found' });
|
|||
|
|
return reply.code(204).send();
|
|||
|
|
});
|
|||
|
|
}
|