38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
|
|
/**
|
|||
|
|
* Layers Routes – CRUD for layers
|
|||
|
|
*/
|
|||
|
|
import type { FastifyInstance } from 'fastify';
|
|||
|
|
import type { DatabaseInterface, DBLayer } from '../database/DatabaseInterface.js';
|
|||
|
|
|
|||
|
|
export function registerLayerRoutes(fastify: FastifyInstance, db: DatabaseInterface) {
|
|||
|
|
// List layers for a drawing
|
|||
|
|
fastify.get('/api/drawings/:drawingId/layers', async (request) => {
|
|||
|
|
const { drawingId } = request.params as { drawingId: string };
|
|||
|
|
return db.listLayers(drawingId);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Create layer
|
|||
|
|
fastify.post('/api/drawings/:drawingId/layers', async (request, reply) => {
|
|||
|
|
const { drawingId } = request.params as { drawingId: string };
|
|||
|
|
const body = request.body as Partial<DBLayer>;
|
|||
|
|
return reply.code(201).send(db.createLayer({ ...body, drawing_id: drawingId }));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Update layer
|
|||
|
|
fastify.patch('/api/layers/:id', async (request, reply) => {
|
|||
|
|
const { id } = request.params as { id: string };
|
|||
|
|
const body = request.body as Partial<DBLayer>;
|
|||
|
|
const updated = db.updateLayer(id, body);
|
|||
|
|
if (!updated) return reply.code(404).send({ error: 'Layer not found' });
|
|||
|
|
return updated;
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Delete layer
|
|||
|
|
fastify.delete('/api/layers/:id', async (request, reply) => {
|
|||
|
|
const { id } = request.params as { id: string };
|
|||
|
|
const ok = db.deleteLayer(id);
|
|||
|
|
if (!ok) return reply.code(404).send({ error: 'Layer not found' });
|
|||
|
|
return reply.code(204).send();
|
|||
|
|
});
|
|||
|
|
}
|