feat(01-02): add Hono API routes with validation, image upload, and integration tests
- Item routes: GET, POST, PUT, DELETE with Zod validation and image cleanup - Category routes: GET, POST, PUT, DELETE with Uncategorized protection - Totals route: per-category and global aggregates - Image upload: multipart file handling with type/size validation - Routes use DI via Hono context variables for testability - Integration tests: 10 tests covering all endpoints and edge cases - All 30 tests passing Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
59
src/server/routes/categories.ts
Normal file
59
src/server/routes/categories.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
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<Env>();
|
||||
|
||||
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 };
|
||||
Reference in New Issue
Block a user