refactor: API-Dateien konsequent auf *APIHandler.ts umbenannt und MockData-Handling optimiert
- Einheitliches Naming für API-Routen: *APIHandler.ts - Mock-Daten aus /apiMockData/SERVICE/ werden über API bereitgestellt - API-Endpoints sofort erkennbar und verständlich - Projektstruktur deutlich klarer und wartungsfreundlicher
This commit is contained in:
51
pages/api/cpl/slotDataAPIHandler.ts
Normal file
51
pages/api/cpl/slotDataAPIHandler.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
// /pages/api/cpl/slotDataAPIHandler.ts
|
||||
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" });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user