diff --git a/.codex b/.codex new file mode 100644 index 0000000..e69de29 diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..dc54f80 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "singleQuote": true, + "semi": false, + "trailingComma": "none" +} \ No newline at end of file diff --git a/backend/controllers/ipController.js b/backend/controllers/ipController.js index 7e405d8..04dfe86 100644 --- a/backend/controllers/ipController.js +++ b/backend/controllers/ipController.js @@ -35,24 +35,28 @@ exports.remove = async (req, res) => { }; + exports.update = async (req, res) => { + try { + const { ip, type, comment } = req.body - try{ - - const {ip, type, comment} = req.body; - - await db.query( - "UPDATE resource_ips SET ip=?, type=?, comment=? WHERE id=?", - [ip, type, comment, req.params.id] - ); - - res.json({success:true}); - - }catch(e){ - - console.error("UPDATE IP error:", e); - res.status(500).json({error:"DB error"}); - + if (!ip) { + return res.status(400).json({ error: "IP darf nicht leer sein" }) } -}; + await db.query( + "UPDATE resource_ips SET ip=?, type=?, comment=? WHERE id=?", + [ip, type, comment, req.params.id] + ) + + res.json({ success: true }) + + } catch (e) { + console.error("UPDATE IP error:", e) + + res.status(500).json({ + error: "IP konnte nicht gespeichert werden" + }) + } +} + diff --git a/backend/public/js/api.js b/backend/public/js/api.js index 38956cd..29901dc 100644 --- a/backend/public/js/api.js +++ b/backend/public/js/api.js @@ -1,23 +1,23 @@ +const API = "/resman/api" -const API="/resman/api"; +async function api(url, options = {}) { + const res = await fetch(url, options) -async function api(url, options={}){ + let data - const res = await fetch(url, options); + try { + data = await res.json() + } catch { + data = {} + } - if(!res.ok){ + if (!res.ok) { + const msg = data.error || "Unbekannter Fehler" - let msg="API error"; + showError(msg) - try{ - const err=await res.json(); - msg=err.error || msg; - }catch(e){} - - alert("Fehler: "+msg); - throw new Error(msg); - } - - return res.json(); -} + throw new Error(msg) + } + return data +} \ No newline at end of file diff --git a/backend/public/js/main.js b/backend/public/js/main.js index a6d3239..b517fc7 100644 --- a/backend/public/js/main.js +++ b/backend/public/js/main.js @@ -77,3 +77,25 @@ document.addEventListener("DOMContentLoaded", () => { } }) + +function showError(msg) { + + const div = document.createElement("div") + + div.innerText = msg + + div.style.position = "fixed" + div.style.bottom = "20px" + div.style.right = "20px" + div.style.background = "#dc2626" + div.style.color = "white" + div.style.padding = "10px 15px" + div.style.borderRadius = "6px" + div.style.zIndex = 9999 + + document.body.appendChild(div) + + setTimeout(() => { + div.remove() + }, 3000) +} \ No newline at end of file diff --git a/backend/public/js/resources.js b/backend/public/js/resources.js index c154211..1a709a5 100644 --- a/backend/public/js/resources.js +++ b/backend/public/js/resources.js @@ -204,7 +204,9 @@ window.saveIP = async function(){ return } } - }catch(e){} + }catch(e){ + console.log("Save IP Fehler:", e) + } await api(API + "/resources/" + resourceId + "/ips", { method: "POST", diff --git a/backend/server.js b/backend/server.js index 87638e4..9023fcb 100644 --- a/backend/server.js +++ b/backend/server.js @@ -60,3 +60,11 @@ error: err.message || "Internal Server Error" app.listen(3000, () => { console.log("ResMan running on port 3000"); }); + +app.use((err, req, res, next) => { + console.error("GLOBAL ERROR:", err) + + res.status(500).json({ + error: "Interner Serverfehler" + }) +}) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 3e28b3c..9c6b69a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: "3.9" +# version: "3.9" services: db: