- Add PATCH /:id/items/:itemId/classification endpoint with Zod validation - Add apiPatch helper to client API library - Add useUpdateItemClassification mutation hook - Add classification field to SetupItemWithCategory interface - Create ClassificationBadge click-to-cycle component (base/worn/consumable) - Wire ClassificationBadge into setup detail page item grid - Add integration tests for PATCH classification route (valid + invalid) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
100 lines
2.5 KiB
TypeScript
100 lines
2.5 KiB
TypeScript
import { zValidator } from "@hono/zod-validator";
|
|
import { Hono } from "hono";
|
|
import {
|
|
createSetupSchema,
|
|
syncSetupItemsSchema,
|
|
updateClassificationSchema,
|
|
updateSetupSchema,
|
|
} from "../../shared/schemas.ts";
|
|
import {
|
|
createSetup,
|
|
deleteSetup,
|
|
getAllSetups,
|
|
getSetupWithItems,
|
|
removeSetupItem,
|
|
syncSetupItems,
|
|
updateItemClassification,
|
|
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.patch(
|
|
"/:id/items/:itemId/classification",
|
|
zValidator("json", updateClassificationSchema),
|
|
(c) => {
|
|
const db = c.get("db");
|
|
const setupId = Number(c.req.param("id"));
|
|
const itemId = Number(c.req.param("itemId"));
|
|
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 = 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 };
|