esLint
This commit is contained in:
@@ -3,10 +3,24 @@ import { NextApiRequest, NextApiResponse } from "next";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
|
||||
// Typ für einzelne Einträge im JSON-Array
|
||||
type ChartDataEntry = {
|
||||
timestamp?: string;
|
||||
zeit?: string;
|
||||
time?: string;
|
||||
[key: string]: unknown; // zusätzliche Werte erlaubt
|
||||
};
|
||||
|
||||
// Hilfsfunktion: JSON-Datei laden
|
||||
async function loadJsonData(filePath: string) {
|
||||
async function loadJsonData(filePath: string): Promise<ChartDataEntry[]> {
|
||||
const data = await fs.readFile(filePath, "utf8");
|
||||
return JSON.parse(data);
|
||||
const parsed = JSON.parse(data);
|
||||
|
||||
if (!Array.isArray(parsed)) {
|
||||
throw new Error("Ungültiges Format: Erwartet ein Array");
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export default async function handler(
|
||||
@@ -32,20 +46,23 @@ export default async function handler(
|
||||
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;
|
||||
filteredData = jsonData.filter((item) => {
|
||||
const dateString = item.timestamp ?? item.zeit ?? item.time;
|
||||
const itemDate = dateString ? new Date(dateString) : null;
|
||||
|
||||
return itemDate !== null && itemDate >= von && itemDate <= bis;
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json(filteredData);
|
||||
res.status(200).json(filteredData);
|
||||
} catch (error) {
|
||||
return res.status(404).json({ error: "File not found or read error" });
|
||||
console.error("Fehler beim Lesen der Slot-Daten:", error);
|
||||
res.status(500).json({ error: "Fehler beim Lesen der Slot-Daten" });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user