From 3e20caa2d9debba72dfef584d15b1aff3bc2b040 Mon Sep 17 00:00:00 2001 From: Jocadbz Date: Tue, 9 Jun 2026 18:49:46 -0300 Subject: [PATCH] Fix ConnectionEdit silently NULLing out port references The edit handler was fetching the connection twice but never using the result, then constructing a new Connection struct without PortID1/PortID2 fields. The DB update would write NULL into both port columns, severing the cable from both ends. Now the handler fetches the existing row once and updates only the submitted metadata fields in place. --- internal/handlers/connections.go | 48 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/internal/handlers/connections.go b/internal/handlers/connections.go index 215a266..dca784c 100644 --- a/internal/handlers/connections.go +++ b/internal/handlers/connections.go @@ -50,41 +50,43 @@ func (h *Handlers) ConnectionEdit(w http.ResponseWriter, r *http.Request) { id, _ := strconv.ParseInt(idStr, 10, 64) r.ParseForm() - _, err := h.Store.ConnectionGetByID(id) - if err != nil { + conn, err := h.Store.ConnectionGetByID(id) + if err != nil || conn == nil { http.Error(w, "connection not found", http.StatusNotFound) return } - connTypeID, _ := strconv.ParseInt(r.FormValue("connection_type_id"), 10, 64) + if ctStr := r.FormValue("connection_type_id"); ctStr != "" { + ctID, err := strconv.ParseInt(ctStr, 10, 64) + if err == nil && ctID > 0 { + conn.ConnectionTypeID = ctID + } + } + label1 := r.FormValue("label_1") - label2 := r.FormValue("label_2") - color := r.FormValue("color") - returnPortID := r.FormValue("return_port_id") - - if color == "" { - color = "#808080" - } - - var label1Ptr, label2Ptr *string if label1 != "" { - label1Ptr = &label1 + conn.Label1 = &label1 + } else { + conn.Label1 = nil } + + label2 := r.FormValue("label_2") if label2 != "" { - label2Ptr = &label2 + conn.Label2 = &label2 + } else { + conn.Label2 = nil } - _, err = h.Store.ConnectionGetByID(id) - if err == nil { - _ = h.Store.ConnectionUpdate(&models.Connection{ - ID: id, - ConnectionTypeID: connTypeID, - Label1: label1Ptr, - Label2: label2Ptr, - Color: color, - }) + if color := r.FormValue("color"); color != "" { + conn.Color = color } + if err := h.Store.ConnectionUpdate(conn); err != nil { + http.Error(w, "Failed to update connection", http.StatusInternalServerError) + return + } + + returnPortID := r.FormValue("return_port_id") if returnPortID != "" { h.renderConnectionModal(w, returnPortID) return