import { Hono } from "hono"; import { zValidator } from "@hono/zod-validator"; import { createCategorySchema, updateCategorySchema, } from "../../shared/schemas.ts"; import { getAllCategories, createCategory, updateCategory, deleteCategory, } from "../services/category.service.ts"; type Env = { Variables: { db?: any } }; const app = new Hono(); app.get("/", (c) => { const db = c.get("db"); const cats = getAllCategories(db); return c.json(cats); }); app.post("/", zValidator("json", createCategorySchema), (c) => { const db = c.get("db"); const data = c.req.valid("json"); const cat = createCategory(db, data); return c.json(cat, 201); }); app.put( "/:id", zValidator("json", updateCategorySchema.omit({ id: true })), (c) => { const db = c.get("db"); const id = Number(c.req.param("id")); const data = c.req.valid("json"); const cat = updateCategory(db, id, data); if (!cat) return c.json({ error: "Category not found" }, 404); return c.json(cat); }, ); app.delete("/:id", (c) => { const db = c.get("db"); const id = Number(c.req.param("id")); const result = deleteCategory(db, id); if (!result.success) { if (result.error === "Cannot delete the Uncategorized category") { return c.json({ error: result.error }, 400); } return c.json({ error: result.error }, 404); } return c.json({ success: true }); }); export { app as categoryRoutes };