2026-06-26 10:50:24 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Layers Routes – CRUD for layers
|
|
|
|
|
|
*/
|
|
|
|
|
|
import type { FastifyInstance } from 'fastify';
|
|
|
|
|
|
import type { DatabaseInterface, DBLayer } 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 registerLayerRoutes(fastify: FastifyInstance, db: DatabaseInterface, authService: AuthService) {
|
2026-06-26 10:50:24 +02:00
|
|
|
|
// List layers for a drawing
|
2026-07-04 16:43:55 +02:00
|
|
|
|
fastify.get('/api/drawings/:drawingId/layers', async (request, reply) => {
|
|
|
|
|
|
if (!requireAuth(request, reply, authService)) return;
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const { drawingId } = request.params as { drawingId: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const idErr = validateIdParam(drawingId, 'drawingId');
|
|
|
|
|
|
if (idErr) return reply.code(400).send({ error: idErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
return db.listLayers(drawingId);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Create layer
|
|
|
|
|
|
fastify.post('/api/drawings/:drawingId/layers', 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 { drawingId } = request.params as { drawingId: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const idErr = validateIdParam(drawingId, 'drawingId');
|
|
|
|
|
|
if (idErr) return reply.code(400).send({ error: idErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const body = request.body as Partial<DBLayer>;
|
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.createLayer({ ...body, drawing_id: drawingId }));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Update layer
|
|
|
|
|
|
fastify.patch('/api/layers/: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<DBLayer>;
|
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.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) => {
|
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.deleteLayer(id);
|
|
|
|
|
|
if (!ok) return reply.code(404).send({ error: 'Layer not found' });
|
|
|
|
|
|
return reply.code(204).send();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|