task(E6-backend): guests CRUD - schema, adapter, routes
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Guests Routes – CRUD for event guests (Task E6)
|
||||
*/
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { DatabaseInterface } from '../database/DatabaseInterface.js';
|
||||
import { requireAuth } from '../auth/authMiddleware.js';
|
||||
import type { AuthService } from '../auth/AuthService.js';
|
||||
import { validateIdParam } from '../utils/validation.js';
|
||||
|
||||
export function registerGuestRoutes(fastify: FastifyInstance, db: DatabaseInterface, authService: AuthService) {
|
||||
// List guests for a drawing
|
||||
fastify.get('/api/drawings/:drawingId/guests', async (request, reply) => {
|
||||
if (!requireAuth(request, reply, authService)) return;
|
||||
const { drawingId } = request.params as { drawingId: string };
|
||||
const idErr = validateIdParam(drawingId, 'drawingId');
|
||||
if (idErr) return reply.code(400).send({ error: idErr });
|
||||
return db.listGuests(drawingId);
|
||||
});
|
||||
|
||||
// Create guest
|
||||
fastify.post('/api/drawings/:drawingId/guests', async (request, reply) => {
|
||||
if (!requireAuth(request, reply, authService)) return;
|
||||
const { drawingId } = request.params as { drawingId: string };
|
||||
const idErr = validateIdParam(drawingId, 'drawingId');
|
||||
if (idErr) return reply.code(400).send({ error: idErr });
|
||||
const body = request.body as { name?: string; email?: string; category?: string; seat_element_id?: string; notes?: string };
|
||||
if (!body.name || typeof body.name !== 'string' || body.name.trim() === '') {
|
||||
return reply.code(400).send({ error: 'name is required' });
|
||||
}
|
||||
const guest = db.createGuest({
|
||||
drawing_id: drawingId,
|
||||
name: body.name.trim(),
|
||||
email: body.email,
|
||||
category: body.category ?? 'standard',
|
||||
seat_element_id: body.seat_element_id,
|
||||
notes: body.notes,
|
||||
});
|
||||
return reply.code(201).send(guest);
|
||||
});
|
||||
|
||||
// Update guest
|
||||
fastify.patch('/api/guests/:id', async (request, reply) => {
|
||||
if (!requireAuth(request, reply, authService)) return;
|
||||
const { id } = request.params as { id: string };
|
||||
const idErr = validateIdParam(id, 'id');
|
||||
if (idErr) return reply.code(400).send({ error: idErr });
|
||||
const body = request.body as Partial<{ name: string; email: string; category: string; seat_element_id: string; notes: string }>;
|
||||
const updated = db.updateGuest(id, body);
|
||||
if (!updated) return reply.code(404).send({ error: 'Guest not found' });
|
||||
return updated;
|
||||
});
|
||||
|
||||
// Delete guest
|
||||
fastify.delete('/api/guests/:id', async (request, reply) => {
|
||||
if (!requireAuth(request, reply, authService)) return;
|
||||
const { id } = request.params as { id: string };
|
||||
const idErr = validateIdParam(id, 'id');
|
||||
if (idErr) return reply.code(400).send({ error: idErr });
|
||||
const ok = db.deleteGuest(id);
|
||||
if (!ok) return reply.code(404).send({ error: 'Guest not found' });
|
||||
return reply.code(204).send();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user