import { schemas } from '../schemas.js'; import { translateSqliteError } from '../sqlite-errors.js'; export default async function hostsRoutes(fastify) { const { db } = fastify; fastify.get('/', { schema: { querystring: { type: 'object', properties: { q: { type: 'string' } }, }, response: { 200: { type: 'array', items: schemas.hostResponse }, }, }, }, async (req) => { const q = (req.query.q ?? '').trim(); return q ? db.hosts.search(q) : db.hosts.list(); }); fastify.get('/by-hardware-id/:hardwareId', { schema: { params: { type: 'object', required: ['hardwareId'], properties: { hardwareId: { type: 'string', minLength: 1 } }, }, response: { 200: schemas.hostResponse, 404: schemas.errorResponse }, }, }, async (req) => { const host = db.hosts.getByHardwareId(req.params.hardwareId); if (!host) throw fastify.httpErrors.notFound('host not found'); return host; }); fastify.get('/:id', { schema: { params: schemas.idParam, response: { 200: schemas.hostResponse, 404: schemas.errorResponse }, }, }, async (req) => { const host = db.hosts.get(req.params.id); if (!host) throw fastify.httpErrors.notFound('host not found'); return host; }); fastify.post('/', { schema: { body: schemas.hostBody, response: { 201: schemas.hostResponse }, }, }, async (req, reply) => { try { const host = db.hosts.create(req.body); reply.code(201); return host; } catch (err) { translateSqliteError(err, fastify); } }); fastify.put('/:id', { schema: { params: schemas.idParam, body: schemas.hostBody, response: { 200: schemas.hostResponse, 404: schemas.errorResponse }, }, }, async (req) => { try { const host = db.hosts.update(req.params.id, req.body); if (!host) throw fastify.httpErrors.notFound('host not found'); return host; } catch (err) { if (err.statusCode) throw err; translateSqliteError(err, fastify); } }); fastify.delete('/:id', { schema: { params: schemas.idParam }, }, async (req, reply) => { const removed = db.hosts.delete(req.params.id); if (!removed) throw fastify.httpErrors.notFound('host not found'); reply.code(204); return null; }); }