Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75aad49

Browse files
committed
temp: oops
1 parent ec868cf commit 75aad49

37 files changed

Lines changed: 396 additions & 377 deletions

cmd/web/handlers/handlers.go

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ import (
55
"fmt"
66
"net/http"
77
"sd/cmd/web/views/partials"
8-
"sd/pkg/buttons"
9-
"sd/pkg/devices"
10-
"sd/pkg/instance"
118
"sd/pkg/natsconn"
12-
"sd/pkg/pages"
13-
"sd/pkg/profiles"
9+
"sd/pkg/store"
1410
"strconv"
1511

1612
"github.com/go-chi/chi/v5"
@@ -53,7 +49,7 @@ func HandleButtonPress(w http.ResponseWriter, r *http.Request) {
5349
pageID := chi.URLParam(r, "pageId")
5450
buttonID := chi.URLParam(r, "buttonId")
5551

56-
var button, err = buttons.GetButton("instances." + instanceID + ".devices." + deviceID + ".profiles." + profileID + ".pages." + pageID + ".buttons." + buttonID)
52+
var button, err = store.GetButton("instances." + instanceID + ".devices." + deviceID + ".profiles." + profileID + ".pages." + pageID + ".buttons." + buttonID)
5753

5854
if err != nil {
5955
log.Error().Err(err).Msg("Failed to get button")
@@ -70,32 +66,14 @@ func HandleButtonPress(w http.ResponseWriter, r *http.Request) {
7066
}
7167

7268
func HandleDeviceCardList(w http.ResponseWriter, r *http.Request) {
73-
log.Info().Msg("Handling device list request")
7469
instanceID := chi.URLParam(r, "instanceId")
75-
76-
devices, err := devices.GetDevices(instanceID)
77-
78-
if err != nil {
79-
log.Error().Err(err).Msg("Failed to get devices")
80-
http.Error(w, err.Error(), http.StatusInternalServerError)
81-
return
82-
}
83-
84-
log.Info().Interface("devices", devices).Msg("Found devices")
85-
partials.DeviceCardList(instanceID, devices).Render(r.Context(), w)
70+
instance := store.GetInstance(instanceID)
71+
devices := store.GetDevices(instanceID)
72+
partials.DeviceCardList(instance, devices).Render(r.Context(), w)
8673
}
8774

8875
func HandleInstanceCardList(w http.ResponseWriter, r *http.Request) {
89-
log.Info().Msg("Handling instance list request")
90-
91-
instances, err := instance.GetInstances()
92-
if err != nil {
93-
log.Error().Err(err).Msg("Failed to get instances")
94-
http.Error(w, err.Error(), http.StatusInternalServerError)
95-
return
96-
}
97-
98-
log.Info().Interface("instances", instances).Msg("Found instances")
76+
instances := store.GetInstances()
9977
partials.InstanceCardList(instances).Render(r.Context(), w)
10078
}
10179

@@ -104,13 +82,17 @@ func HandleProfileAddDialog() http.HandlerFunc {
10482
instanceID := r.URL.Query().Get("instanceId")
10583
deviceID := r.URL.Query().Get("deviceId")
10684

107-
component := partials.ProfileAddDialog(instanceID, deviceID)
85+
instance := store.GetInstance(instanceID)
86+
device := store.GetDevice(instanceID, deviceID)
87+
88+
component := partials.ProfileAddDialog(instance, device)
10889
component.Render(r.Context(), w)
10990
}
11091
}
11192

11293
func HandleProfileCreate(w http.ResponseWriter, r *http.Request) {
11394
err := r.ParseForm()
95+
11496
if err != nil {
11597
http.Error(w, err.Error(), http.StatusBadRequest)
11698
return
@@ -120,37 +102,31 @@ func HandleProfileCreate(w http.ResponseWriter, r *http.Request) {
120102
deviceID := r.FormValue("deviceId")
121103
name := r.FormValue("name")
122104

123-
log.Info().Str("instanceId", instanceID).Str("deviceId", deviceID).Str("name", name).Msg("Creating profile")
105+
instance := store.GetInstance(instanceID)
106+
device := store.GetDevice(instanceID, deviceID)
124107

125-
profile, err := profiles.CreateProfile(instanceID, deviceID, name)
108+
profile, err := store.CreateProfile(instanceID, deviceID, name)
126109

127110
if err != nil {
128111
log.Error().Err(err).Msg("Failed to create profile")
129112
http.Error(w, err.Error(), http.StatusInternalServerError)
130113
return
131114
}
132115

133-
page, err := pages.CreatePage(instanceID, deviceID, profile.ID)
116+
page, err := store.CreatePage(instanceID, deviceID, profile.ID)
134117

135118
if err != nil {
136119
log.Error().Err(err).Msg("Failed to create page")
137120
http.Error(w, err.Error(), http.StatusInternalServerError)
138121
return
139122
}
140123

141-
// I want to create 32 blank buttons for the page
142-
for i := 0; i < 32; i++ {
143-
buttons.CreateButton(instanceID, deviceID, profile.ID, page.ID, strconv.Itoa(i))
124+
for i := 0; i < 32; i++ { // TODO: Make this configurable
125+
store.CreateButton(instanceID, deviceID, profile.ID, page.ID, strconv.Itoa(i))
144126
}
145127

146-
profiles, err := profiles.GetProfiles(instanceID, deviceID)
147-
148-
if err != nil {
149-
http.Error(w, err.Error(), http.StatusInternalServerError)
150-
return
151-
}
152-
153-
partials.ProfileCardList(instanceID, deviceID, profiles).Render(r.Context(), w)
128+
profiles := store.GetProfiles(instanceID, deviceID)
129+
partials.ProfileCardList(instance, device, profiles).Render(r.Context(), w)
154130
}
155131

156132
func HandleProfileDeleteDialog() http.HandlerFunc {
@@ -159,15 +135,11 @@ func HandleProfileDeleteDialog() http.HandlerFunc {
159135
deviceID := r.URL.Query().Get("deviceId")
160136
profileID := r.URL.Query().Get("profileId")
161137

162-
profile, err := profiles.GetProfile(instanceID, deviceID, profileID)
138+
instance := store.GetInstance(instanceID)
139+
device := store.GetDevice(instanceID, deviceID)
140+
profile := store.GetProfile(instanceID, deviceID, profileID)
163141

164-
if err != nil {
165-
log.Error().Err(err).Msg("Failed to get profile")
166-
http.Error(w, err.Error(), http.StatusInternalServerError)
167-
return
168-
}
169-
170-
component := partials.ProfileDeleteDialog(instanceID, deviceID, *profile)
142+
component := partials.ProfileDeleteDialog(instance, device, profile)
171143
component.Render(r.Context(), w)
172144
}
173145
}
@@ -178,22 +150,18 @@ func HandleProfileDelete() http.HandlerFunc {
178150
deviceID := r.URL.Query().Get("deviceId")
179151
profileID := r.URL.Query().Get("profileId")
180152

181-
err := profiles.DeleteProfile(instanceID, deviceID, profileID)
153+
err := store.DeleteProfile(instanceID, deviceID, profileID)
182154

183155
if err != nil {
184156
log.Error().Err(err).Msg("Failed to delete profile")
185157
http.Error(w, err.Error(), http.StatusInternalServerError)
186158
return
187159
}
188160

189-
profiles, err := profiles.GetProfiles(instanceID, deviceID)
190-
191-
if err != nil {
192-
log.Error().Err(err).Msg("Failed to get profiles after deletion")
193-
http.Error(w, err.Error(), http.StatusInternalServerError)
194-
return
195-
}
161+
instance := store.GetInstance(instanceID)
162+
device := store.GetDevice(instanceID, deviceID)
163+
profiles := store.GetProfiles(instanceID, deviceID)
196164

197-
partials.ProfileCardList(instanceID, deviceID, profiles).Render(r.Context(), w)
165+
partials.ProfileCardList(instance, device, profiles).Render(r.Context(), w)
198166
}
199167
}

cmd/web/server/server.go

Lines changed: 34 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import (
1313

1414
"sd/cmd/web/handlers"
1515
"sd/cmd/web/views/partials"
16-
"sd/pkg/devices"
17-
"sd/pkg/instance"
1816
"sd/pkg/natsconn"
19-
"sd/pkg/profiles"
17+
"sd/pkg/store"
2018
"sd/pkg/types"
2119
)
2220

@@ -87,81 +85,47 @@ func NewServer() *Server {
8785
func (s *Server) setupRoutes() {
8886
// Routes
8987
s.router.Get("/", func(w http.ResponseWriter, r *http.Request) {
90-
instances, err := instance.GetInstances()
91-
if err != nil {
92-
http.Error(w, err.Error(), http.StatusInternalServerError)
93-
return
94-
}
88+
instances := store.GetInstances()
9589
partials.HomePage(instances).Render(r.Context(), w)
9690
})
9791

9892
s.router.Get("/instance/{instanceID}", func(w http.ResponseWriter, r *http.Request) {
99-
instances, err := instance.GetInstances()
10093
instanceID := chi.URLParam(r, "instanceID")
101-
if err != nil {
102-
http.Error(w, err.Error(), http.StatusInternalServerError)
103-
return
104-
}
105-
devices, err := devices.GetDevices(instanceID)
106-
if err != nil {
107-
http.Error(w, err.Error(), http.StatusInternalServerError)
108-
return
109-
}
110-
partials.InstancePage(instances, devices).Render(r.Context(), w)
94+
95+
instances := store.GetInstances()
96+
devices := store.GetDevices(instanceID)
97+
instance := store.GetInstance(instanceID)
98+
partials.InstancePage(instance, instances, devices).Render(r.Context(), w)
11199
})
112100

113101
s.router.Get("/instance/{instanceID}/device/{deviceID}", func(w http.ResponseWriter, r *http.Request) {
114-
instances, err := instance.GetInstances()
102+
instances := store.GetInstances()
115103
instanceID := chi.URLParam(r, "instanceID")
116104
deviceID := chi.URLParam(r, "deviceID")
117-
if err != nil {
118-
http.Error(w, err.Error(), http.StatusInternalServerError)
119-
return
120-
}
121-
devices, err := devices.GetDevices(instanceID)
122-
if err != nil {
123-
http.Error(w, err.Error(), http.StatusInternalServerError)
124-
return
125-
}
126-
profiles, err := profiles.GetProfiles(instanceID, deviceID)
127-
if err != nil {
128-
http.Error(w, err.Error(), http.StatusInternalServerError)
129-
return
130-
}
131-
pages, err := s.getPages()
132-
if err != nil {
133-
http.Error(w, err.Error(), http.StatusInternalServerError)
134-
return
135-
}
136-
partials.DevicePage(instances, devices, profiles, pages, instanceID, deviceID).Render(r.Context(), w)
105+
106+
devices := store.GetDevices(instanceID)
107+
profiles := store.GetProfiles(instanceID, deviceID)
108+
pages := store.GetPages(instanceID, deviceID, profiles[0].ID) // TODO: Fix this
109+
instance := store.GetInstance(instanceID)
110+
device := store.GetDevice(instanceID, deviceID)
111+
partials.DevicePage(instances, devices, profiles, pages, instance, device).Render(r.Context(), w)
137112
})
138113

139114
s.router.Get("/instance/{instanceID}/device/{deviceID}/profile/{profileID}/page/{pageID}", func(w http.ResponseWriter, r *http.Request) {
140-
instances, err := instance.GetInstances()
141115
instanceID := chi.URLParam(r, "instanceID")
142116
deviceID := chi.URLParam(r, "deviceID")
143117
profileID := chi.URLParam(r, "profileID")
144118
pageID := chi.URLParam(r, "pageID")
145-
if err != nil {
146-
http.Error(w, err.Error(), http.StatusInternalServerError)
147-
return
148-
}
149-
devices, err := devices.GetDevices(instanceID)
150-
if err != nil {
151-
http.Error(w, err.Error(), http.StatusInternalServerError)
152-
return
153-
}
154-
profiles, err := profiles.GetProfiles(instanceID, deviceID)
155-
if err != nil {
156-
http.Error(w, err.Error(), http.StatusInternalServerError)
157-
return
158-
}
159-
pages, err := s.getPages()
160-
if err != nil {
161-
http.Error(w, err.Error(), http.StatusInternalServerError)
162-
return
163-
}
164-
partials.ProfilePage(instances, devices, profiles, pages, instanceID, deviceID, profileID, pageID).Render(r.Context(), w)
119+
120+
instances := store.GetInstances()
121+
devices := store.GetDevices(instanceID)
122+
instance := store.GetInstance(instanceID)
123+
device := store.GetDevice(instanceID, deviceID)
124+
profile := store.GetProfile(instanceID, deviceID, profileID)
125+
page := store.GetPage(instanceID, deviceID, profileID, pageID)
126+
profiles := store.GetProfiles(instanceID, deviceID)
127+
pages := store.GetPages(instanceID, deviceID, profileID)
128+
partials.ProfilePage(instances, devices, profiles, pages, instance, device, profile, page).Render(r.Context(), w)
165129
})
166130

167131
// HTMX Routes
@@ -201,14 +165,15 @@ func (s *Server) setupRoutes() {
201165
defer watcher.Stop()
202166

203167
// 4. Send initial device list
204-
d, err := devices.GetDevices(instanceID)
168+
d := store.GetDevices(instanceID)
205169

206170
log.Info().Interface("devices", d).Msg("Initial devices")
207171

208172
if err != nil {
209173
log.Error().Err(err).Msg("Failed to get initial devices")
210174
} else {
211-
if err := s.sendDeviceList(w, r.Context(), instanceID, d); err != nil {
175+
instance := store.GetInstance(instanceID)
176+
if err := s.sendDeviceList(w, r.Context(), instance, d); err != nil {
212177
log.Error().Err(err).Msg("Failed to send initial device list")
213178
return
214179
}
@@ -234,13 +199,10 @@ func (s *Server) setupRoutes() {
234199
case <-done:
235200
return
236201
default:
237-
d, err := devices.GetDevices(instanceID)
238-
if err != nil {
239-
log.Error().Err(err).Msg("Failed to get devices")
240-
continue
241-
}
202+
d := store.GetDevices(instanceID)
203+
instance := store.GetInstance(instanceID)
242204

243-
if err := s.sendDeviceList(w, r.Context(), instanceID, d); err != nil {
205+
if err := s.sendDeviceList(w, r.Context(), instance, d); err != nil {
244206
if err != context.Canceled {
245207
log.Error().Err(err).Msg("Failed to send device list")
246208
}
@@ -264,9 +226,10 @@ func (s *Server) setupRoutes() {
264226
}
265227

266228
// Move sendDeviceList outside setupRoutes
267-
func (s *Server) sendDeviceList(w http.ResponseWriter, ctx context.Context, instanceID string, devices []types.Device) error {
229+
func (s *Server) sendDeviceList(w http.ResponseWriter, ctx context.Context, instance types.Instance, devices []types.Device) error {
268230
var buf bytes.Buffer
269-
if err := partials.DeviceCardList(instanceID, devices).Render(ctx, &buf); err != nil {
231+
232+
if err := partials.DeviceCardList(instance, devices).Render(ctx, &buf); err != nil {
270233
return err
271234
}
272235

@@ -280,10 +243,6 @@ func (s *Server) sendDeviceList(w http.ResponseWriter, ctx context.Context, inst
280243
return nil
281244
}
282245

283-
func (s *Server) getPages() ([]types.Page, error) {
284-
return nil, nil
285-
}
286-
287246
func (s *Server) Start() error {
288247
port := os.Getenv("PORT")
289248
if port == "" {

cmd/web/views/partials/device_card.templ

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"sd/pkg/types"
88
)
99

10-
templ DeviceCard(instanceID string, device types.Device) {
10+
templ DeviceCard(instance types.Instance, device types.Device) {
1111
<a
1212
class="block p-3 bg-sd-dark rounded cursor-pointer hover:bg-sd-light transition-colors"
1313
id={ "device-card-" + device.ID }
14-
href={ templ.SafeURL("/instance/" + instanceID + "/device/" + device.ID) }
14+
href={ templ.SafeURL("/instance/" + instance.ID + "/device/" + device.ID) }
1515
>
1616
//hx-get={ "/instance/" + device.Instance + "/device/" + device.ID }
1717
//hx-select="#main-content"

cmd/web/views/partials/device_card_list.templ

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package partials
33

44
import "sd/pkg/types"
55

6-
templ DeviceCardList(instanceID string, devices []types.Device) {
6+
templ DeviceCardList(instance types.Instance, devices []types.Device) {
77
<div
88
id="device-card-list"
99
class="space-y-2"
@@ -12,7 +12,7 @@ templ DeviceCardList(instanceID string, devices []types.Device) {
1212
<div class="text-gray-400">No devices connected</div>
1313
} else {
1414
for _, device := range devices {
15-
@DeviceCard(instanceID, device)
15+
@DeviceCard(instance, device)
1616
}
1717
}
1818
</div>

cmd/web/views/partials/device_card_list_templ.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)