Mist, jetzt vielleicht

This commit is contained in:
rxf
2026-03-11 20:33:19 +01:00
parent bc235e4e32
commit a949ebcdc8
28 changed files with 1666 additions and 74 deletions

94
app/api/data/route.ts Normal file
View File

@@ -0,0 +1,94 @@
import { NextRequest, NextResponse } from 'next/server';
import type { RowDataPacket } from 'mysql2';
import pool from '@/lib/mysql';
import { checkAblauf } from '@/lib/checkAblauf';
import moment from 'moment';
import { Tablette, DataResponse } from '@/types/tablette';
function formatDate(dt: Date | string | null): string {
if (!dt) return '';
const d = moment(dt);
if (!d.isValid() || d.isBefore(moment('2020-01-01'))) return '';
return d.format('YYYY-MM-DD');
}
const SORT_WHITELIST = new Set(['tab', 'pday', 'cnt', 'at', 'akt', 'until', 'warn', 'rem', 'order']);
// GET /api/data?sidx=...&sord=asc|desc
export async function GET(req: NextRequest) {
const { searchParams } = req.nextUrl;
const sidxRaw = searchParams.get('sidx') || 'pday';
const sidx = SORT_WHITELIST.has(sidxRaw) ? sidxRaw : 'pday';
const sord = searchParams.get('sord') === 'asc' ? 'ASC' : 'DESC';
const col = `\`${sidx}\``;
const [rows] = await pool.query<RowDataPacket[]>(
`SELECT tab, pday, cnt, at, akt, until, warn, rem, \`order\`
FROM tabletten
ORDER BY ${col} ${sord}, rem DESC, tab ASC`
);
const values: Tablette[] = rows.map((r) => ({
tab: r.tab,
pday: r.pday,
cnt: r.cnt,
at: formatDate(r.at),
akt: r.akt,
until: formatDate(r.until),
warn: r.warn === 1 || r.warn === true,
rem: r.rem,
order: r.order,
}));
const result: DataResponse = {
total: 1,
page: 1,
records: values.length,
values,
};
return NextResponse.json(result);
}
// POST /api/data
// body: { oper: 'add'|'edit'|'del', tab, pday, cnt, at, rem, order }
export async function POST(req: NextRequest) {
const data = await req.json();
if (data.oper === 'del') {
await pool.execute('DELETE FROM tabletten WHERE tab = ?', [data.tab]);
return NextResponse.json({ ok: true });
}
// add or edit
const pday = parseFloat(data.pday) || 1;
const cnt = parseInt(data.cnt, 10) || 0;
const at = data.at === '' ? '1900-01-01' : data.at;
const rem = data.rem || '';
const order = data.order || '';
const updates = checkAblauf({ cnt, pday, at: new Date(at) });
await pool.execute(
`INSERT INTO tabletten (tab, pday, cnt, at, akt, until, warn, rem, \`order\`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
pday = VALUES(pday),
cnt = VALUES(cnt),
at = VALUES(at),
akt = VALUES(akt),
until = VALUES(until),
warn = VALUES(warn),
rem = VALUES(rem),
\`order\` = VALUES(\`order\`)`,
[
data.tab, pday, cnt, at,
updates.akt,
moment(updates.until).format('YYYY-MM-DD'),
updates.warn ? 1 : 0,
rem, order,
]
);
return NextResponse.json({ ok: true });
}