fix: improve API logging, error handling and add status endpoint
Deploy Bürgerwind / deploy (push) Successful in 17s Details

This commit is contained in:
Johannes Baumeister 2026-04-28 13:08:01 +02:00
parent 3135021ebd
commit 65fdbbc9f6
1 changed files with 40 additions and 32 deletions

View File

@ -27,50 +27,58 @@ pool.connect()
.then(() => console.log('Connected to PostgreSQL successfully')) .then(() => console.log('Connected to PostgreSQL successfully'))
.catch(err => console.error('Connection error', err.stack)); .catch(err => console.error('Connection error', err.stack));
// Status-Endpunkt für Diagnose
app.get('/api/status', async (req, res) => {
try {
const result = await pool.query('SELECT NOW()');
res.json({
status: 'online',
database: 'connected',
time: result.rows[0].now,
schema: process.env.DB_SCHEMA || 'geodaten'
});
} catch (err) {
res.status(500).json({ status: 'error', database: 'disconnected', error: err.message });
}
});
// API to save turbines // API to save turbines
app.post('/api/wea', async (req, res) => { app.post('/api/wea', async (req, res) => {
const { projekt_id, turbines } = req.body; const { projekt_id, turbines } = req.body;
console.log(`Empfange Speicheranfrage für Projekt: ${projekt_id}, Anzahl WEAs: ${turbines?.length}`); const targetProject = projekt_id || 'BWSamern-Ohne';
const schema = process.env.DB_SCHEMA || 'geodaten';
if (!turbines || !Array.isArray(turbines)) { console.log(`Empfange Save-Request für Projekt: ${targetProject} (${turbines?.length || 0} WEAs)`);
return res.status(400).json({ error: 'Keine validen Turbinen-Daten empfangen' });
}
const client = await pool.connect(); const client = await pool.connect();
try { try {
await client.query('BEGIN'); await client.query('BEGIN');
console.log(`Lösche alte Einträge für Projekt ${projekt_id}...`); // 1. Bestehende WEAs für dieses Projekt löschen
await client.query('DELETE FROM geodaten.wea_standorte WHERE projekt_id = $1', [projekt_id]);
for(let t of turbines) {
console.log(`Speichere WEA ${t.nr} (${t.type}) an Position ${t.latlng.lat}, ${t.latlng.lng}`);
await client.query( await client.query(
`INSERT INTO geodaten.wea_standorte ( `DELETE FROM ${schema}.wea_standorte WHERE projekt_id = $1`,
wea_nummer, hersteller, anlagentyp, nabenhoehe, rotordurchmesser, ksf_drehung, projekt_id, geom [targetProject]
) VALUES ($1, $2, $3, $4, $5, $6, $7, ST_Transform(ST_SetSRID(ST_MakePoint($8, $9), 4326), 25832))`,
[
t.nr,
t.hersteller || '',
t.type,
parseInt(t.hh),
parseInt(t.rd),
parseInt(t.ksfAngle || 0),
projekt_id,
t.latlng.lng,
t.latlng.lat
]
); );
// 2. Neue WEAs einfügen
if (turbines && turbines.length > 0) {
for (const t of turbines) {
await client.query(
`INSERT INTO ${schema}.wea_standorte
(projekt_id, wea_nummer, typ, nabenhoehe, rotordurchmesser, geom)
VALUES ($1, $2, $3, $4, $5, ST_Transform(ST_SetSRID(ST_MakePoint($6, $7), 4326), 25832))`,
[targetProject, t.nr, t.type, t.hh, t.rd, t.lng, t.lat]
);
}
} }
await client.query('COMMIT'); await client.query('COMMIT');
console.log('Speichervorgang erfolgreich abgeschlossen.'); console.log(`Erfolgreich ${turbines?.length || 0} WEAs gespeichert.`);
res.status(200).json({ message: 'Windenergieanlagen erfolgreich in Datenbank gespeichert' }); res.json({ message: `${turbines?.length || 0} WEAs erfolgreich in Datenbank gespeichert.` });
} catch (e) { } catch (err) {
await client.query('ROLLBACK'); await client.query('ROLLBACK');
console.error('Fehler beim Speichern in die Datenbank:', e); console.error('Fehler beim Speichern in DB:', err);
res.status(500).json({ error: e.message }); res.status(500).json({ error: 'Datenbankfehler beim Speichern', details: err.message });
} finally { } finally {
client.release(); client.release();
} }