From 3fa8315d622e895392be98345589e775e543b87e Mon Sep 17 00:00:00 2001 From: leandrofars Date: Sun, 24 Dec 2023 11:19:53 -0300 Subject: [PATCH] fix(controller): list device by id --- backend/services/controller/internal/api/device.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/services/controller/internal/api/device.go b/backend/services/controller/internal/api/device.go index af4e798..8c0675b 100644 --- a/backend/services/controller/internal/api/device.go +++ b/backend/services/controller/internal/api/device.go @@ -2,11 +2,12 @@ package api import ( "encoding/json" - "go.mongodb.org/mongo-driver/bson" "log" "net/http" "strconv" + "go.mongodb.org/mongo-driver/bson" + "github.com/gorilla/mux" "github.com/leandrofars/oktopus/internal/db" usp_msg "github.com/leandrofars/oktopus/internal/usp_message" @@ -37,11 +38,15 @@ func (a *Api) retrieveDevices(w http.ResponseWriter, r *http.Request) { const PAGE_SIZE_DEFAULT = 20 // Get specific device - id := mux.Vars(r)["id"] + id := r.URL.Query().Get("id") if id != "" { device, err := a.Db.RetrieveDevice(id) if err != nil { - log.Println(err) + if err == mongo.ErrNoDocuments { + json.NewEncoder(w).Encode("Device id: " + id + " not found") + return + } + json.NewEncoder(w).Encode(err) w.WriteHeader(http.StatusInternalServerError) return }