feat: add CSV import/export for gear collection
Adds export (GET /api/items/export) and import (POST /api/items/import) routes backed by a pure csv.service with no external deps, plus useExportItems/useImportItems hooks and an Import/Export section in the Settings page. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import { zValidator } from "@hono/zod-validator";
|
||||
import { Hono } from "hono";
|
||||
import { createItemSchema, updateItemSchema } from "../../shared/schemas.ts";
|
||||
import { parseId } from "../lib/params.ts";
|
||||
import { exportItemsCsv, importItemsCsv } from "../services/csv.service.ts";
|
||||
import {
|
||||
createItem,
|
||||
deleteItem,
|
||||
@@ -17,6 +18,27 @@ type Env = { Variables: { db?: any } };
|
||||
|
||||
const app = new Hono<Env>();
|
||||
|
||||
app.get("/export", (c) => {
|
||||
const db = c.get("db");
|
||||
const csv = exportItemsCsv(db);
|
||||
c.header("Content-Type", "text/csv");
|
||||
c.header("Content-Disposition", 'attachment; filename="gearbox-export.csv"');
|
||||
return c.body(csv);
|
||||
});
|
||||
|
||||
app.post("/import", async (c) => {
|
||||
const db = c.get("db");
|
||||
const body = await c.req.parseBody();
|
||||
// Accept either "file" (direct) or "image" (via apiUpload helper)
|
||||
const file = body.file ?? body.image;
|
||||
if (!file || typeof file === "string") {
|
||||
return c.json({ error: "No CSV file provided" }, 400);
|
||||
}
|
||||
const content = await file.text();
|
||||
const result = importItemsCsv(db, content);
|
||||
return c.json(result);
|
||||
});
|
||||
|
||||
app.get("/", (c) => {
|
||||
const db = c.get("db");
|
||||
const items = getAllItems(db);
|
||||
|
||||
Reference in New Issue
Block a user