chore: init monorepo snapshot
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { createSupabaseServerClient } from "@/lib/supabase/server";
|
||||
|
||||
type CreateChildPayload = {
|
||||
parentId: string | null;
|
||||
title?: string;
|
||||
blocks?: unknown;
|
||||
};
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const supabase = await createSupabaseServerClient();
|
||||
const {
|
||||
data: { user },
|
||||
error: authError,
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (authError || !user) {
|
||||
return NextResponse.json(
|
||||
{ message: "未登录无法创建页面" },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
const { parentId, title, blocks }: CreateChildPayload =
|
||||
await request.json();
|
||||
|
||||
if (parentId === undefined) {
|
||||
return NextResponse.json(
|
||||
{ message: "缺少 parentId" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const siblingQuery = supabase
|
||||
.from("documents")
|
||||
.select("id", { count: "exact", head: true })
|
||||
.eq("user_id", user.id);
|
||||
|
||||
if (parentId) {
|
||||
siblingQuery.eq("parent_id", parentId);
|
||||
} else {
|
||||
siblingQuery.is("parent_id", null);
|
||||
}
|
||||
|
||||
const { count: siblingCount = 0, error: countError } =
|
||||
await siblingQuery;
|
||||
|
||||
if (countError) {
|
||||
return NextResponse.json(
|
||||
{ message: countError.message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
const resolvedTitle =
|
||||
title?.trim() && title.trim().length > 0
|
||||
? title.trim()
|
||||
: "未命名页面";
|
||||
|
||||
const contentPayload = Array.isArray(blocks) ? blocks : [];
|
||||
|
||||
const { data: newDoc, error } = await supabase
|
||||
.from("documents")
|
||||
.insert({
|
||||
user_id: user.id,
|
||||
parent_id: parentId,
|
||||
title: resolvedTitle,
|
||||
content: contentPayload,
|
||||
sort_order: siblingCount,
|
||||
})
|
||||
.select("id, title")
|
||||
.single();
|
||||
|
||||
if (error || !newDoc) {
|
||||
return NextResponse.json(
|
||||
{ message: error?.message ?? "创建子页面失败" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
pageId: newDoc.id,
|
||||
title: newDoc.title ?? resolvedTitle,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user