import { zValidator } from "@hono/zod-validator"; import { Hono } from "hono"; import { createSetupSchema, syncSetupItemsSchema, updateClassificationSchema, updateSetupSchema, } from "../../shared/schemas.ts"; import { parseId } from "../lib/params.ts"; import { createSetup, deleteSetup, getAllSetups, getSetupWithItems, removeSetupItem, syncSetupItems, updateItemClassification, updateSetup, } from "../services/setup.service.ts"; type Env = { Variables: { db?: any } }; const app = new Hono(); // 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 = parseId(c.req.param("id")); if (!id) return c.json({ error: "Invalid setup ID" }, 400); 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 = parseId(c.req.param("id")); if (!id) return c.json({ error: "Invalid setup ID" }, 400); 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 = parseId(c.req.param("id")); if (!id) return c.json({ error: "Invalid setup ID" }, 400); 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 = parseId(c.req.param("id")); if (!id) return c.json({ error: "Invalid setup ID" }, 400); 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.patch( "/:id/items/:itemId/classification", zValidator("json", updateClassificationSchema), (c) => { const db = c.get("db"); const setupId = parseId(c.req.param("id")); const itemId = parseId(c.req.param("itemId")); if (!setupId || !itemId) return c.json({ error: "Invalid ID" }, 400); const { classification } = c.req.valid("json"); updateItemClassification(db, setupId, itemId, classification); return c.json({ success: true }); }, ); app.delete("/:id/items/:itemId", (c) => { const db = c.get("db"); const setupId = parseId(c.req.param("id")); const itemId = parseId(c.req.param("itemId")); if (!setupId || !itemId) return c.json({ error: "Invalid ID" }, 400); removeSetupItem(db, setupId, itemId); return c.json({ success: true }); }); export { app as setupRoutes };