feat: API-Route für gefilterte Mock-Daten implementiert
- Neue Route /api/cpl/slotData erstellt - Lädt Daten aus /public/CPLmockData/kuesChartData/... basierend auf slot, messart und dia - Optionales Filtern über vonDatum und bisDatum hinzugefügt - 404-Fehler bei fehlender Datei wird abgefangen
This commit is contained in:
50
pages/api/cpl/slotData.ts
Normal file
50
pages/api/cpl/slotData.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
|
||||
// Hilfsfunktion: JSON-Datei laden
|
||||
async function loadJsonData(filePath: string) {
|
||||
const data = await fs.readFile(filePath, "utf8");
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const { slot, messart, dia, vonDatum, bisDatum } = req.query;
|
||||
|
||||
if (!slot || !messart || !dia) {
|
||||
return res.status(400).json({ error: "Missing required parameters" });
|
||||
}
|
||||
|
||||
const jsonFilePath = path.join(
|
||||
process.cwd(),
|
||||
"public",
|
||||
"CPLmockData",
|
||||
"kuesChartData",
|
||||
`slot${slot}`,
|
||||
`${messart}`,
|
||||
`${dia}.json`
|
||||
);
|
||||
|
||||
try {
|
||||
const jsonData = await loadJsonData(jsonFilePath);
|
||||
|
||||
// Filtern nach Datum, wenn angegeben
|
||||
let filteredData = jsonData;
|
||||
if (vonDatum && bisDatum) {
|
||||
const von = new Date(`${vonDatum}T00:00:00`);
|
||||
const bis = new Date(`${bisDatum}T23:59:59`);
|
||||
|
||||
filteredData = jsonData.filter((item: any) => {
|
||||
const timestamp = new Date(item.t);
|
||||
return timestamp >= von && timestamp <= bis;
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json(filteredData);
|
||||
} catch (error) {
|
||||
return res.status(404).json({ error: "File not found or read error" });
|
||||
}
|
||||
}
|
||||
11
pages/api/test.ts
Normal file
11
pages/api/test.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
// pages/api/test.ts
|
||||
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
res.status(200).json({
|
||||
message: "✅ Die API ist erreichbar!",
|
||||
method: req.method,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user