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 GET() { const dishes = await query("select * from dishes order by name"); return NextResponse.json(dishes); } export async function POST(request: Request) { if (!isSameOrigin(request)) { return NextResponse.json({ error: "Nicht erlaubt." }, { status: 403 }); } try { await requireRole(["staff", "admin"]); 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( `insert into dishes (name, name_en, name_pl, price, allergens, additives) values ($1, $2, $3, $4, $5, $6) returning *`, [body.name, body.name_en, body.name_pl, body.price, body.allergens, body.additives] ); return NextResponse.json(dish, { status: 201 }); } catch (err) { const res = authErrorResponse(err); if (res) return res; throw err; } }