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] / route.ts
src/app/api/products/[slug]/route.ts · 123 lines · 3.5 KB← back to folder
| 1 | import { NextResponse } from "next/server"; |
| 2 | import { query } from "@/db"; |
| 3 | import { getCurrentUser } from "@/lib/auth"; |
| 4 | import { getProductBySlug } from "@/lib/products"; |
| 5 | import { isReservedUsername, slugifyProductName, validateProductInput } from "@/lib/validation"; |
| 6 | |
| 7 | export async function PATCH( |
| 8 | request: Request, |
| 9 | context: { params: Promise<{ slug: string }> } |
| 10 | ) { |
| 11 | const user = await getCurrentUser(); |
| 12 | if (!user) { |
| 13 | return NextResponse.json({ error: "Sign in required." }, { status: 401 }); |
| 14 | } |
| 15 | |
| 16 | const { slug } = await context.params; |
| 17 | const existing = await getProductBySlug(slug, user.id, true); |
| 18 | if (!existing) { |
| 19 | return NextResponse.json({ error: "Product not found." }, { status: 404 }); |
| 20 | } |
| 21 | |
| 22 | if (existing.owner_id !== user.id && !user.is_admin) { |
| 23 | return NextResponse.json({ error: "Forbidden." }, { status: 403 }); |
| 24 | } |
| 25 | |
| 26 | const body = (await request.json().catch(() => null)) as { |
| 27 | name?: string; |
| 28 | slug?: string; |
| 29 | website_url?: string; |
| 30 | description?: string; |
| 31 | readme_md?: string; |
| 32 | icon_url?: string | null; |
| 33 | screenshot_url?: string | null; |
| 34 | } | null; |
| 35 | |
| 36 | if (!body) { |
| 37 | return NextResponse.json({ error: "Invalid request body." }, { status: 400 }); |
| 38 | } |
| 39 | |
| 40 | const validated = validateProductInput(body); |
| 41 | if (!validated.ok) { |
| 42 | return NextResponse.json({ error: validated.error }, { status: 400 }); |
| 43 | } |
| 44 | |
| 45 | let nextSlug = existing.slug; |
| 46 | if (body.slug && body.slug.trim()) { |
| 47 | const candidate = slugifyProductName(body.slug); |
| 48 | if (!candidate || isReservedUsername(candidate)) { |
| 49 | return NextResponse.json( |
| 50 | { error: "That URL slug is invalid or reserved." }, |
| 51 | { status: 400 } |
| 52 | ); |
| 53 | } |
| 54 | if (candidate !== existing.slug) { |
| 55 | const [prodTaken, userTaken] = await Promise.all([ |
| 56 | query(`SELECT 1 FROM products WHERE LOWER(slug) = LOWER($1) AND id <> $2`, [ |
| 57 | candidate, |
| 58 | existing.id, |
| 59 | ]), |
| 60 | query(`SELECT 1 FROM users WHERE LOWER(username) = LOWER($1)`, [candidate]), |
| 61 | ]); |
| 62 | if ( |
| 63 | (prodTaken.rowCount && prodTaken.rowCount > 0) || |
| 64 | (userTaken.rowCount && userTaken.rowCount > 0) |
| 65 | ) { |
| 66 | return NextResponse.json({ error: "That URL slug is already taken." }, { status: 409 }); |
| 67 | } |
| 68 | nextSlug = candidate; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | await query( |
| 73 | `UPDATE products |
| 74 | SET name = $1, |
| 75 | slug = $2, |
| 76 | website_url = $3, |
| 77 | description = $4, |
| 78 | readme_md = $5, |
| 79 | icon_url = $6, |
| 80 | screenshot_url = $7, |
| 81 | updated_at = NOW() |
| 82 | WHERE id = $8`, |
| 83 | [ |
| 84 | validated.value.name, |
| 85 | nextSlug, |
| 86 | validated.value.website_url, |
| 87 | validated.value.description, |
| 88 | validated.value.readme_md, |
| 89 | validated.value.icon_url, |
| 90 | validated.value.screenshot_url, |
| 91 | existing.id, |
| 92 | ] |
| 93 | ); |
| 94 | |
| 95 | return NextResponse.json({ ok: true, slug: nextSlug }); |
| 96 | } |
| 97 | |
| 98 | export async function DELETE( |
| 99 | _request: Request, |
| 100 | context: { params: Promise<{ slug: string }> } |
| 101 | ) { |
| 102 | const user = await getCurrentUser(); |
| 103 | if (!user) { |
| 104 | return NextResponse.json({ error: "Sign in required." }, { status: 401 }); |
| 105 | } |
| 106 | |
| 107 | const { slug } = await context.params; |
| 108 | const existing = await getProductBySlug(slug, user.id, true); |
| 109 | if (!existing) { |
| 110 | return NextResponse.json({ error: "Product not found." }, { status: 404 }); |
| 111 | } |
| 112 | |
| 113 | if (existing.owner_id !== user.id && !user.is_admin) { |
| 114 | return NextResponse.json({ error: "Forbidden." }, { status: 403 }); |
| 115 | } |
| 116 | |
| 117 | await query(`UPDATE products SET status = 'removed', updated_at = NOW() WHERE id = $1`, [ |
| 118 | existing.id, |
| 119 | ]); |
| 120 | |
| 121 | return NextResponse.json({ ok: true }); |
| 122 | } |
| 123 |