-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
52 lines (44 loc) · 1.6 KB
/
Copy pathroute.ts
File metadata and controls
52 lines (44 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { prisma } from "@/lib/prisma";
import { getUserFromRequest } from "@/lib/supabase/get-user";
import { NextRequest } from "next/server";
// POST /api/collections/[id]/bookmarks — add a bookmark to a collection
export async function POST(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const user = await getUserFromRequest(req);
if (!user) return Response.json({ error: "unauthorized" }, { status: 401 });
const { id: collectionId } = await params;
// Verify collection belongs to user
const collection = await prisma.collection.findUnique({
where: { id: collectionId },
});
if (!collection || collection.userId !== user.id) {
return Response.json({ error: "not found" }, { status: 404 });
}
let body: { bookmarkId?: unknown };
try {
body = await req.json();
} catch {
return Response.json({ error: "invalid json" }, { status: 400 });
}
const bookmarkId =
typeof body.bookmarkId === "string" ? body.bookmarkId.trim() : "";
if (!bookmarkId) {
return Response.json({ error: "bookmarkId is required" }, { status: 400 });
}
// Verify bookmark belongs to user
const bookmark = await prisma.bookmark.findUnique({
where: { id: bookmarkId },
});
if (!bookmark || bookmark.userId !== user.id) {
return Response.json({ error: "bookmark not found" }, { status: 404 });
}
// Upsert to avoid duplicate errors
await prisma.collectionBookmark.upsert({
where: { collectionId_bookmarkId: { collectionId, bookmarkId } },
create: { collectionId, bookmarkId },
update: {},
});
return new Response(null, { status: 204 });
}