optimize GetChildDevice function
This commit is contained in:
2 files changed
+13
-26
No files matched your search
+7
-7
@@ -36,13 +36,13 @@ func (a *App) New() *mux.Router {
|
|||||||
|
|
||||||
apiCreate := r.PathPrefix("/api/v1").Subrouter()
|
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("/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.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", 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/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/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/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("/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("/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
|
apiCreate.Handle("/devices", api.Middleware(http.HandlerFunc(device.DeviceHandler))).Methods("GET") // Returns all devices
|
||||||
|
|||||||
+6
-19
@@ -58,7 +58,7 @@ func (d Device) DeviceHandlerQuery(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to get cow(s)", http.StatusNotFound, w, err)
|
config.ErrorStatus("failed to get cow(s)", http.StatusNotFound, w, err)
|
||||||
return
|
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
|
// Is able to get child devices with a prodived Cow Model
|
||||||
func (d Device) GetChildDevices(w http.ResponseWriter, r *http.Request) {
|
func (d Device) GetChildDevices(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
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()
|
defer cancel()
|
||||||
|
|
||||||
// validate the request body
|
devices, _ := d.DB.Find(ctx, bson.M{"detials.parent": cowID})
|
||||||
if err := json.NewDecoder(r.Body).Decode(&parentCow); err != nil {
|
|
||||||
config.ErrorStatus("failed to unpack request body", http.StatusInternalServerError, w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// use the validator library to validate required fields
|
// If there is no devices from the query, return empty device array.
|
||||||
if validationErr := validate.Struct(&parentCow); validationErr != nil {
|
if len(devices) == 0 {
|
||||||
config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr)
|
devices = []models.Device{}
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
devices := []models.Device{}
|
|
||||||
|
|
||||||
for _, deviceID := range parentCow.Details.Devices {
|
|
||||||
dbResp, _ := d.DB.FindOne(ctx, bson.M{"_id": deviceID})
|
|
||||||
|
|
||||||
devices = append(devices, *dbResp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": devices}})
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": devices}})
|
||||||
|
|||||||
Reference in new issue
Block a user