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