Fix WEA and Nutzung mapping after structure scan
Deploy TrassenPlaner / deploy (push) Waiting to run Details

This commit is contained in:
Johannes Baumeister 2026-04-20 09:37:24 +02:00
parent 7753c8b262
commit c5d488e5c1
1 changed files with 16 additions and 25 deletions

View File

@ -32,9 +32,6 @@ async function setSchema(client) {
await client.query(`SET search_path TO "${schema}", public;`); await client.query(`SET search_path TO "${schema}", public;`);
} }
// Database Initial Setup / Migration Logic Removed
// (Now treating existing tables as the read-only 'Source of Truth')
// --- API Routes --- // --- API Routes ---
app.get('/api/health', (req, res) => { app.get('/api/health', (req, res) => {
res.json({ status: 'OK', schema: process.env.DB_SCHEMA, time: new Date().toISOString() }); res.json({ status: 'OK', schema: process.env.DB_SCHEMA, time: new Date().toISOString() });
@ -45,8 +42,6 @@ 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);
// Strict Case-Sensitivity Mapping based on server structure
const query = ` const query = `
SELECT SELECT
id, id,
@ -59,13 +54,12 @@ app.get('/api/owners', async (req, res) => {
"PLZ" AS "PLZ", "PLZ" AS "PLZ",
"Land" AS "Land", "Land" AS "Land",
"AFlaeche" AS "AFlaeche", "AFlaeche" AS "AFlaeche",
status as "status", status,
notiz as "notiz", notiz,
ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry
FROM eigentuemerdaten FROM eigentuemerdaten
`; `;
const result = await client.query(query); const result = await client.query(query);
const geojson = { const geojson = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
@ -95,12 +89,12 @@ app.get('/api/usage', async (req, res) => {
const query = ` const query = `
SELECT SELECT
id, id,
"nutzart" as "nutzart", nutzart AS "nutzart",
bez AS "bez",
ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry
FROM nutzung FROM nutzung
`; `;
const result = await client.query(query); const result = await client.query(query);
const geojson = { const geojson = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
@ -114,8 +108,8 @@ app.get('/api/usage', async (req, res) => {
}; };
res.json(geojson); res.json(geojson);
} catch (err) { } catch (err) {
console.error("Usage Fetch Error:", err); console.error("Usage Error:", err);
res.status(500).json({ error: 'Failed to fetch usage data' }); res.json({ type: "FeatureCollection", features: [] });
} finally { } finally {
client.release(); client.release();
} }
@ -129,12 +123,12 @@ app.get('/api/wea', async (req, res) => {
const query = ` const query = `
SELECT SELECT
id, id,
"Name" AS "Name", "WEA" AS "Name",
"GH" AS "GH",
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); const result = await client.query(query);
const geojson = { const geojson = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
@ -148,6 +142,7 @@ app.get('/api/wea', async (req, res) => {
}; };
res.json(geojson); res.json(geojson);
} catch (err) { } catch (err) {
console.error("WEA Error:", err);
res.json({ type: "FeatureCollection", features: [] }); res.json({ type: "FeatureCollection", features: [] });
} finally { } finally {
client.release(); client.release();
@ -166,7 +161,6 @@ app.get('/api/infrastructure', async (req, res) => {
FROM infrastruktur FROM infrastruktur
`; `;
const result = await client.query(query); const result = await client.query(query);
const geojson = { const geojson = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
@ -193,13 +187,12 @@ app.get('/api/variants', async (req, res) => {
await setSchema(client); await setSchema(client);
const query = ` const query = `
SELECT SELECT
id, name, "Variante" AS "Variante", id, name, "Variante",
ST_AsGeoJSON(ST_Transform(ST_SetSRID(geom, 25832), 4326)) as geometry ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry
FROM kabeltrasse FROM kabeltrasse
ORDER BY id DESC ORDER BY id DESC
`; `;
const result = await client.query(query); const result = await client.query(query);
const featureCollection = { const featureCollection = {
type: "FeatureCollection", type: "FeatureCollection",
features: result.rows.map(row => { features: result.rows.map(row => {
@ -217,8 +210,7 @@ app.get('/api/variants', async (req, res) => {
}; };
res.json(featureCollection); res.json(featureCollection);
} catch (err) { } catch (err) {
console.error("Variants Fetch Error:", err); res.json({ type: "FeatureCollection", features: [] });
res.status(500).json({ error: 'Failed to fetch variants' });
} finally { } finally {
client.release(); client.release();
} }
@ -238,7 +230,7 @@ app.post('/api/variants', async (req, res) => {
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_GeomFromGeoJSON($1), 25832)),
$2, $2,
$3 $3
) )
@ -273,8 +265,7 @@ 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) 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("Update Owner 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' });
@ -283,6 +274,6 @@ app.patch('/api/owners/:id', async (req, res) => {
} }
}); });
app.listen(port, () => { app.listen(3000, () => {
console.log(`Server running at http://localhost:${port}`); console.log(`Server running at http://localhost:3000`);
}); });