90 lines
2.0 KiB
TypeScript
90 lines
2.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { auth } from "@/app/(auth)/auth";
|
|
import {
|
|
createWorkspaceDocument,
|
|
normalizeWorkspacePayload,
|
|
type UpdateAiCadWorkspaceRequest,
|
|
} from "@/app/api/ai-cad/workspace-utils";
|
|
import {
|
|
createAiCadWorkspace,
|
|
listAiCadWorkspacesByUserId,
|
|
updateAiCadWorkspace,
|
|
} from "@/lib/db/queries";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
export async function GET() {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ success: false, error: "未登录" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const workspaces = await listAiCadWorkspacesByUserId({
|
|
userId: session.user.id,
|
|
});
|
|
const workspace = workspaces[0] ?? null;
|
|
|
|
if (!workspace) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: null,
|
|
});
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: createWorkspaceDocument(workspace),
|
|
});
|
|
}
|
|
|
|
export async function PUT(request: Request) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ success: false, error: "未登录" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const body = (await request.json()) as UpdateAiCadWorkspaceRequest;
|
|
const existingWorkspaces = await listAiCadWorkspacesByUserId({
|
|
userId: session.user.id,
|
|
});
|
|
const targetWorkspace =
|
|
existingWorkspaces[0] ??
|
|
(await createAiCadWorkspace({
|
|
userId: session.user.id,
|
|
name: "AI-CAD 工作台",
|
|
}));
|
|
|
|
if (!targetWorkspace) {
|
|
return NextResponse.json(
|
|
{ success: false, error: "初始化 AI-CAD 工作台失败" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
const savedWorkspace = await updateAiCadWorkspace({
|
|
workspaceId: targetWorkspace.id,
|
|
userId: session.user.id,
|
|
...normalizeWorkspacePayload(body),
|
|
});
|
|
|
|
if (!savedWorkspace) {
|
|
return NextResponse.json(
|
|
{ success: false, error: "保存 AI-CAD 工作台失败" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: {
|
|
updatedAt: savedWorkspace.updatedAt?.toISOString() || null,
|
|
},
|
|
});
|
|
}
|