cg / hackerproducts
64 files · 5,803 lines · 161.0 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
root / src / app / api / products / [slug] / comments / [id] / route.ts
src/app/api/products/[slug]/comments/[id]/route.ts · 38 lines · 1.1 KB← back to folder
| 1 | import { NextResponse } from "next/server"; |
| 2 | import { query } from "@/db"; |
| 3 | import { getCurrentUser } from "@/lib/auth"; |
| 4 | |
| 5 | export async function DELETE( |
| 6 | _request: Request, |
| 7 | context: { params: Promise<{ slug: string; id: string }> } |
| 8 | ) { |
| 9 | const user = await getCurrentUser(); |
| 10 | if (!user) { |
| 11 | return NextResponse.json({ error: "Sign in required." }, { status: 401 }); |
| 12 | } |
| 13 | |
| 14 | const { slug, id } = await context.params; |
| 15 | const res = await query<{ id: string; user_id: string; product_owner_id: string }>( |
| 16 | `SELECT c.id, c.user_id, p.owner_id AS product_owner_id |
| 17 | FROM comments c |
| 18 | JOIN products p ON p.id = c.product_id |
| 19 | WHERE c.id = $1 AND p.slug = $2 |
| 20 | LIMIT 1`, |
| 21 | [id, slug] |
| 22 | ); |
| 23 | |
| 24 | const row = res.rows[0]; |
| 25 | if (!row) { |
| 26 | return NextResponse.json({ error: "Comment not found." }, { status: 404 }); |
| 27 | } |
| 28 | |
| 29 | const canDelete = |
| 30 | user.is_admin || user.id === row.user_id || user.id === row.product_owner_id; |
| 31 | if (!canDelete) { |
| 32 | return NextResponse.json({ error: "Forbidden." }, { status: 403 }); |
| 33 | } |
| 34 | |
| 35 | await query(`DELETE FROM comments WHERE id = $1`, [row.id]); |
| 36 | return NextResponse.json({ ok: true }); |
| 37 | } |
| 38 |