Files
wzdj/app/api/admin/doc/route.ts

48 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from "next/server"
import { getDb } from "@/lib/db/mongo"
const DOC_ID = "conversation"
export async function GET() {
try {
const db = await getDb()
const coll = db.collection("adminDocs")
const doc = await coll.findOne({ id: DOC_ID })
const title = (doc as { title?: string } | null)?.title ?? "对话文档"
const content = (doc as { content?: string } | null)?.content ?? ""
return NextResponse.json({
success: true,
title,
content,
updatedAt: (doc as { updatedAt?: string } | null)?.updatedAt ?? null,
})
} catch {
return NextResponse.json({
success: true,
title: "对话文档",
content: "暂无对话内容。\n\n在后台完成对话后可在此处生成并保存内容部署后本页将直接呈现已保存的文档。",
updatedAt: null,
})
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const title = typeof body.title === "string" ? body.title : "对话文档"
const content = typeof body.content === "string" ? body.content : ""
const db = await getDb()
const coll = db.collection("adminDocs")
const now = new Date().toISOString()
await coll.updateOne(
{ id: DOC_ID },
{ $set: { id: DOC_ID, title, content, updatedAt: now } },
{ upsert: true }
)
return NextResponse.json({ success: true, updatedAt: now })
} catch (e) {
console.error("POST /api/admin/doc error:", e)
return NextResponse.json({ success: false, message: "保存失败" }, { status: 500 })
}
}