Standardize column naming to lowercase with frontend aliases
Deploy TrassenPlaner / deploy (push) Waiting to run Details

This commit is contained in:
Johannes Baumeister 2026-04-19 22:57:01 +02:00
parent 86f2e4c8ff
commit 4770dd239b
1 changed files with 51 additions and 60 deletions

111
server.js
View File

@ -45,12 +45,20 @@ app.get('/api/owners', async (req, res) => {
const client = await pool.connect(); const client = await pool.connect();
try { try {
await setSchema(client); await setSchema(client);
const countRes = await client.query('SELECT count(*) FROM "Eigentuemerdaten"');
console.log(`Datenbank-Check: ${countRes.rows[0].count} Einträge in "Eigentuemerdaten".`); // Using mapping with aliases to keep the frontend compatible
const query = ` const query = `
SELECT SELECT
*, id,
nachname AS "Nachname",
vorname AS "Vorname",
ort AS "Ort",
flur AS "Flur",
flurstueck AS "Flurstueck",
gemarkung AS "Gemarkung",
status,
notiz,
str_hnr AS "Str_HNr",
ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry
FROM "Eigentuemerdaten" FROM "Eigentuemerdaten"
`; `;
@ -59,19 +67,18 @@ app.get('/api/owners', async (req, res) => {
const geojson = { const geojson = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
const { geometry, geom, ...properties } = row; const { geometry, ...properties } = row;
const geomObj = typeof geometry === 'string' ? JSON.parse(geometry) : geometry;
return { return {
type: "Feature", type: "Feature",
id: row.id, id: row.id,
geometry: geomObj, geometry: JSON.parse(geometry),
properties: properties properties: properties
}; };
}) })
}; };
res.json(geojson); res.json(geojson);
} catch (err) { } catch (err) {
console.error(err); console.error("Owner Fetch Error:", err);
res.status(500).json({ error: 'Failed to fetch owner data' }); res.status(500).json({ error: 'Failed to fetch owner data' });
} finally { } finally {
client.release(); client.release();
@ -85,7 +92,8 @@ app.get('/api/usage', async (req, res) => {
await setSchema(client); await setSchema(client);
const query = ` const query = `
SELECT SELECT
*, id,
nutzart AS "nutzart",
ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry
FROM "Nutzung" FROM "Nutzung"
`; `;
@ -94,18 +102,17 @@ app.get('/api/usage', async (req, res) => {
const geojson = { const geojson = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
const { geometry, geom, ...properties } = row; const { geometry, ...properties } = row;
const geomObj = typeof geometry === 'string' ? JSON.parse(geometry) : geometry;
return { return {
type: "Feature", type: "Feature",
geometry: geomObj, geometry: JSON.parse(geometry),
properties: properties properties: properties
}; };
}) })
}; };
res.json(geojson); res.json(geojson);
} catch (err) { } catch (err) {
console.error(err); console.error("Usage Fetch Error:", err);
res.status(500).json({ error: 'Failed to fetch usage data' }); res.status(500).json({ error: 'Failed to fetch usage data' });
} finally { } finally {
client.release(); client.release();
@ -117,34 +124,29 @@ app.get('/api/wea', async (req, res) => {
const client = await pool.connect(); const client = await pool.connect();
try { try {
await setSchema(client); await setSchema(client);
let result; const query = `
try { SELECT
result = await client.query(` id,
SELECT name AS "Name",
*, ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry
ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry FROM "WEA"
FROM "WEA" `;
`); const result = await client.query(query);
} catch (dbErr) {
result = { rows: [], rowCount: 0 };
}
const geojson = { const geojson = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
const { geometry, geom, ...properties } = row; const { geometry, ...properties } = row;
const geomObj = typeof geometry === 'string' ? JSON.parse(geometry) : geometry;
return { return {
type: "Feature", type: "Feature",
geometry: geomObj, geometry: JSON.parse(geometry),
properties: properties properties: properties
}; };
}) })
}; };
res.json(geojson); res.json(geojson);
} catch (err) { } catch (err) {
console.error(err); res.json({ type: "FeatureCollection", features: [] });
res.status(500).json({ error: 'Failed to fetch WEA data' });
} finally { } finally {
client.release(); client.release();
} }
@ -155,34 +157,28 @@ app.get('/api/infrastructure', async (req, res) => {
const client = await pool.connect(); const client = await pool.connect();
try { try {
await setSchema(client); await setSchema(client);
let result; const query = `
try { SELECT
result = await client.query(` id,
SELECT ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry
*, FROM "Infrastruktur"
ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry `;
FROM "Infrastruktur" const result = await client.query(query);
`);
} catch (dbErr) {
result = { rows: [], rowCount: 0 };
}
const geojson = { const geojson = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
const { geometry, geom, ...properties } = row; const { geometry, ...properties } = row;
const geomObj = typeof geometry === 'string' ? JSON.parse(geometry) : (geometry || null);
return { return {
type: "Feature", type: "Feature",
geometry: geomObj, geometry: JSON.parse(geometry),
properties: properties properties: properties
}; };
}) })
}; };
res.json(geojson); res.json(geojson);
} catch (err) { } catch (err) {
console.error(err); res.json({ type: "FeatureCollection", features: [] });
res.status(500).json({ error: 'Failed to fetch infrastructure data' });
} finally { } finally {
client.release(); client.release();
} }
@ -195,7 +191,7 @@ app.get('/api/variants', async (req, res) => {
await setSchema(client); await setSchema(client);
const query = ` const query = `
SELECT SELECT
id, name, "Variante", id, name, variante AS "Variante",
ST_AsGeoJSON(ST_Transform(ST_SetSRID(geom, 25832), 4326)) as geometry ST_AsGeoJSON(ST_Transform(ST_SetSRID(geom, 25832), 4326)) as geometry
FROM "Kabeltrasse" FROM "Kabeltrasse"
ORDER BY id DESC ORDER BY id DESC
@ -205,11 +201,10 @@ app.get('/api/variants', async (req, res) => {
const featureCollection = { const featureCollection = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
const geomObj = JSON.parse(row.geometry || 'null');
return { return {
type: "Feature", type: "Feature",
id: row.id, id: row.id,
geometry: geomObj, geometry: JSON.parse(row.geometry || 'null'),
properties: { properties: {
id: row.id, id: row.id,
name: row.name || (row.Variante ? `Variante ${row.Variante}` : 'Neue Trasse'), name: row.name || (row.Variante ? `Variante ${row.Variante}` : 'Neue Trasse'),
@ -220,7 +215,7 @@ app.get('/api/variants', async (req, res) => {
}; };
res.json(featureCollection); res.json(featureCollection);
} catch (err) { } catch (err) {
console.error(err); console.error("Variants Fetch Error:", err);
res.status(500).json({ error: 'Failed to fetch variants' }); res.status(500).json({ error: 'Failed to fetch variants' });
} finally { } finally {
client.release(); client.release();
@ -235,27 +230,25 @@ app.post('/api/variants', async (req, res) => {
await setSchema(client); await setSchema(client);
const routeName = properties.name || 'Neue Trasse'; const routeName = properties.name || 'Neue Trasse';
const variante = properties.Variante || (properties.name ? properties.name.replace('Variante ', '') : 'A'); const varianteValue = properties.Variante || (properties.name ? properties.name.replace('Variante ', '') : 'A');
const geoJsonStr = JSON.stringify(geometry); const geoJsonStr = JSON.stringify(geometry);
const upsertQuery = ` const upsertQuery = `
INSERT INTO "Kabeltrasse" (geom, name, "Variante") INSERT INTO "Kabeltrasse" (geom, name, variante)
VALUES ( VALUES (
ST_MakeValid(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), 4326), 25832)), ST_MakeValid(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), 4326), 25832)),
$2, $2,
$3 $3
) )
ON CONFLICT ("Variante") ON CONFLICT (variante)
DO UPDATE SET DO UPDATE SET
geom = EXCLUDED.geom, geom = EXCLUDED.geom,
name = EXCLUDED.name name = EXCLUDED.name
RETURNING id RETURNING id
`; `;
const upsertRes = await client.query(upsertQuery, [geoJsonStr, routeName, variante]); const upsertRes = await client.query(upsertQuery, [geoJsonStr, routeName, varianteValue]);
const finalId = upsertRes.rows[0].id; res.json({ success: true, id: upsertRes.rows[0].id });
res.json({ success: true, id: finalId });
} catch (err) { } catch (err) {
console.error("SQL-FEHLER (SAVE):", err.message); console.error("SQL-FEHLER (SAVE):", err.message);
res.status(500).json({ error: 'Failed to save variant' }); res.status(500).json({ error: 'Failed to save variant' });
@ -278,12 +271,10 @@ app.patch('/api/owners/:id', async (req, res) => {
RETURNING id; RETURNING id;
`; `;
const result = await client.query(query, [status, notiz, id]); const result = await client.query(query, [status, notiz, id]);
if (result.rowCount === 0) { if (result.rowCount === 0) return res.status(404).json({ error: 'Owner not found' });
return res.status(404).json({ error: 'Owner not found' });
}
res.json({ success: true, id: result.rows[0].id }); res.json({ success: true, id: result.rows[0].id });
} catch (err) { } catch (err) {
console.error(err); console.error("Update Owner Error:", err);
res.status(500).json({ error: 'Failed to update owner' }); res.status(500).json({ error: 'Failed to update owner' });
} finally { } finally {
client.release(); client.release();