Files
zhimingge/app/api/history/route.ts
T

50 lines
1.4 KiB
TypeScript

import { NextResponse } from "next/server";
import { getHistoryUsername } from "@/lib/history/request-user";
import {
addHistoryEntry,
listHistoryEntries,
} from "@/lib/history/server-store";
import type { CalcHistoryCreate } from "@/lib/history/types";
export async function GET() {
const username = await getHistoryUsername();
if (!username) {
return NextResponse.json({ error: "请先登录" }, { status: 401 });
}
const items = await listHistoryEntries(username);
return NextResponse.json({ items });
}
export async function POST(req: Request) {
const username = await getHistoryUsername();
if (!username) {
return NextResponse.json({ error: "请先登录" }, { status: 401 });
}
let body: CalcHistoryCreate;
try {
body = (await req.json()) as CalcHistoryCreate;
} catch {
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
}
if (!body.mode || !body.title || !body.question || !body.completion) {
return NextResponse.json({ error: "缺少必填字段" }, { status: 400 });
}
const entry = await addHistoryEntry(username, {
mode: body.mode,
title: body.title,
question: body.question,
summary: body.summary ?? "",
completion: body.completion,
meta: body.meta ?? {},
baziInput: body.baziInput,
baziChart: body.baziChart,
hexagram: body.hexagram,
});
return NextResponse.json({ entry });
}