90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"lostcavewireplanner/internal/models"
|
|
)
|
|
|
|
type WallSocketData struct {
|
|
Sockets []models.Device
|
|
Connections []models.Connection
|
|
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{}
|
|
}
|
|
|
|
var conns []models.Connection
|
|
for _, s := range sockets {
|
|
for _, p := range s.Ports {
|
|
conn, err := h.Store.ConnectionGetByPortID(p.ID)
|
|
if err != nil || conn == nil {
|
|
continue
|
|
}
|
|
conns = append(conns, *conn)
|
|
}
|
|
}
|
|
if conns == nil {
|
|
conns = []models.Connection{}
|
|
}
|
|
|
|
h.render(w, "wall_sockets.html", WallSocketData{
|
|
Sockets: sockets,
|
|
Connections: conns,
|
|
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")
|
|
}
|