diff --git a/api/handlers/api.go b/api/handlers/api.go index f31b4b0..f49e872 100644 --- a/api/handlers/api.go +++ b/api/handlers/api.go @@ -36,13 +36,13 @@ func (a *App) New() *mux.Router { apiCreate := r.PathPrefix("/api/v1").Subrouter() - apiCreate.Handle("/cow/{cow_id}", api.Middleware(http.HandlerFunc(cow.CowByIDHandler))).Methods("GET") // By Object ID not Cow Name - apiCreate.Handle("/cows", api.Middleware(http.HandlerFunc(cow.CowHandler))).Methods("GET") // Returns all cows - apiCreate.Handle("/cows", api.Middleware(http.HandlerFunc(cow.CowHandlerQuery))).Methods("POST") // Returns list of cows based of name query - apiCreate.Handle("/cows/new", api.Middleware(http.HandlerFunc(cow.NewCowHandler))).Methods("POST") // Create new cow - apiCreate.Handle("/cows/update/{cow_id}", api.Middleware(http.HandlerFunc(cow.UpdateCowHandler))).Methods("POST") // Update Cow by Object ID - apiCreate.Handle("/cows/add/{cow_id}", api.Middleware(http.HandlerFunc(cow.AddDeviceHandler))).Methods("POST") // Add Device to cow device list - apiCreate.Handle("/cows/getChildDevices", api.Middleware(http.HandlerFunc(device.GetChildDevices))).Methods("POST") // Returns a list of devices from a given Cow obj + apiCreate.Handle("/cow/{cow_id}", api.Middleware(http.HandlerFunc(cow.CowByIDHandler))).Methods("GET") // By Object ID not Cow Name + apiCreate.Handle("/cows", api.Middleware(http.HandlerFunc(cow.CowHandler))).Methods("GET") // Returns all cows + apiCreate.Handle("/cows", api.Middleware(http.HandlerFunc(cow.CowHandlerQuery))).Methods("POST") // Returns list of cows based of name query + apiCreate.Handle("/cows/new", api.Middleware(http.HandlerFunc(cow.NewCowHandler))).Methods("POST") // Create new cow + apiCreate.Handle("/cows/update/{cow_id}", api.Middleware(http.HandlerFunc(cow.UpdateCowHandler))).Methods("POST") // Update Cow by Object ID + apiCreate.Handle("/cows/add/{cow_id}", api.Middleware(http.HandlerFunc(cow.AddDeviceHandler))).Methods("POST") // Add Device to cow device list + apiCreate.Handle("/cows/getChildDevices/{cow_id}", api.Middleware(http.HandlerFunc(device.GetChildDevices))).Methods("POST") // Returns a list of devices from a given Cow obj apiCreate.Handle("/device/{device_id}", api.Middleware(http.HandlerFunc(device.DeviceByIDHandler))).Methods("GET") // By Object ID not Device Name apiCreate.Handle("/devices", api.Middleware(http.HandlerFunc(device.DeviceHandler))).Methods("GET") // Returns all devices diff --git a/api/handlers/device.go b/api/handlers/device.go index c4fc796..72736bb 100644 --- a/api/handlers/device.go +++ b/api/handlers/device.go @@ -58,7 +58,7 @@ func (d Device) DeviceHandlerQuery(w http.ResponseWriter, r *http.Request) { return } - dbResp, err := d.DB.Find(context.TODO(), bson.M{"detials.name": query.Name}) // Search by cow name + dbResp, err := d.DB.Find(context.TODO(), bson.M{"detials.name": query.Name}) // Search by device name if err != nil { config.ErrorStatus("failed to get cow(s)", http.StatusNotFound, w, err) return @@ -185,27 +185,14 @@ func (d Device) UpdateDeviceHandler(w http.ResponseWriter, r *http.Request) { // Is able to get child devices with a prodived Cow Model func (d Device) GetChildDevices(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - var parentCow models.Cow // Json data will represent the cow details model + cowID := mux.Vars(r)["cow_id"] defer cancel() - // validate the request body - if err := json.NewDecoder(r.Body).Decode(&parentCow); err != nil { - config.ErrorStatus("failed to unpack request body", http.StatusInternalServerError, w, err) - return - } + devices, _ := d.DB.Find(ctx, bson.M{"detials.parent": cowID}) - // use the validator library to validate required fields - if validationErr := validate.Struct(&parentCow); validationErr != nil { - config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr) - return - } - - devices := []models.Device{} - - for _, deviceID := range parentCow.Details.Devices { - dbResp, _ := d.DB.FindOne(ctx, bson.M{"_id": deviceID}) - - devices = append(devices, *dbResp) + // If there is no devices from the query, return empty device array. + if len(devices) == 0 { + devices = []models.Device{} } b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": devices}})