fix: improve API logging, error handling and add status endpoint
Deploy Bürgerwind / deploy (push) Successful in 17s
Details
Deploy Bürgerwind / deploy (push) Successful in 17s
Details
This commit is contained in:
parent
3135021ebd
commit
65fdbbc9f6
72
server.js
72
server.js
|
|
@ -27,50 +27,58 @@ pool.connect()
|
|||
.then(() => console.log('Connected to PostgreSQL successfully'))
|
||||
.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
|
||||
app.post('/api/wea', async (req, res) => {
|
||||
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)) {
|
||||
return res.status(400).json({ error: 'Keine validen Turbinen-Daten empfangen' });
|
||||
}
|
||||
console.log(`Empfange Save-Request für Projekt: ${targetProject} (${turbines?.length || 0} WEAs)`);
|
||||
|
||||
const client = await pool.connect();
|
||||
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
console.log(`Lösche alte Einträge für Projekt ${projekt_id}...`);
|
||||
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(
|
||||
`INSERT INTO geodaten.wea_standorte (
|
||||
wea_nummer, hersteller, anlagentyp, nabenhoehe, rotordurchmesser, ksf_drehung, projekt_id, geom
|
||||
) 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
|
||||
]
|
||||
);
|
||||
// 1. Bestehende WEAs für dieses Projekt löschen
|
||||
await client.query(
|
||||
`DELETE FROM ${schema}.wea_standorte WHERE projekt_id = $1`,
|
||||
[targetProject]
|
||||
);
|
||||
|
||||
// 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');
|
||||
console.log('Speichervorgang erfolgreich abgeschlossen.');
|
||||
res.status(200).json({ message: 'Windenergieanlagen erfolgreich in Datenbank gespeichert' });
|
||||
} catch (e) {
|
||||
console.log(`Erfolgreich ${turbines?.length || 0} WEAs gespeichert.`);
|
||||
res.json({ message: `${turbines?.length || 0} WEAs erfolgreich in Datenbank gespeichert.` });
|
||||
} catch (err) {
|
||||
await client.query('ROLLBACK');
|
||||
console.error('Fehler beim Speichern in die Datenbank:', e);
|
||||
res.status(500).json({ error: e.message });
|
||||
console.error('Fehler beim Speichern in DB:', err);
|
||||
res.status(500).json({ error: 'Datenbankfehler beim Speichern', details: err.message });
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue