import { NextResponse } from "next/server"; import { query, queryOne } from "@/lib/db"; import { authErrorResponse, requireRole } from "@/lib/auth/session"; import { isSameOrigin } from "@/lib/origin"; import type { Section } from "@/lib/types"; export async function PATCH( request: Request, { params }: { params: Promise<{ key: string }> } ) { if (!isSameOrigin(request)) { return NextResponse.json({ error: "Nicht erlaubt." }, { status: 403 }); } try { await requireRole(["staff", "admin"]); const { key } = await params; const body = (await request.json()) as Partial< Pick >; const fields: string[] = []; const values: unknown[] = []; for (const col of ["label", "label_en", "label_pl", "time_range", "sort"] as const) { if (body[col] !== undefined) { values.push(body[col]); fields.push(`${col} = $${values.length}`); } } if (fields.length === 0) return NextResponse.json({ error: "empty_update" }, { status: 400 }); values.push(key); const section = await queryOne
( `update sections set ${fields.join(", ")} where key = $${values.length} returning *`, values ); if (!section) return NextResponse.json({ error: "not_found" }, { status: 404 }); return NextResponse.json(section); } catch (err) { const res = authErrorResponse(err); if (res) return res; throw err; } } export async function DELETE( request: Request, { params }: { params: Promise<{ key: string }> } ) { if (!isSameOrigin(request)) { return NextResponse.json({ error: "Nicht erlaubt." }, { status: 403 }); } try { await requireRole(["staff", "admin"]); const { key } = await params; await query("delete from sections where key = $1", [key]); return NextResponse.json({ ok: true }); } catch (err) { const res = authErrorResponse(err); if (res) return res; throw err; } }