f500db971b
build-and-push / build-and-push (push) Successful in 1m26s
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>
58 lines
1.9 KiB
JavaScript
58 lines
1.9 KiB
JavaScript
import { schemas } from '../schemas.js';
|
|
import { translateSqliteError } from '../sqlite-errors.js';
|
|
|
|
export default async function sitesRoutes(fastify) {
|
|
const { db } = fastify;
|
|
|
|
fastify.get('/', {
|
|
schema: { response: { 200: { type: 'array', items: schemas.lookupResponse } } },
|
|
}, async () => db.sites.list());
|
|
|
|
fastify.get('/:id', {
|
|
schema: { params: schemas.idParam, response: { 200: schemas.lookupResponse } },
|
|
}, async (req) => {
|
|
const row = db.sites.get(req.params.id);
|
|
if (!row) throw fastify.httpErrors.notFound('site not found');
|
|
return row;
|
|
});
|
|
|
|
fastify.post('/', {
|
|
schema: { body: schemas.lookupBody, response: { 201: schemas.lookupResponse } },
|
|
}, async (req, reply) => {
|
|
try {
|
|
const row = db.sites.create(req.body.name);
|
|
reply.code(201);
|
|
return row;
|
|
} catch (err) {
|
|
translateSqliteError(err, fastify, { uniqueMessage: 'a site with that name already exists' });
|
|
}
|
|
});
|
|
|
|
fastify.put('/:id', {
|
|
schema: { params: schemas.idParam, body: schemas.lookupBody, response: { 200: schemas.lookupResponse } },
|
|
}, async (req) => {
|
|
try {
|
|
const row = db.sites.update(req.params.id, req.body.name);
|
|
if (!row) throw fastify.httpErrors.notFound('site not found');
|
|
return row;
|
|
} catch (err) {
|
|
if (err.statusCode) throw err;
|
|
translateSqliteError(err, fastify, { uniqueMessage: 'a site with that name already exists' });
|
|
}
|
|
});
|
|
|
|
fastify.delete('/:id', {
|
|
schema: { params: schemas.idParam },
|
|
}, async (req, reply) => {
|
|
try {
|
|
const removed = db.sites.delete(req.params.id);
|
|
if (!removed) throw fastify.httpErrors.notFound('site not found');
|
|
reply.code(204);
|
|
return null;
|
|
} catch (err) {
|
|
if (err.statusCode) throw err;
|
|
translateSqliteError(err, fastify, { foreignKeyMessage: 'cannot delete: rooms still reference this site' });
|
|
}
|
|
});
|
|
}
|