Files
Infrastructure/src/routes/rooms.js
T
josh f500db971b
build-and-push / build-and-push (push) Successful in 1m26s
Initial commit: Infrastructure host tracking app
Fastify + node:sqlite single-process app with vanilla JS UI for
looking up hosts by hardware ID, hostname, or asset ID. Includes
per-host network interface tracking, sites/rooms/server-types CRUD,
Docker packaging, and a Gitea Actions workflow that runs tests then
builds and pushes to gitea.thewrightserver.net/josh/infrastructure.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-19 17:05:50 -04:00

70 lines
2.2 KiB
JavaScript

import { schemas } from '../schemas.js';
import { translateSqliteError } from '../sqlite-errors.js';
export default async function roomsRoutes(fastify) {
const { db } = fastify;
fastify.get('/', {
schema: {
querystring: {
type: 'object',
properties: { site_id: { type: 'integer', minimum: 1 } },
},
response: { 200: { type: 'array', items: schemas.roomResponse } },
},
}, async (req) => db.rooms.list(req.query.site_id));
fastify.get('/:id', {
schema: { params: schemas.idParam, response: { 200: schemas.roomResponse } },
}, async (req) => {
const row = db.rooms.get(req.params.id);
if (!row) throw fastify.httpErrors.notFound('room not found');
return row;
});
fastify.post('/', {
schema: { body: schemas.roomBody, response: { 201: schemas.roomResponse } },
}, async (req, reply) => {
try {
const row = db.rooms.create(req.body.site_id, req.body.name);
reply.code(201);
return row;
} catch (err) {
translateSqliteError(err, fastify, {
uniqueMessage: 'a room with that name already exists at this site',
foreignKeyMessage: 'site does not exist',
});
}
});
fastify.put('/:id', {
schema: { params: schemas.idParam, body: schemas.roomBody, response: { 200: schemas.roomResponse } },
}, async (req) => {
try {
const row = db.rooms.update(req.params.id, req.body.site_id, req.body.name);
if (!row) throw fastify.httpErrors.notFound('room not found');
return row;
} catch (err) {
if (err.statusCode) throw err;
translateSqliteError(err, fastify, {
uniqueMessage: 'a room with that name already exists at this site',
foreignKeyMessage: 'site does not exist',
});
}
});
fastify.delete('/:id', {
schema: { params: schemas.idParam },
}, async (req, reply) => {
try {
const removed = db.rooms.delete(req.params.id);
if (!removed) throw fastify.httpErrors.notFound('room not found');
reply.code(204);
return null;
} catch (err) {
if (err.statusCode) throw err;
translateSqliteError(err, fastify, { foreignKeyMessage: 'cannot delete: hosts still reference this room' });
}
});
}