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.
master
Joca 2026-06-09 18:49:46 -03:00
parent fcb04be887
commit 3e20caa2d9
Signed by: jocadbz
GPG Key ID: B1836DCE2F50BDF7
1 changed files with 25 additions and 23 deletions

View File

@ -50,41 +50,43 @@ func (h *Handlers) ConnectionEdit(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(idStr, 10, 64) id, _ := strconv.ParseInt(idStr, 10, 64)
r.ParseForm() r.ParseForm()
_, err := h.Store.ConnectionGetByID(id) conn, err := h.Store.ConnectionGetByID(id)
if err != nil { if err != nil || conn == nil {
http.Error(w, "connection not found", http.StatusNotFound) http.Error(w, "connection not found", http.StatusNotFound)
return 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") 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 != "" { if label1 != "" {
label1Ptr = &label1 conn.Label1 = &label1
} else {
conn.Label1 = nil
} }
label2 := r.FormValue("label_2")
if label2 != "" { if label2 != "" {
label2Ptr = &label2 conn.Label2 = &label2
} else {
conn.Label2 = nil
} }
_, err = h.Store.ConnectionGetByID(id) if color := r.FormValue("color"); color != "" {
if err == nil { conn.Color = color
_ = h.Store.ConnectionUpdate(&models.Connection{
ID: id,
ConnectionTypeID: connTypeID,
Label1: label1Ptr,
Label2: label2Ptr,
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 != "" { if returnPortID != "" {
h.renderConnectionModal(w, returnPortID) h.renderConnectionModal(w, returnPortID)
return return