-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
34 lines (29 loc) · 1.2 KB
/
Copy pathroute.ts
File metadata and controls
34 lines (29 loc) · 1.2 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
import { prisma } from "@/lib/prisma";
import { getUserFromRequest } from "@/lib/supabase/get-user";
import { collectionRatelimit } from "@/lib/ratelimit";
import { NextRequest } from "next/server";
// DELETE /api/collections/[id]
export async function DELETE(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const user = await getUserFromRequest(req);
if (!user) return Response.json({ error: "unauthorized" }, { status: 401 });
const { success } = await collectionRatelimit.limit(user.id);
if (!success)
return Response.json({ error: "too many requests - slow down" }, { status: 429 });
const { id } = await params;
const collection = await prisma.collection.findUnique({ where: { id } });
if (!collection || collection.userId !== user.id) {
return Response.json({ error: "not found" }, { status: 404 });
}
try {
await prisma.collection.delete({ where: { id } });
} catch (e: unknown) {
if ((e as { code?: string })?.code === "P2025") {
return new Response(null, { status: 204 }); // already deleted — idempotent
}
return Response.json({ error: "failed to delete collection" }, { status: 500 });
}
return new Response(null, { status: 204 });
}