49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { createSupabaseRouteClient } from "@/lib/supabase/server";
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
import { getConvexHttpClient } from "@/lib/convex/server";
|
|
import { api } from "@/lib/convex/api";
|
|
import { requireAuthContext } from "@/lib/auth/authContext";
|
|
|
|
interface RenamePayload {
|
|
documentId: string;
|
|
title: string;
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
if (isConvexEnabled()) {
|
|
const auth = requireAuthContext();
|
|
const { documentId, title }: RenamePayload = await request.json();
|
|
const client = getConvexHttpClient();
|
|
await client.mutation(api.documents.updateTitle, {
|
|
userId: auth.userId,
|
|
id: documentId,
|
|
title,
|
|
});
|
|
return NextResponse.json({ ok: true });
|
|
}
|
|
|
|
const supabase = await createSupabaseRouteClient();
|
|
const {
|
|
data: { session },
|
|
} = await supabase.auth.getSession();
|
|
|
|
if (!session) {
|
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
}
|
|
|
|
const { documentId, title }: RenamePayload = await request.json();
|
|
|
|
const { error } = await supabase
|
|
.from("documents")
|
|
.update({ title })
|
|
.eq("id", documentId)
|
|
.eq("user_id", session.user.id);
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 400 });
|
|
}
|
|
|
|
return NextResponse.json({ ok: true });
|
|
}
|