Files
mnote/wolai-frontend/src/app/api/mindmap/[docId]/route.ts
T

64 lines
1.9 KiB
TypeScript
Raw Normal View History

2026-01-02 07:25:50 +08:00
import { NextResponse } from "next/server";
2026-01-17 10:12:53 +08:00
import { isConvexEnabled } from "@/lib/convex/enabled";
import { getAuthedConvexClient } from "@/lib/convex/route";
import { api } from "@/lib/convex/api";
2026-01-02 07:25:50 +08:00
const defaultMindmapData = {
data: { text: "中心主题" },
children: [],
};
export async function GET(
_req: Request,
2026-01-08 06:28:14 +08:00
{ params }: { params: Promise<{ docId: string }> },
2026-01-02 07:25:50 +08:00
) {
2026-01-18 19:01:31 +08:00
const { docId } = await params;
2026-01-17 10:12:53 +08:00
if (isConvexEnabled()) {
2026-01-18 19:01:31 +08:00
const { client } = await getAuthedConvexClient();
const mindmapId = `legacy-${docId}`;
const res = await client.query(api.mindmaps.get, { docId, mindmapId });
2026-01-17 10:12:53 +08:00
return NextResponse.json({ data: res?.data ?? defaultMindmapData, source: "convex" });
}
2026-01-18 19:01:31 +08:00
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
2026-01-02 07:25:50 +08:00
}
export async function POST(
request: Request,
2026-01-08 06:28:14 +08:00
{ params }: { params: Promise<{ docId: string }> },
2026-01-02 07:25:50 +08:00
) {
2026-01-18 19:01:31 +08:00
const { docId } = await params;
2026-01-17 10:12:53 +08:00
if (isConvexEnabled()) {
2026-01-18 19:01:31 +08:00
const { client } = await getAuthedConvexClient();
const mindmapId = `legacy-${docId}`;
const payload = (await request.json().catch(() => ({}))) as { data?: unknown };
2026-01-17 10:12:53 +08:00
const result = await client.mutation(api.mindmaps.put, {
2026-01-18 19:01:31 +08:00
docId,
2026-01-17 10:12:53 +08:00
mindmapId,
2026-01-18 19:01:31 +08:00
data: payload.data ?? defaultMindmapData,
2026-01-17 10:12:53 +08:00
});
2026-01-18 19:01:31 +08:00
return NextResponse.json(result ?? { ok: true });
2026-01-17 10:12:53 +08:00
}
2026-01-18 19:01:31 +08:00
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
2026-01-02 07:25:50 +08:00
}
export async function DELETE(
_req: Request,
2026-01-08 06:28:14 +08:00
{ params }: { params: Promise<{ docId: string }> },
2026-01-02 07:25:50 +08:00
) {
2026-01-18 19:01:31 +08:00
const { docId } = await params;
2026-01-17 10:12:53 +08:00
if (isConvexEnabled()) {
2026-01-18 19:01:31 +08:00
const { client } = await getAuthedConvexClient();
const mindmapId = `legacy-${docId}`;
const result = await client.mutation(api.mindmaps.softDelete, { docId, mindmapId });
return NextResponse.json(result ?? { ok: true });
2026-01-17 10:12:53 +08:00
}
2026-01-18 19:01:31 +08:00
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
2026-01-02 07:25:50 +08:00
}
2026-01-18 19:01:31 +08:00