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 { Dish } from "@/lib/types"; export async function PATCH( request: Request, { params }: { params: Promise<{ id: string }> } ) { if (!isSameOrigin(request)) { return NextResponse.json({ error: "Nicht erlaubt." }, { status: 403 }); } try { await requireRole(["staff", "admin"]); const { id } = await params; const body = (await request.json()) as { name: string; name_en: string | null; name_pl: string | null; price: number; allergens: string[]; additives: string[]; }; const dish = await queryOne( `update dishes set name = $1, name_en = $2, name_pl = $3, price = $4, allergens = $5, additives = $6 where id = $7 returning *`, [body.name, body.name_en, body.name_pl, body.price, body.allergens, body.additives, id] ); if (!dish) return NextResponse.json({ error: "not_found" }, { status: 404 }); return NextResponse.json(dish); } catch (err) { const res = authErrorResponse(err); if (res) return res; throw err; } } export async function DELETE( request: Request, { params }: { params: Promise<{ id: string }> } ) { if (!isSameOrigin(request)) { return NextResponse.json({ error: "Nicht erlaubt." }, { status: 403 }); } try { await requireRole(["staff", "admin"]); const { id } = await params; await query("delete from dishes where id = $1", [id]); return NextResponse.json({ ok: true }); } catch (err) { const res = authErrorResponse(err); if (res) return res; throw err; } }