diff --git a/internal/handlers/devices.go b/internal/handlers/devices.go index aa3d784..407b413 100644 --- a/internal/handlers/devices.go +++ b/internal/handlers/devices.go @@ -9,8 +9,8 @@ import ( type DeviceViewData struct { Device models.Device + Connections []models.Connection ConnectionTypes []models.ConnectionType - AllPorts []models.DevicePort Error string } @@ -28,6 +28,7 @@ func (h *Handlers) DeviceView(w http.ResponseWriter, r *http.Request) { return } + conns := h.connectionsForDevice(device) connTypes, _ := h.Store.ConnectionTypeGetAll() if connTypes == nil { connTypes = []models.ConnectionType{} @@ -35,10 +36,29 @@ func (h *Handlers) DeviceView(w http.ResponseWriter, r *http.Request) { 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() @@ -135,6 +155,7 @@ func (h *Handlers) renderDeviceError(w http.ResponseWriter, device models.Device } h.render(w, "device.html", DeviceViewData{ Device: device, + Connections: h.connectionsForDevice(&device), ConnectionTypes: connTypes, Error: errMsg, }) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 1cbead3..0a4ebb5 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -52,7 +52,10 @@ func New(store *db.Store) *Handlers { baseFiles := []string{filepath.Join("templates", "base.html")} for _, page := range pages { - files := append(baseFiles, filepath.Join("templates", page)) + files := append(baseFiles, + filepath.Join("templates", page), + filepath.Join("templates", "_port_list.html"), + ) t, err := template.New("base.html").Funcs(funcMap).ParseFiles(files...) if err != nil { panic(fmt.Errorf("parse %s: %w", page, err)) diff --git a/internal/handlers/wall_sockets.go b/internal/handlers/wall_sockets.go index f6a396c..3867900 100644 --- a/internal/handlers/wall_sockets.go +++ b/internal/handlers/wall_sockets.go @@ -8,9 +8,10 @@ import ( ) type WallSocketData struct { - Sockets []models.Device - Models []models.DeviceModel - Error string + Sockets []models.Device + Connections []models.Connection + Models []models.DeviceModel + Error string } func (h *Handlers) WallSockets(w http.ResponseWriter, r *http.Request) { @@ -30,9 +31,24 @@ func (h *Handlers) WallSockets(w http.ResponseWriter, r *http.Request) { 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, - Models: wallModels, + Sockets: sockets, + Connections: conns, + Models: wallModels, }) } diff --git a/templates/_port_list.html b/templates/_port_list.html new file mode 100644 index 0000000..e2f2f94 --- /dev/null +++ b/templates/_port_list.html @@ -0,0 +1,32 @@ +{{define "port_list"}} +