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