From c5d488e5c15e6a903892ce746ef4a3f0dd6d6080 Mon Sep 17 00:00:00 2001 From: Johannes Baumeister Date: Mon, 20 Apr 2026 09:37:24 +0200 Subject: [PATCH] Fix WEA and Nutzung mapping after structure scan --- server.js | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/server.js b/server.js index 222272d..9aa76df 100644 --- a/server.js +++ b/server.js @@ -32,9 +32,6 @@ async function setSchema(client) { 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 --- app.get('/api/health', (req, res) => { 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(); try { await setSchema(client); - - // Strict Case-Sensitivity Mapping based on server structure const query = ` SELECT id, @@ -59,13 +54,12 @@ app.get('/api/owners', async (req, res) => { "PLZ" AS "PLZ", "Land" AS "Land", "AFlaeche" AS "AFlaeche", - status as "status", - notiz as "notiz", + status, + notiz, ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry FROM eigentuemerdaten `; const result = await client.query(query); - const geojson = { type: "FeatureCollection", features: result.rows.map(row => { @@ -95,12 +89,12 @@ app.get('/api/usage', async (req, res) => { const query = ` SELECT id, - "nutzart" as "nutzart", + nutzart AS "nutzart", + bez AS "bez", ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry FROM nutzung `; const result = await client.query(query); - const geojson = { type: "FeatureCollection", features: result.rows.map(row => { @@ -114,8 +108,8 @@ app.get('/api/usage', async (req, res) => { }; res.json(geojson); } catch (err) { - console.error("Usage Fetch Error:", err); - res.status(500).json({ error: 'Failed to fetch usage data' }); + console.error("Usage Error:", err); + res.json({ type: "FeatureCollection", features: [] }); } finally { client.release(); } @@ -129,12 +123,12 @@ app.get('/api/wea', async (req, res) => { const query = ` SELECT id, - "Name" AS "Name", + "WEA" AS "Name", + "GH" AS "GH", 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 => { @@ -148,6 +142,7 @@ app.get('/api/wea', async (req, res) => { }; res.json(geojson); } catch (err) { + console.error("WEA Error:", err); res.json({ type: "FeatureCollection", features: [] }); } finally { client.release(); @@ -166,7 +161,6 @@ app.get('/api/infrastructure', async (req, res) => { FROM infrastruktur `; const result = await client.query(query); - const geojson = { type: "FeatureCollection", features: result.rows.map(row => { @@ -193,13 +187,12 @@ app.get('/api/variants', async (req, res) => { await setSchema(client); const query = ` SELECT - id, name, "Variante" AS "Variante", - ST_AsGeoJSON(ST_Transform(ST_SetSRID(geom, 25832), 4326)) as geometry + id, name, "Variante", + ST_AsGeoJSON(ST_Transform(geom, 4326)) as geometry FROM kabeltrasse ORDER BY id DESC `; const result = await client.query(query); - const featureCollection = { type: "FeatureCollection", features: result.rows.map(row => { @@ -217,8 +210,7 @@ app.get('/api/variants', async (req, res) => { }; res.json(featureCollection); } catch (err) { - console.error("Variants Fetch Error:", err); - res.status(500).json({ error: 'Failed to fetch variants' }); + res.json({ type: "FeatureCollection", features: [] }); } finally { client.release(); } @@ -238,7 +230,7 @@ app.post('/api/variants', async (req, res) => { const upsertQuery = ` INSERT INTO kabeltrasse (geom, name, "Variante") VALUES ( - ST_MakeValid(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), 4326), 25832)), + ST_MakeValid(ST_Transform(ST_GeomFromGeoJSON($1), 25832)), $2, $3 ) @@ -273,8 +265,7 @@ 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' }); - res.json({ success: true, id: result.rows[0].id }); + res.json({ success: true, id: result.rows[0]?.id }); } catch (err) { console.error("Update Owner Error:", err); res.status(500).json({ error: 'Failed to update owner' }); @@ -283,6 +274,6 @@ app.patch('/api/owners/:id', async (req, res) => { } }); -app.listen(port, () => { - console.log(`Server running at http://localhost:${port}`); +app.listen(3000, () => { + console.log(`Server running at http://localhost:3000`); });