All checks were successful
CI / ci (push) Successful in 15s
Run biome check --write --unsafe to fix tabs, import ordering, and non-null assertions across entire codebase. Disable a11y rules not applicable to this single-user app. Exclude auto-generated routeTree. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
import { zValidator } from "@hono/zod-validator";
|
|
import { Hono } from "hono";
|
|
import {
|
|
createSetupSchema,
|
|
syncSetupItemsSchema,
|
|
updateSetupSchema,
|
|
} from "../../shared/schemas.ts";
|
|
import {
|
|
createSetup,
|
|
deleteSetup,
|
|
getAllSetups,
|
|
getSetupWithItems,
|
|
removeSetupItem,
|
|
syncSetupItems,
|
|
updateSetup,
|
|
} from "../services/setup.service.ts";
|
|
|
|
type Env = { Variables: { db?: any } };
|
|
|
|
const app = new Hono<Env>();
|
|
|
|
// Setup CRUD
|
|
|
|
app.get("/", (c) => {
|
|
const db = c.get("db");
|
|
const setups = getAllSetups(db);
|
|
return c.json(setups);
|
|
});
|
|
|
|
app.post("/", zValidator("json", createSetupSchema), (c) => {
|
|
const db = c.get("db");
|
|
const data = c.req.valid("json");
|
|
const setup = createSetup(db, data);
|
|
return c.json(setup, 201);
|
|
});
|
|
|
|
app.get("/:id", (c) => {
|
|
const db = c.get("db");
|
|
const id = Number(c.req.param("id"));
|
|
const setup = getSetupWithItems(db, id);
|
|
if (!setup) return c.json({ error: "Setup not found" }, 404);
|
|
return c.json(setup);
|
|
});
|
|
|
|
app.put("/:id", zValidator("json", updateSetupSchema), (c) => {
|
|
const db = c.get("db");
|
|
const id = Number(c.req.param("id"));
|
|
const data = c.req.valid("json");
|
|
const setup = updateSetup(db, id, data);
|
|
if (!setup) return c.json({ error: "Setup not found" }, 404);
|
|
return c.json(setup);
|
|
});
|
|
|
|
app.delete("/:id", (c) => {
|
|
const db = c.get("db");
|
|
const id = Number(c.req.param("id"));
|
|
const deleted = deleteSetup(db, id);
|
|
if (!deleted) return c.json({ error: "Setup not found" }, 404);
|
|
return c.json({ success: true });
|
|
});
|
|
|
|
// Setup Items
|
|
|
|
app.put("/:id/items", zValidator("json", syncSetupItemsSchema), (c) => {
|
|
const db = c.get("db");
|
|
const id = Number(c.req.param("id"));
|
|
const { itemIds } = c.req.valid("json");
|
|
|
|
const setup = getSetupWithItems(db, id);
|
|
if (!setup) return c.json({ error: "Setup not found" }, 404);
|
|
|
|
syncSetupItems(db, id, itemIds);
|
|
return c.json({ success: true });
|
|
});
|
|
|
|
app.delete("/:id/items/:itemId", (c) => {
|
|
const db = c.get("db");
|
|
const setupId = Number(c.req.param("id"));
|
|
const itemId = Number(c.req.param("itemId"));
|
|
removeSetupItem(db, setupId, itemId);
|
|
return c.json({ success: true });
|
|
});
|
|
|
|
export { app as setupRoutes };
|