105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"lostcavewireplanner/internal/models"
|
|
)
|
|
|
|
type OverviewData struct {
|
|
Racks []models.Rack
|
|
Devices []models.Device
|
|
Connections []models.Connection
|
|
AllModels []models.DeviceModel
|
|
Error string
|
|
}
|
|
|
|
func (h *Handlers) Overview(w http.ResponseWriter, r *http.Request) {
|
|
racks, _ := h.Store.RackGetAll()
|
|
devices, _ := h.Store.DeviceGetAllUnracked()
|
|
connections, _ := h.Store.ConnectionGetAll()
|
|
allModels, _ := h.Store.ModelGetAll()
|
|
|
|
if racks == nil {
|
|
racks = []models.Rack{}
|
|
}
|
|
if devices == nil {
|
|
devices = []models.Device{}
|
|
}
|
|
if connections == nil {
|
|
connections = []models.Connection{}
|
|
}
|
|
if allModels == nil {
|
|
allModels = []models.DeviceModel{}
|
|
}
|
|
|
|
h.render(w, "overview.html", OverviewData{
|
|
Racks: racks,
|
|
Devices: devices,
|
|
Connections: connections,
|
|
AllModels: allModels,
|
|
})
|
|
}
|
|
|
|
func (h *Handlers) RackCreate(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
name := r.FormValue("name")
|
|
rackType := r.FormValue("rack_type")
|
|
depth := r.FormValue("depth")
|
|
heightUnits := 42
|
|
if huStr := r.FormValue("height_units"); huStr != "" {
|
|
if n, err := strconv.Atoi(huStr); err == nil && n > 0 {
|
|
heightUnits = n
|
|
}
|
|
}
|
|
|
|
if name == "" {
|
|
h.renderError(w, "overview.html", "Name is required")
|
|
return
|
|
}
|
|
|
|
rack := &models.Rack{
|
|
Name: name,
|
|
RackType: rackType,
|
|
Depth: depth,
|
|
HeightUnits: heightUnits,
|
|
}
|
|
_, err := h.Store.RackCreate(rack)
|
|
if err != nil {
|
|
h.renderError(w, "overview.html", err.Error())
|
|
return
|
|
}
|
|
h.redirect(w, r, "/")
|
|
}
|
|
|
|
func renderOverviewError(h *Handlers, w http.ResponseWriter, errMsg string) {
|
|
racks, _ := h.Store.RackGetAll()
|
|
devices, _ := h.Store.DeviceGetAllUnracked()
|
|
connections, _ := h.Store.ConnectionGetAll()
|
|
allModels, _ := h.Store.ModelGetAll()
|
|
if racks == nil {
|
|
racks = []models.Rack{}
|
|
}
|
|
if devices == nil {
|
|
devices = []models.Device{}
|
|
}
|
|
if connections == nil {
|
|
connections = []models.Connection{}
|
|
}
|
|
if allModels == nil {
|
|
allModels = []models.DeviceModel{}
|
|
}
|
|
data := OverviewData{Racks: racks, Devices: devices, Connections: connections, AllModels: allModels, Error: errMsg}
|
|
h.render(w, "overview.html", data)
|
|
}
|
|
|
|
func (h *Handlers) renderError(w http.ResponseWriter, page string, errMsg string) {
|
|
switch page {
|
|
case "overview.html":
|
|
renderOverviewError(h, w, errMsg)
|
|
default:
|
|
http.Error(w, errMsg, http.StatusBadRequest)
|
|
}
|
|
}
|