morz-infoboard/server/backend/internal/httpapi/manage/display.go
Jesko Anschütz 79fcc20b79 fix(display): screen UUID lookup, authScreen middleware, JSON encoding
- playerstatus: look up screen by slug before UpsertDisplayState to pass UUID (not slug) and avoid FK violation
- router: switch display command route from authOnly to authScreen for proper permission enforcement
- display.go: remove redundant GetBySlug + requireScreenAccess (now handled by authScreen middleware), drop store dependency
- notifier: replace fmt.Sprintf %q with json.Marshal for correct JSON encoding of display command payload

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-26 23:35:05 +01:00

44 lines
1.1 KiB
Go

package manage
import (
"encoding/json"
"log/slog"
"net/http"
"git.az-it.net/az/morz-infoboard/server/backend/internal/mqttnotifier"
)
// HandleDisplayCommand nimmt {"state":"on"} oder {"state":"off"} entgegen und
// schickt den entsprechenden MQTT-Befehl an den Agent.
func HandleDisplayCommand(notifier *mqttnotifier.Notifier) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
screenSlug := r.PathValue("screenSlug")
var body struct {
State string `json:"state"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, "invalid JSON", http.StatusBadRequest)
return
}
var action string
switch body.State {
case "on":
action = "display_on"
case "off":
action = "display_off"
default:
http.Error(w, `state must be "on" or "off"`, http.StatusBadRequest)
return
}
if err := notifier.SendDisplayCommand(screenSlug, action); err != nil {
slog.Error("send display command", "err", err)
http.Error(w, "failed to send command", http.StatusBadGateway)
return
}
w.WriteHeader(http.StatusNoContent)
}
}