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
parent
fcb04be887
commit
3e20caa2d9
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue