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 / admin / products / [slug] / route.ts
src/app/api/admin/products/[slug]/route.ts · 34 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 | const ALLOWED_STATUSES = new Set(["published", "pending", "hidden", "removed"]); |
| 6 | |
| 7 | export async function PATCH( |
| 8 | request: Request, |
| 9 | context: { params: Promise<{ slug: string }> } |
| 10 | ) { |
| 11 | const user = await getCurrentUser(); |
| 12 | if (!user || !user.is_admin) { |
| 13 | return NextResponse.json({ error: "Admin access required." }, { status: 403 }); |
| 14 | } |
| 15 | |
| 16 | const { slug } = await context.params; |
| 17 | const body = (await request.json().catch(() => null)) as { status?: string } | null; |
| 18 | const status = body?.status; |
| 19 | if (!status || !ALLOWED_STATUSES.has(status)) { |
| 20 | return NextResponse.json({ error: "Invalid status." }, { status: 400 }); |
| 21 | } |
| 22 | |
| 23 | const res = await query( |
| 24 | `UPDATE products SET status = $1, updated_at = NOW() WHERE slug = $2 RETURNING slug, status`, |
| 25 | [status, slug] |
| 26 | ); |
| 27 | |
| 28 | if (res.rowCount === 0) { |
| 29 | return NextResponse.json({ error: "Product not found." }, { status: 404 }); |
| 30 | } |
| 31 | |
| 32 | return NextResponse.json({ ok: true, slug, status }); |
| 33 | } |
| 34 |