feat: Speiseplan-Inhalte variabel mehrsprachig (Teil 2)
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.
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
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 { Section } from "@/lib/types";
|
||||
|
||||
interface SectionRow {
|
||||
key: string;
|
||||
label: string;
|
||||
time_range: string;
|
||||
sort: number;
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ key: string }> }
|
||||
@@ -15,25 +23,37 @@ export async function PATCH(
|
||||
await requireRole(["staff", "admin"]);
|
||||
const { key } = await params;
|
||||
const body = (await request.json()) as Partial<
|
||||
Pick<Section, "label" | "label_en" | "label_pl" | "time_range" | "sort">
|
||||
>;
|
||||
Pick<Section, "label" | "time_range" | "sort">
|
||||
> & { translations?: Record<string, string | null> };
|
||||
|
||||
const fields: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
for (const col of ["label", "label_en", "label_pl", "time_range", "sort"] as const) {
|
||||
for (const col of ["label", "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<Section>(
|
||||
`update sections set ${fields.join(", ")} where key = $${values.length} returning *`,
|
||||
values
|
||||
);
|
||||
if (!section) return NextResponse.json({ error: "not_found" }, { status: 404 });
|
||||
let row: SectionRow | null;
|
||||
if (fields.length > 0) {
|
||||
values.push(key);
|
||||
row = await queryOne<SectionRow>(
|
||||
`update sections set ${fields.join(", ")} where key = $${values.length}
|
||||
returning key, label, time_range, sort`,
|
||||
values
|
||||
);
|
||||
} else {
|
||||
row = await queryOne<SectionRow>(
|
||||
"select key, label, time_range, sort from sections where key = $1",
|
||||
[key]
|
||||
);
|
||||
}
|
||||
if (!row) return NextResponse.json({ error: "not_found" }, { status: 404 });
|
||||
if (body.translations) {
|
||||
await replaceTranslations("section_translations", row.key, body.translations);
|
||||
}
|
||||
const translations = await loadTranslationsFor("section_translations", [row.key]);
|
||||
const section: Section = { ...row, translations: translations.get(row.key) ?? {} };
|
||||
return NextResponse.json(section);
|
||||
} catch (err) {
|
||||
const res = authErrorResponse(err);
|
||||
|
||||
@@ -1,12 +1,30 @@
|
||||
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 { Section } from "@/lib/types";
|
||||
|
||||
interface SectionRow {
|
||||
key: string;
|
||||
label: string;
|
||||
time_range: string;
|
||||
sort: number;
|
||||
}
|
||||
|
||||
async function withTranslations(rows: SectionRow[]): Promise<Section[]> {
|
||||
const translations = await loadTranslationsFor(
|
||||
"section_translations",
|
||||
rows.map((r) => r.key)
|
||||
);
|
||||
return rows.map((r) => ({ ...r, translations: translations.get(r.key) ?? {} }));
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const sections = await query<Section>("select * from sections order by sort");
|
||||
return NextResponse.json(sections);
|
||||
const rows = await query<SectionRow>(
|
||||
"select key, label, time_range, sort from sections order by sort"
|
||||
);
|
||||
return NextResponse.json(await withTranslations(rows));
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
@@ -17,19 +35,22 @@ export async function POST(request: Request) {
|
||||
await requireRole(["staff", "admin"]);
|
||||
const body = (await request.json()) as {
|
||||
label: string;
|
||||
label_en: string | null;
|
||||
label_pl: string | null;
|
||||
time_range: string;
|
||||
translations?: Record<string, string | null>;
|
||||
};
|
||||
|
||||
const maxRow = await queryOne<{ sort: number }>(
|
||||
"select sort from sections order by sort desc limit 1"
|
||||
);
|
||||
const section = await queryOne<Section>(
|
||||
`insert into sections (label, label_en, label_pl, time_range, sort)
|
||||
values ($1, $2, $3, $4, $5) returning *`,
|
||||
[body.label, body.label_en, body.label_pl, body.time_range, (maxRow?.sort ?? 0) + 1]
|
||||
);
|
||||
const row = (await queryOne<SectionRow>(
|
||||
`insert into sections (label, time_range, sort)
|
||||
values ($1, $2, $3) returning key, label, time_range, sort`,
|
||||
[body.label, body.time_range, (maxRow?.sort ?? 0) + 1]
|
||||
))!;
|
||||
if (body.translations) {
|
||||
await replaceTranslations("section_translations", row.key, body.translations);
|
||||
}
|
||||
const [section] = await withTranslations([row]);
|
||||
return NextResponse.json(section, { status: 201 });
|
||||
} catch (err) {
|
||||
const res = authErrorResponse(err);
|
||||
|
||||
Reference in New Issue
Block a user