package handlers import ( "net/http" "strconv" "lostcavewireplanner/internal/models" ) type DeviceViewData struct { Device models.Device ConnectionTypes []models.ConnectionType AllPorts []models.DevicePort Error string } func (h *Handlers) DeviceView(w http.ResponseWriter, r *http.Request) { idStr := r.PathValue("id") id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { http.NotFound(w, r) return } device, err := h.Store.DeviceGetByID(id) if err != nil || device == nil { http.NotFound(w, r) return } connTypes, _ := h.Store.ConnectionTypeGetAll() if connTypes == nil { connTypes = []models.ConnectionType{} } h.render(w, "device.html", DeviceViewData{ Device: *device, ConnectionTypes: connTypes, }) } func (h *Handlers) DeviceCreate(w http.ResponseWriter, r *http.Request) { r.ParseForm() name := r.FormValue("name") modelIDStr := r.FormValue("device_model_id") modelID, _ := strconv.ParseInt(modelIDStr, 10, 64) usage := r.FormValue("usage_description") comment := r.FormValue("comment") location := r.FormValue("location") if name == "" || modelID == 0 { h.redirect(w, r, "/") return } device := &models.Device{ DeviceModelID: modelID, Name: name, UsageDescription: usage, Comment: comment, Location: location, } id, err := h.Store.DeviceCreate(device) if err != nil { h.redirect(w, r, "/") return } h.redirect(w, r, "/devices/"+strconv.FormatInt(id, 10)) } func (h *Handlers) DeviceEdit(w http.ResponseWriter, r *http.Request) { idStr := r.PathValue("id") id, _ := strconv.ParseInt(idStr, 10, 64) r.ParseForm() device, err := h.Store.DeviceGetByID(id) if err != nil || device == nil { http.NotFound(w, r) return } device.Name = r.FormValue("name") device.UsageDescription = r.FormValue("usage_description") device.Comment = r.FormValue("comment") device.Location = r.FormValue("location") if rackSide := r.FormValue("rack_side"); rackSide != "" { device.RackSide = &rackSide } else { device.RackSide = nil } if unitStartStr := r.FormValue("rack_unit_start"); unitStartStr != "" { us, _ := strconv.Atoi(unitStartStr) device.RackUnitStart = &us } else { device.RackUnitStart = nil } if err := h.Store.DeviceUpdate(device); err != nil { h.renderDeviceError(w, *device, err.Error()) return } redirectTo := "/devices/" + idStr if device.RackID != nil { redirectTo = "/racks/" + strconv.FormatInt(*device.RackID, 10) } h.redirect(w, r, redirectTo) } func (h *Handlers) DeviceDelete(w http.ResponseWriter, r *http.Request) { idStr := r.PathValue("id") id, _ := strconv.ParseInt(idStr, 10, 64) device, _ := h.Store.DeviceGetByID(id) rackID := device.RackID h.Store.DeviceDelete(id) if rackID != nil { h.redirect(w, r, "/racks/"+strconv.FormatInt(*rackID, 10)) } else { h.redirect(w, r, "/") } } func (h *Handlers) renderDeviceError(w http.ResponseWriter, device models.Device, errMsg string) { connTypes, _ := h.Store.ConnectionTypeGetAll() if connTypes == nil { connTypes = []models.ConnectionType{} } h.render(w, "device.html", DeviceViewData{ Device: device, ConnectionTypes: connTypes, Error: errMsg, }) }