73 lines
2.6 KiB
JavaScript
73 lines
2.6 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
export default function handler(req, res) {
|
|
console.log('API /update-devlog aufgerufen');
|
|
console.log('Request method:', req.method);
|
|
console.log('Request body:', JSON.stringify(req.body, null, 2));
|
|
|
|
const filePath = path.join(process.cwd(), 'data', 'devlog.json');
|
|
console.log('File path:', filePath);
|
|
|
|
if (req.method === 'POST') {
|
|
try {
|
|
// Validiere eingehende Daten
|
|
if (!req.body) {
|
|
console.error('Fehler: Kein Request Body vorhanden');
|
|
return res.status(400).json({ message: 'Kein Request Body vorhanden' });
|
|
}
|
|
|
|
if (!Array.isArray(req.body)) {
|
|
console.error('Fehler: Request Body ist kein Array');
|
|
return res.status(400).json({ message: 'Request Body muss ein Array sein' });
|
|
}
|
|
|
|
console.log('Anzahl der Einträge:', req.body.length);
|
|
|
|
// Prüfe Dateiberechtigungen
|
|
try {
|
|
fs.accessSync(path.dirname(filePath), fs.constants.W_OK);
|
|
console.log('Schreibberechtigung für Verzeichnis vorhanden');
|
|
} catch (permError) {
|
|
console.error('Fehler: Keine Schreibberechtigung für Verzeichnis:', permError.message);
|
|
return res.status(500).json({ message: 'Keine Schreibberechtigung für Verzeichnis', error: permError.message });
|
|
}
|
|
|
|
// Stelle sicher, dass jeder Eintrag ein content-Array hat
|
|
const updatedData = req.body.map((entry, index) => {
|
|
console.log(`Verarbeite Eintrag ${index}:`, entry);
|
|
|
|
// Validiere erforderliche Felder
|
|
if (!entry.title || !entry.date) {
|
|
console.error(`Fehler: Eintrag ${index} fehlen erforderliche Felder (title oder date)`);
|
|
throw new Error(`Eintrag ${index} fehlen erforderliche Felder`);
|
|
}
|
|
|
|
return {
|
|
...entry,
|
|
content: entry.content || []
|
|
};
|
|
});
|
|
|
|
console.log('Verarbeitete Daten:', JSON.stringify(updatedData, null, 2));
|
|
|
|
// Schreibe Datei
|
|
fs.writeFileSync(filePath, JSON.stringify(updatedData, null, 2), 'utf8');
|
|
console.log('Datei erfolgreich geschrieben');
|
|
|
|
res.status(200).json({ message: 'DevLog updated successfully!' });
|
|
} catch (error) {
|
|
console.error('Fehler beim Verarbeiten der Anfrage:', error);
|
|
console.error('Error stack:', error.stack);
|
|
res.status(500).json({
|
|
message: 'Error updating DevLog',
|
|
error: error.message,
|
|
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
|
|
});
|
|
}
|
|
} else {
|
|
console.log('Methode nicht erlaubt:', req.method);
|
|
res.status(405).json({ message: 'Method not allowed' });
|
|
}
|
|
}
|