/** /api/admin/works — 作品列表 + 新增/更新(upsert by slug)。 */ import { NextResponse } from "next/server"; import { getSession, verifyCsrf } from "@/lib/auth"; import { listWorks, upsertWork, type WorkRow } from "@/lib/siteConfig"; export const runtime = "nodejs"; export async function GET() { if (!(await getSession())) { return NextResponse.json({ error: "未登录" }, { status: 401 }); } return NextResponse.json({ works: listWorks(false) }); } export async function POST(req: Request) { if (!(await verifyCsrf())) { return NextResponse.json({ error: "未登录" }, { status: 401 }); } let body: Partial & { name?: string }; try { body = await req.json(); } catch { return NextResponse.json({ error: "请求格式错误" }, { status: 400 }); } const name = String(body.name ?? "").trim(); if (!name) { return NextResponse.json({ error: "作品名称必填" }, { status: 400 }); } const row = upsertWork({ ...body, name }); return NextResponse.json({ ok: true, work: row }); }