package handlers import ( "net/http" "strconv" "lostcavewireplanner/internal/models" ) type WallSocketData struct { Sockets []models.Device Models []models.DeviceModel Error string } func (h *Handlers) WallSockets(w http.ResponseWriter, r *http.Request) { sockets, _ := h.Store.DeviceGetAllWallSockets() if sockets == nil { sockets = []models.Device{} } allModels, _ := h.Store.ModelGetAll() wallModels := []models.DeviceModel{} for _, m := range allModels { if m.IsWallSocket { wallModels = append(wallModels, m) } } if wallModels == nil { wallModels = []models.DeviceModel{} } h.render(w, "wall_sockets.html", WallSocketData{ Sockets: sockets, Models: wallModels, }) } func (h *Handlers) WallSocketCreate(w http.ResponseWriter, r *http.Request) { r.ParseForm() name := r.FormValue("name") modelIDStr := r.FormValue("device_model_id") modelID, _ := strconv.ParseInt(modelIDStr, 10, 64) location := r.FormValue("location") comment := r.FormValue("comment") if name == "" || modelID == 0 { h.redirect(w, r, "/wall-sockets") return } device := &models.Device{ DeviceModelID: modelID, Name: name, Location: location, Comment: comment, } _, err := h.Store.DeviceCreate(device) if err != nil { h.redirect(w, r, "/wall-sockets") return } h.redirect(w, r, "/wall-sockets") } func (h *Handlers) WallSocketDelete(w http.ResponseWriter, r *http.Request) { idStr := r.PathValue("id") id, _ := strconv.ParseInt(idStr, 10, 64) h.Store.DeviceDelete(id) h.redirect(w, r, "/wall-sockets") }