feat(38-01): admin tag routes + route registration + integration tests

- Create admin-tags.ts with GET list, GET single, POST, PUT (cycle guard → 400), DELETE
- Register /tags route in admin.ts
- Add 13-test integration suite covering CRUD, cycle detection, orphan behavior
This commit is contained in:
2026-04-19 22:28:02 +02:00
parent 8cefdf625b
commit 311ebe8afe
3 changed files with 267 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";
import { parseId } from "../lib/params.ts";
import {
createTag,
deleteTag,
getAdminTags,
getTagWithCounts,
updateTag,
} from "../services/tag.service.ts";
type Env = { Variables: { db?: any; userId?: number } };
const app = new Hono<Env>();
const createTagSchema = z.object({
name: z.string().min(1),
parentId: z.number().int().positive().nullable().optional(),
});
const updateTagSchema = z.object({
name: z.string().min(1).optional(),
parentId: z.number().int().positive().nullable().optional(),
});
// GET /api/admin/tags — list all tags with parentId and itemCount
app.get("/", async (c) => {
const db = c.get("db");
const result = await getAdminTags(db);
return c.json(result);
});
// GET /api/admin/tags/:id — single tag with counts
app.get("/:id", async (c) => {
const db = c.get("db");
const id = parseId(c.req.param("id"));
if (!id) return c.json({ error: "Invalid tag ID" }, 400);
const tag = await getTagWithCounts(db, id);
if (!tag) return c.json({ error: "Tag not found" }, 404);
return c.json(tag);
});
// POST /api/admin/tags — create a new tag
app.post("/", zValidator("json", createTagSchema), async (c) => {
const db = c.get("db");
const data = c.req.valid("json");
const tag = await createTag(db, data);
return c.json(tag, 201);
});
// PUT /api/admin/tags/:id — rename and/or reparent a tag
app.put("/:id", zValidator("json", updateTagSchema), async (c) => {
const db = c.get("db");
const id = parseId(c.req.param("id"));
if (!id) return c.json({ error: "Invalid tag ID" }, 400);
const data = c.req.valid("json");
try {
const tag = await updateTag(db, id, data);
if (!tag) return c.json({ error: "Tag not found" }, 404);
return c.json(tag);
} catch (err) {
if (err instanceof Error && err.message.startsWith("Cycle detected")) {
return c.json({ error: err.message }, 400);
}
throw err;
}
});
// DELETE /api/admin/tags/:id — remove tag (children become top-level via ON DELETE SET NULL)
app.delete("/:id", async (c) => {
const db = c.get("db");
const id = parseId(c.req.param("id"));
if (!id) return c.json({ error: "Invalid tag ID" }, 400);
const deleted = await deleteTag(db, id);
if (!deleted) return c.json({ error: "Tag not found" }, 404);
return c.json({ success: true });
});
export { app as adminTagRoutes };

View File

@@ -1,6 +1,7 @@
import { Hono } from "hono";
import { requireAdmin, requireAuth } from "../middleware/auth.ts";
import { adminItemRoutes } from "./admin-items.ts";
import { adminTagRoutes } from "./admin-tags.ts";
type Env = { Variables: { db?: any; userId?: number } };
@@ -17,4 +18,7 @@ app.get("/", async (c) => {
// Admin item management
app.route("/items", adminItemRoutes);
// Admin tag management
app.route("/tags", adminTagRoutes);
export { app as adminRoutes };