feat(14-04): convert data route handlers to async/await
- Add await before all service calls in items, categories, threads, setups, totals routes - Make all handler callbacks async - Covers getAllItems, createItem, updateItem, deleteItem, duplicateItem, getAllCategories, createCategory, updateCategory, deleteCategory, getAllThreads, getThreadWithCandidates, createThread, updateThread, deleteThread, resolveThread, createCandidate, updateCandidate, deleteCandidate, reorderCandidates, getAllSetups, getSetupWithItems, createSetup, updateSetup, deleteSetup, syncSetupItems, updateItemClassification, removeSetupItem, getCategoryTotals, getGlobalTotals, exportItemsCsv, importItemsCsv
This commit is contained in:
@@ -16,38 +16,38 @@ type Env = { Variables: { db?: any } };
|
||||
|
||||
const app = new Hono<Env>();
|
||||
|
||||
app.get("/", (c) => {
|
||||
app.get("/", async (c) => {
|
||||
const db = c.get("db");
|
||||
const cats = getAllCategories(db);
|
||||
const cats = await getAllCategories(db);
|
||||
return c.json(cats);
|
||||
});
|
||||
|
||||
app.post("/", zValidator("json", createCategorySchema), (c) => {
|
||||
app.post("/", zValidator("json", createCategorySchema), async (c) => {
|
||||
const db = c.get("db");
|
||||
const data = c.req.valid("json");
|
||||
const cat = createCategory(db, data);
|
||||
const cat = await createCategory(db, data);
|
||||
return c.json(cat, 201);
|
||||
});
|
||||
|
||||
app.put(
|
||||
"/:id",
|
||||
zValidator("json", updateCategorySchema.omit({ id: true })),
|
||||
(c) => {
|
||||
async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid category ID" }, 400);
|
||||
const data = c.req.valid("json");
|
||||
const cat = updateCategory(db, id, data);
|
||||
const cat = await updateCategory(db, id, data);
|
||||
if (!cat) return c.json({ error: "Category not found" }, 404);
|
||||
return c.json(cat);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete("/:id", (c) => {
|
||||
app.delete("/:id", async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid category ID" }, 400);
|
||||
const result = deleteCategory(db, id);
|
||||
const result = await deleteCategory(db, id);
|
||||
|
||||
if (!result.success) {
|
||||
if (result.error === "Cannot delete the Uncategorized category") {
|
||||
|
||||
@@ -18,9 +18,9 @@ type Env = { Variables: { db?: any } };
|
||||
|
||||
const app = new Hono<Env>();
|
||||
|
||||
app.get("/export", (c) => {
|
||||
app.get("/export", async (c) => {
|
||||
const db = c.get("db");
|
||||
const csv = exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db);
|
||||
c.header("Content-Type", "text/csv");
|
||||
c.header("Content-Disposition", 'attachment; filename="gearbox-export.csv"');
|
||||
return c.body(csv);
|
||||
@@ -35,51 +35,51 @@ app.post("/import", async (c) => {
|
||||
return c.json({ error: "No CSV file provided" }, 400);
|
||||
}
|
||||
const content = await file.text();
|
||||
const result = importItemsCsv(db, content);
|
||||
const result = await importItemsCsv(db, content);
|
||||
return c.json(result);
|
||||
});
|
||||
|
||||
app.get("/", (c) => {
|
||||
app.get("/", async (c) => {
|
||||
const db = c.get("db");
|
||||
const items = getAllItems(db);
|
||||
const items = await getAllItems(db);
|
||||
return c.json(items);
|
||||
});
|
||||
|
||||
app.get("/:id", (c) => {
|
||||
app.get("/:id", async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid item ID" }, 400);
|
||||
const item = getItemById(db, id);
|
||||
const item = await getItemById(db, id);
|
||||
if (!item) return c.json({ error: "Item not found" }, 404);
|
||||
return c.json(item);
|
||||
});
|
||||
|
||||
app.post("/", zValidator("json", createItemSchema), (c) => {
|
||||
app.post("/", zValidator("json", createItemSchema), async (c) => {
|
||||
const db = c.get("db");
|
||||
const data = c.req.valid("json");
|
||||
const item = createItem(db, data);
|
||||
const item = await createItem(db, data);
|
||||
return c.json(item, 201);
|
||||
});
|
||||
|
||||
app.put(
|
||||
"/:id",
|
||||
zValidator("json", updateItemSchema.omit({ id: true })),
|
||||
(c) => {
|
||||
async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid item ID" }, 400);
|
||||
const data = c.req.valid("json");
|
||||
const item = updateItem(db, id, data);
|
||||
const item = await updateItem(db, id, data);
|
||||
if (!item) return c.json({ error: "Item not found" }, 404);
|
||||
return c.json(item);
|
||||
},
|
||||
);
|
||||
|
||||
app.post("/:id/duplicate", (c) => {
|
||||
app.post("/:id/duplicate", async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid item ID" }, 400);
|
||||
const newItem = duplicateItem(db, id);
|
||||
const newItem = await duplicateItem(db, id);
|
||||
if (!newItem) return c.json({ error: "Item not found" }, 404);
|
||||
return c.json(newItem, 201);
|
||||
});
|
||||
@@ -88,7 +88,7 @@ app.delete("/:id", async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid item ID" }, 400);
|
||||
const deleted = deleteItem(db, id);
|
||||
const deleted = await deleteItem(db, id);
|
||||
if (!deleted) return c.json({ error: "Item not found" }, 404);
|
||||
|
||||
// Clean up image file if exists
|
||||
|
||||
@@ -24,82 +24,82 @@ const app = new Hono<Env>();
|
||||
|
||||
// Setup CRUD
|
||||
|
||||
app.get("/", (c) => {
|
||||
app.get("/", async (c) => {
|
||||
const db = c.get("db");
|
||||
const setups = getAllSetups(db);
|
||||
const setups = await getAllSetups(db);
|
||||
return c.json(setups);
|
||||
});
|
||||
|
||||
app.post("/", zValidator("json", createSetupSchema), (c) => {
|
||||
app.post("/", zValidator("json", createSetupSchema), async (c) => {
|
||||
const db = c.get("db");
|
||||
const data = c.req.valid("json");
|
||||
const setup = createSetup(db, data);
|
||||
const setup = await createSetup(db, data);
|
||||
return c.json(setup, 201);
|
||||
});
|
||||
|
||||
app.get("/:id", (c) => {
|
||||
app.get("/:id", async (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);
|
||||
const setup = await getSetupWithItems(db, id);
|
||||
if (!setup) return c.json({ error: "Setup not found" }, 404);
|
||||
return c.json(setup);
|
||||
});
|
||||
|
||||
app.put("/:id", zValidator("json", updateSetupSchema), (c) => {
|
||||
app.put("/:id", zValidator("json", updateSetupSchema), async (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);
|
||||
const setup = await updateSetup(db, id, data);
|
||||
if (!setup) return c.json({ error: "Setup not found" }, 404);
|
||||
return c.json(setup);
|
||||
});
|
||||
|
||||
app.delete("/:id", (c) => {
|
||||
app.delete("/:id", async (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);
|
||||
const deleted = await 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) => {
|
||||
app.put("/:id/items", zValidator("json", syncSetupItemsSchema), async (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);
|
||||
const setup = await getSetupWithItems(db, id);
|
||||
if (!setup) return c.json({ error: "Setup not found" }, 404);
|
||||
|
||||
syncSetupItems(db, id, itemIds);
|
||||
await syncSetupItems(db, id, itemIds);
|
||||
return c.json({ success: true });
|
||||
});
|
||||
|
||||
app.patch(
|
||||
"/:id/items/:itemId/classification",
|
||||
zValidator("json", updateClassificationSchema),
|
||||
(c) => {
|
||||
async (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);
|
||||
await updateItemClassification(db, setupId, itemId, classification);
|
||||
return c.json({ success: true });
|
||||
},
|
||||
);
|
||||
|
||||
app.delete("/:id/items/:itemId", (c) => {
|
||||
app.delete("/:id/items/:itemId", async (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);
|
||||
await removeSetupItem(db, setupId, itemId);
|
||||
return c.json({ success: true });
|
||||
});
|
||||
|
||||
|
||||
@@ -30,35 +30,35 @@ const app = new Hono<Env>();
|
||||
|
||||
// Thread CRUD
|
||||
|
||||
app.get("/", (c) => {
|
||||
app.get("/", async (c) => {
|
||||
const db = c.get("db");
|
||||
const includeResolved = c.req.query("includeResolved") === "true";
|
||||
const threads = getAllThreads(db, includeResolved);
|
||||
const threads = await getAllThreads(db, includeResolved);
|
||||
return c.json(threads);
|
||||
});
|
||||
|
||||
app.post("/", zValidator("json", createThreadSchema), (c) => {
|
||||
app.post("/", zValidator("json", createThreadSchema), async (c) => {
|
||||
const db = c.get("db");
|
||||
const data = c.req.valid("json");
|
||||
const thread = createThread(db, data);
|
||||
const thread = await createThread(db, data);
|
||||
return c.json(thread, 201);
|
||||
});
|
||||
|
||||
app.get("/:id", (c) => {
|
||||
app.get("/:id", async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid thread ID" }, 400);
|
||||
const thread = getThreadWithCandidates(db, id);
|
||||
const thread = await getThreadWithCandidates(db, id);
|
||||
if (!thread) return c.json({ error: "Thread not found" }, 404);
|
||||
return c.json(thread);
|
||||
});
|
||||
|
||||
app.put("/:id", zValidator("json", updateThreadSchema), (c) => {
|
||||
app.put("/:id", zValidator("json", updateThreadSchema), async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid thread ID" }, 400);
|
||||
const data = c.req.valid("json");
|
||||
const thread = updateThread(db, id, data);
|
||||
const thread = await updateThread(db, id, data);
|
||||
if (!thread) return c.json({ error: "Thread not found" }, 404);
|
||||
return c.json(thread);
|
||||
});
|
||||
@@ -67,7 +67,7 @@ app.delete("/:id", async (c) => {
|
||||
const db = c.get("db");
|
||||
const id = parseId(c.req.param("id"));
|
||||
if (!id) return c.json({ error: "Invalid thread ID" }, 400);
|
||||
const deleted = deleteThread(db, id);
|
||||
const deleted = await deleteThread(db, id);
|
||||
if (!deleted) return c.json({ error: "Thread not found" }, 404);
|
||||
|
||||
// Clean up candidate image files
|
||||
@@ -84,29 +84,29 @@ app.delete("/:id", async (c) => {
|
||||
|
||||
// Candidate CRUD (nested under thread)
|
||||
|
||||
app.post("/:id/candidates", zValidator("json", createCandidateSchema), (c) => {
|
||||
app.post("/:id/candidates", zValidator("json", createCandidateSchema), async (c) => {
|
||||
const db = c.get("db");
|
||||
const threadId = parseId(c.req.param("id"));
|
||||
if (!threadId) return c.json({ error: "Invalid thread ID" }, 400);
|
||||
|
||||
// Verify thread exists
|
||||
const thread = getThreadWithCandidates(db, threadId);
|
||||
const thread = await getThreadWithCandidates(db, threadId);
|
||||
if (!thread) return c.json({ error: "Thread not found" }, 404);
|
||||
|
||||
const data = c.req.valid("json");
|
||||
const candidate = createCandidate(db, threadId, data);
|
||||
const candidate = await createCandidate(db, threadId, data);
|
||||
return c.json(candidate, 201);
|
||||
});
|
||||
|
||||
app.put(
|
||||
"/:threadId/candidates/:candidateId",
|
||||
zValidator("json", updateCandidateSchema),
|
||||
(c) => {
|
||||
async (c) => {
|
||||
const db = c.get("db");
|
||||
const candidateId = parseId(c.req.param("candidateId"));
|
||||
if (!candidateId) return c.json({ error: "Invalid candidate ID" }, 400);
|
||||
const data = c.req.valid("json");
|
||||
const candidate = updateCandidate(db, candidateId, data);
|
||||
const candidate = await updateCandidate(db, candidateId, data);
|
||||
if (!candidate) return c.json({ error: "Candidate not found" }, 404);
|
||||
return c.json(candidate);
|
||||
},
|
||||
@@ -116,7 +116,7 @@ app.delete("/:threadId/candidates/:candidateId", async (c) => {
|
||||
const db = c.get("db");
|
||||
const candidateId = parseId(c.req.param("candidateId"));
|
||||
if (!candidateId) return c.json({ error: "Invalid candidate ID" }, 400);
|
||||
const deleted = deleteCandidate(db, candidateId);
|
||||
const deleted = await deleteCandidate(db, candidateId);
|
||||
if (!deleted) return c.json({ error: "Candidate not found" }, 404);
|
||||
|
||||
// Clean up image file if exists
|
||||
@@ -136,12 +136,12 @@ app.delete("/:threadId/candidates/:candidateId", async (c) => {
|
||||
app.patch(
|
||||
"/:id/candidates/reorder",
|
||||
zValidator("json", reorderCandidatesSchema),
|
||||
(c) => {
|
||||
async (c) => {
|
||||
const db = c.get("db");
|
||||
const threadId = parseId(c.req.param("id"));
|
||||
if (!threadId) return c.json({ error: "Invalid thread ID" }, 400);
|
||||
const { orderedIds } = c.req.valid("json");
|
||||
const result = reorderCandidates(db, threadId, orderedIds);
|
||||
const result = await reorderCandidates(db, threadId, orderedIds);
|
||||
if (!result.success) return c.json({ error: result.error }, 400);
|
||||
return c.json({ success: true });
|
||||
},
|
||||
@@ -149,13 +149,13 @@ app.patch(
|
||||
|
||||
// Resolution
|
||||
|
||||
app.post("/:id/resolve", zValidator("json", resolveThreadSchema), (c) => {
|
||||
app.post("/:id/resolve", zValidator("json", resolveThreadSchema), async (c) => {
|
||||
const db = c.get("db");
|
||||
const threadId = parseId(c.req.param("id"));
|
||||
if (!threadId) return c.json({ error: "Invalid thread ID" }, 400);
|
||||
const { candidateId } = c.req.valid("json");
|
||||
|
||||
const result = resolveThread(db, threadId, candidateId);
|
||||
const result = await resolveThread(db, threadId, candidateId);
|
||||
if (!result.success) {
|
||||
return c.json({ error: result.error }, 400);
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ type Env = { Variables: { db?: any } };
|
||||
|
||||
const app = new Hono<Env>();
|
||||
|
||||
app.get("/", (c) => {
|
||||
app.get("/", async (c) => {
|
||||
const db = c.get("db");
|
||||
const categoryTotals = getCategoryTotals(db);
|
||||
const globalTotals = getGlobalTotals(db);
|
||||
const categoryTotals = await getCategoryTotals(db);
|
||||
const globalTotals = await getGlobalTotals(db);
|
||||
return c.json({ categories: categoryTotals, global: globalTotals });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user