diff --git a/index.html b/index.html
index 0bfdab0..eeab02c 100644
--- a/index.html
+++ b/index.html
@@ -2731,10 +2731,12 @@
const indicator = document.getElementById('db-status-indicator');
if (indicator) indicator.classList.add('active');
+ try {
try {
// Ensure we have a valid variant object and points
- const v = variantDef.id ? variantDef : state.variants.find(vx => vx.id === variantDef.id);
- if (!v) {
+ if (!variantDef.id && !variantDef.name) return;
+ const v = variantDef.id ? variantDef : state.variants.find(vx => vx.id === variantDef.id || vx.name === variantDef.name);
+ if (!v || !v.routes || v.routes.length < 2) {
if (indicator) indicator.classList.remove('active');
return;
}
diff --git a/server.js b/server.js
index b2d8ba3..0e9ec44 100644
--- a/server.js
+++ b/server.js
@@ -226,22 +226,37 @@ app.post('/api/variants', async (req, res) => {
const routeName = properties.name || 'Neue Trasse';
const varianteValue = properties.Variante || (properties.name ? properties.name.replace('Variante ', '') : 'A');
const geoJsonStr = JSON.stringify(geometry);
+
+ // Safety Check: DON'T delete/save if geometry is empty or invalid
+ if (!geometry || !geometry.coordinates || geometry.coordinates.length < 2) {
+ console.log(`Aborting save for ${varianteValue}: Geometry is too short or empty.`);
+ return res.json({ success: true, message: 'Skipped empty geometry' });
+ }
- // Delete existing variant if it exists (Manual UPSERT to bypass missing index permissions)
- await client.query('DELETE FROM kabeltrasse WHERE "Variante" = $1 OR name = $2', [varianteValue, routeName]);
+ try {
+ await client.query('BEGIN');
+
+ // Delete existing variant if it exists
+ await client.query('DELETE FROM kabeltrasse WHERE "Variante" = $1 OR name = $2', [varianteValue, routeName]);
- const insertQuery = `
- INSERT INTO kabeltrasse (geom, name, "Variante")
- VALUES (
- ST_MakeValid(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), 4326), 25832)),
- $2,
- $3
- )
- RETURNING id
- `;
-
- const insertRes = await client.query(insertQuery, [geoJsonStr, routeName, varianteValue]);
- res.json({ success: true, id: insertRes.rows[0].id });
+ const insertQuery = `
+ INSERT INTO kabeltrasse (geom, name, "Variante")
+ VALUES (
+ ST_Multi(ST_MakeValid(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), 4326), 25832))),
+ $2,
+ $3
+ )
+ RETURNING id
+ `;
+
+ const insertRes = await client.query(insertQuery, [geoJsonStr, routeName, varianteValue]);
+ await client.query('COMMIT');
+
+ res.json({ success: true, id: insertRes.rows[0].id });
+ } catch (sqlErr) {
+ await client.query('ROLLBACK');
+ throw sqlErr;
+ }
} catch (err) {
console.error("SQL-FEHLER (SAVE):", err.message);
res.status(500).json({ error: 'Failed to save variant' });