191 lines
4.3 KiB
Go
191 lines
4.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"lostcavewireplanner/internal/models"
|
|
)
|
|
|
|
type DeviceViewData struct {
|
|
Device models.Device
|
|
Connections []models.Connection
|
|
ConnectionTypes []models.ConnectionType
|
|
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
|
|
}
|
|
|
|
conns := h.connectionsForDevice(device)
|
|
connTypes, _ := h.Store.ConnectionTypeGetAll()
|
|
if connTypes == nil {
|
|
connTypes = []models.ConnectionType{}
|
|
}
|
|
|
|
h.render(w, "device.html", DeviceViewData{
|
|
Device: *device,
|
|
Connections: conns,
|
|
ConnectionTypes: connTypes,
|
|
})
|
|
}
|
|
|
|
func (h *Handlers) connectionsForDevice(device *models.Device) []models.Connection {
|
|
if device == nil {
|
|
return []models.Connection{}
|
|
}
|
|
var allConns []models.Connection
|
|
for _, p := range device.Ports {
|
|
conn, err := h.Store.ConnectionGetByPortID(p.ID)
|
|
if err != nil || conn == nil {
|
|
continue
|
|
}
|
|
allConns = append(allConns, *conn)
|
|
}
|
|
if allConns == nil {
|
|
return []models.Connection{}
|
|
}
|
|
return allConns
|
|
}
|
|
|
|
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) DeviceRenamePorts(w http.ResponseWriter, r *http.Request) {
|
|
idStr := r.PathValue("id")
|
|
id, _ := strconv.ParseInt(idStr, 10, 64)
|
|
|
|
device, err := h.Store.DeviceGetByID(id)
|
|
if err != nil || device == nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
r.ParseForm()
|
|
portIDs := r.Form["port_id"]
|
|
portNames := r.Form["port_name"]
|
|
|
|
for i := range portIDs {
|
|
if i >= len(portNames) {
|
|
break
|
|
}
|
|
pid, err := strconv.ParseInt(portIDs[i], 10, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
h.Store.PortUpdateName(pid, portNames[i])
|
|
}
|
|
|
|
h.redirect(w, r, "/devices/"+idStr)
|
|
}
|
|
|
|
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,
|
|
Connections: h.connectionsForDevice(&device),
|
|
ConnectionTypes: connTypes,
|
|
Error: errMsg,
|
|
})
|
|
}
|