From 641aae883bb3a6f1f85512f5960076c321da6091 Mon Sep 17 00:00:00 2001 From: leandrofars Date: Sun, 30 Jun 2024 20:00:03 -0300 Subject: [PATCH] feat(controller): set device alias --- .../controller/internal/api/device.go | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/backend/services/controller/internal/api/device.go b/backend/services/controller/internal/api/device.go index cd8a515..bcb68ea 100644 --- a/backend/services/controller/internal/api/device.go +++ b/backend/services/controller/internal/api/device.go @@ -2,10 +2,13 @@ package api import ( "encoding/json" + "io" "log" "net/http" "strconv" + "github.com/leandrofars/oktopus/internal/bridge" + local "github.com/leandrofars/oktopus/internal/nats" "github.com/leandrofars/oktopus/internal/utils" "github.com/nats-io/nats.go/jetstream" "go.mongodb.org/mongo-driver/bson" @@ -76,12 +79,25 @@ func (a *Api) retrieveDevices(w http.ResponseWriter, r *http.Request) { skip = 0 } - //TODO: Create filters - //TODO: Create sorting + //TODO: fix status ordering + statusOrder := r.URL.Query().Get("status") + if statusOrder != "" { + if statusOrder == "asc" { + statusOrder = "1" + } else if statusOrder == "desc" { + statusOrder = "-1" + } else { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode("Status order must be 'asc' or 'desc'") + return + } + } sort := bson.M{} sort["status"] = 1 + //TODO: Create filters + filter := bson.A{ //bson.M{"$match": filter}, bson.M{"$sort": sort}, // shows online devices first @@ -232,3 +248,37 @@ func (a *Api) deviceAuth(w http.ResponseWriter, r *http.Request) { return } } + +func (a *Api) setDeviceAlias(w http.ResponseWriter, r *http.Request) { + + id := r.URL.Query().Get("id") + if id == "" { + w.WriteHeader(http.StatusBadRequest) + utils.MarshallEncoder("No id provided", w) + return + } + + payload, err := io.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + utils.MarshallEncoder("Error to decode payload: "+err.Error(), w) + return + } + + payloadLen := len(payload) + if payloadLen == 0 { + w.WriteHeader(http.StatusBadRequest) + utils.MarshallEncoder("No alias provided", w) + return + } + if payloadLen > 50 { + w.WriteHeader(http.StatusBadRequest) + utils.MarshallEncoder("Alias too long", w) + return + } + + _, err = bridge.NatsReq[[]byte](local.NATS_ADAPTER_SUBJECT+id+".device.alias", payload, w, a.nc) + if err != nil { + return + } +}