fix(controller): list device by id

This commit is contained in:
leandrofars 2023-12-24 11:19:53 -03:00
parent e207bd0f0a
commit 3fa8315d62

View File

@ -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
}