|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { getServerSession } from "next-auth"; |
| 3 | +import { authOptions } from "@/lib/auth"; |
| 4 | +import { |
| 5 | + updateResource, |
| 6 | + deleteResource, |
| 7 | + incrementResourceViews, |
| 8 | + incrementResourceShares, |
| 9 | + incrementResourceBookmarks, |
| 10 | + decrementResourceBookmarks, |
| 11 | +} from "@/lib/firestore/resources"; |
| 12 | +import { isAdminUser } from "@/lib/admin"; |
| 13 | + |
| 14 | +/** |
| 15 | + * PATCH /api/resources/[id] |
| 16 | + * |
| 17 | + * Body (all fields optional): |
| 18 | + * { action: "increment_views" | "increment_shares" | "bookmark" | "unbookmark" } |
| 19 | + * { description, pinned, category, source, author, title, url, readTime, thumbnail } |
| 20 | + * Admin-only: description, pinned, category, source, author, title, url, readTime, thumbnail |
| 21 | + */ |
| 22 | +export async function PATCH( |
| 23 | + request: NextRequest, |
| 24 | + { params }: { params: Promise<{ id: string }> } |
| 25 | +) { |
| 26 | + const session = await getServerSession(authOptions); |
| 27 | + if (!session) { |
| 28 | + return NextResponse.json({ error: "Unauthorised" }, { status: 401 }); |
| 29 | + } |
| 30 | + |
| 31 | + const { id } = await params; |
| 32 | + const body: Record<string, unknown> = await request.json(); |
| 33 | + const userId = (session.user as { id?: string }).id; |
| 34 | + |
| 35 | + try { |
| 36 | + const { action, ...patch } = body; |
| 37 | + |
| 38 | + if (action === "increment_views") { |
| 39 | + await incrementResourceViews(id); |
| 40 | + return NextResponse.json({ ok: true }); |
| 41 | + } |
| 42 | + |
| 43 | + if (action === "increment_shares") { |
| 44 | + await incrementResourceShares(id); |
| 45 | + return NextResponse.json({ ok: true }); |
| 46 | + } |
| 47 | + |
| 48 | + if (action === "bookmark") { |
| 49 | + await incrementResourceBookmarks(id); |
| 50 | + return NextResponse.json({ ok: true }); |
| 51 | + } |
| 52 | + |
| 53 | + if (action === "unbookmark") { |
| 54 | + await decrementResourceBookmarks(id); |
| 55 | + return NextResponse.json({ ok: true }); |
| 56 | + } |
| 57 | + |
| 58 | + // Admin-only metadata edits |
| 59 | + if (!isAdminUser(userId)) { |
| 60 | + return NextResponse.json({ error: "Forbidden" }, { status: 403 }); |
| 61 | + } |
| 62 | + |
| 63 | + await updateResource(id, patch as Parameters<typeof updateResource>[1]); |
| 64 | + return NextResponse.json({ ok: true }); |
| 65 | + } catch (err) { |
| 66 | + console.error(`[PATCH /api/resources/${id}]`, err); |
| 67 | + return NextResponse.json({ error: "Internal server error" }, { status: 500 }); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** DELETE /api/resources/[id] — admin only */ |
| 72 | +export async function DELETE( |
| 73 | + _request: NextRequest, |
| 74 | + { params }: { params: Promise<{ id: string }> } |
| 75 | +) { |
| 76 | + const session = await getServerSession(authOptions); |
| 77 | + if (!session) { |
| 78 | + return NextResponse.json({ error: "Unauthorised" }, { status: 401 }); |
| 79 | + } |
| 80 | + |
| 81 | + const userId = (session.user as { id?: string }).id; |
| 82 | + if (!isAdminUser(userId)) { |
| 83 | + return NextResponse.json({ error: "Forbidden" }, { status: 403 }); |
| 84 | + } |
| 85 | + |
| 86 | + const { id } = await params; |
| 87 | + |
| 88 | + try { |
| 89 | + await deleteResource(id); |
| 90 | + return NextResponse.json({ ok: true }); |
| 91 | + } catch (err) { |
| 92 | + console.error(`[DELETE /api/resources/${id}]`, err); |
| 93 | + return NextResponse.json({ error: "Internal server error" }, { status: 500 }); |
| 94 | + } |
| 95 | +} |
0 commit comments