feat: add CSV import/export for gear collection
Some checks failed
CI / ci (pull_request) Failing after 22s
CI / e2e (pull_request) Has been skipped

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:
2026-04-03 18:12:07 +02:00
parent 8c1fe47a99
commit 15f146ee89
6 changed files with 645 additions and 2 deletions

View File

@@ -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);