38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
/**
|
||
* Elements Routes – CRUD for elements
|
||
*/
|
||
import type { FastifyInstance } from 'fastify';
|
||
import type { DatabaseInterface, DBElement } from '../database/DatabaseInterface.js';
|
||
|
||
export function registerElementRoutes(fastify: FastifyInstance, db: DatabaseInterface) {
|
||
// List elements for a drawing
|
||
fastify.get('/api/drawings/:drawingId/elements', async (request) => {
|
||
const { drawingId } = request.params as { drawingId: string };
|
||
return db.listElements(drawingId);
|
||
});
|
||
|
||
// Create element
|
||
fastify.post('/api/drawings/:drawingId/elements', async (request, reply) => {
|
||
const { drawingId } = request.params as { drawingId: string };
|
||
const body = request.body as Partial<DBElement>;
|
||
return reply.code(201).send(db.createElement({ ...body, drawing_id: drawingId }));
|
||
});
|
||
|
||
// Update element
|
||
fastify.patch('/api/elements/:id', async (request, reply) => {
|
||
const { id } = request.params as { id: string };
|
||
const body = request.body as Partial<DBElement>;
|
||
const updated = db.updateElement(id, body);
|
||
if (!updated) return reply.code(404).send({ error: 'Element not found' });
|
||
return updated;
|
||
});
|
||
|
||
// Delete element
|
||
fastify.delete('/api/elements/:id', async (request, reply) => {
|
||
const { id } = request.params as { id: string };
|
||
const ok = db.deleteElement(id);
|
||
if (!ok) return reply.code(404).send({ error: 'Element not found' });
|
||
return reply.code(204).send();
|
||
});
|
||
}
|