39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
/**
|
||
* Blocks Routes – CRUD for reusable drawing blocks
|
||
*/
|
||
import type { FastifyInstance } from 'fastify';
|
||
import type { DatabaseInterface, DBBlock } from '../database/DatabaseInterface.js';
|
||
|
||
export function registerBlockRoutes(fastify: FastifyInstance, db: DatabaseInterface) {
|
||
// List all blocks for a drawing
|
||
fastify.get('/api/drawings/:drawingId/blocks', async (request) => {
|
||
const { drawingId } = request.params as { drawingId: string };
|
||
return db.listBlocks(drawingId);
|
||
});
|
||
|
||
// Create a new block
|
||
fastify.post('/api/drawings/:drawingId/blocks', async (request, reply) => {
|
||
const { drawingId } = request.params as { drawingId: string };
|
||
const body = request.body as Partial<DBBlock>;
|
||
if (!body.name) return reply.code(400).send({ error: 'Name is required' });
|
||
return reply.code(201).send(db.createBlock({ ...body, drawing_id: drawingId }));
|
||
});
|
||
|
||
// Update a block
|
||
fastify.patch('/api/blocks/:id', async (request, reply) => {
|
||
const { id } = request.params as { id: string };
|
||
const body = request.body as Partial<DBBlock>;
|
||
const updated = db.updateBlock(id, body);
|
||
if (!updated) return reply.code(404).send({ error: 'Block not found' });
|
||
return updated;
|
||
});
|
||
|
||
// Delete a block
|
||
fastify.delete('/api/blocks/:id', async (request, reply) => {
|
||
const { id } = request.params as { id: string };
|
||
const ok = db.deleteBlock(id);
|
||
if (!ok) return reply.code(404).send({ error: 'Block not found' });
|
||
return reply.code(204).send();
|
||
});
|
||
}
|