Gerichtnamen, Essenszeiten und Allergen-/Zusatzstoff-Bezeichnungen sind jetzt genauso dynamisch mehrsprachig wie die Bedienoberfläche aus v1.10.0, statt fest auf Englisch/Polnisch begrenzt zu sein. Neue Übersetzungstabellen ersetzen die alten name_en/name_pl-Spalten, DeepL-Auto-Übersetzung und CSV-Import/-Export unterstützen jetzt beliebig viele aktive Sprachen.
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { query, queryOne } from "@/lib/db";
|
|
import { loadTranslationsFor, replaceTranslations } from "@/lib/db/translations";
|
|
import { authErrorResponse, requireRole } from "@/lib/auth/session";
|
|
import { isSameOrigin } from "@/lib/origin";
|
|
import type { Dish } from "@/lib/types";
|
|
|
|
interface DishRow {
|
|
id: string;
|
|
name: string;
|
|
price: string;
|
|
allergens: string[];
|
|
additives: string[];
|
|
}
|
|
|
|
async function withTranslations(rows: DishRow[]): Promise<Dish[]> {
|
|
const translations = await loadTranslationsFor(
|
|
"dish_translations",
|
|
rows.map((r) => r.id)
|
|
);
|
|
return rows.map((r) => ({
|
|
...r,
|
|
price: Number(r.price),
|
|
translations: translations.get(r.id) ?? {},
|
|
}));
|
|
}
|
|
|
|
export async function GET() {
|
|
const rows = await query<DishRow>(
|
|
"select id, name, price, allergens, additives from dishes order by name"
|
|
);
|
|
return NextResponse.json(await withTranslations(rows));
|
|
}
|
|
|
|
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;
|
|
price: number;
|
|
allergens: string[];
|
|
additives: string[];
|
|
translations?: Record<string, string | null>;
|
|
};
|
|
const row = (await queryOne<DishRow>(
|
|
`insert into dishes (name, price, allergens, additives)
|
|
values ($1, $2, $3, $4) returning id, name, price, allergens, additives`,
|
|
[body.name, body.price, body.allergens, body.additives]
|
|
))!;
|
|
if (body.translations) {
|
|
await replaceTranslations("dish_translations", row.id, body.translations);
|
|
}
|
|
const [dish] = await withTranslations([row]);
|
|
return NextResponse.json(dish, { status: 201 });
|
|
} catch (err) {
|
|
const res = authErrorResponse(err);
|
|
if (res) return res;
|
|
throw err;
|
|
}
|
|
}
|