From 4a25fab944e91de1f5cbd3af4a89a0ebf830d62e Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Thu, 29 Sep 2022 14:18:29 -0700 Subject: [PATCH] add devices to cow --- api/handlers/api.go | 21 +++++++++++---------- api/handlers/cow.go | 35 +++++++++++++++++++++++++++++++++++ models/io.go | 5 +++++ 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/api/handlers/api.go b/api/handlers/api.go index b845668..9739e1b 100644 --- a/api/handlers/api.go +++ b/api/handlers/api.go @@ -36,17 +36,18 @@ 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 CowCode - 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 query - apiCreate.Handle("/cows/new", api.Middleware(http.HandlerFunc(cow.NewCowHandler))).Methods("POST") - apiCreate.Handle("/cows/update/{cow_id}", api.Middleware(http.HandlerFunc(cow.UpdateCowHandler))).Methods("POST") // By Object ID not CowCode + 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("/device/{device_id}", api.Middleware(http.HandlerFunc(device.DeviceByIDHandler))).Methods("GET") - apiCreate.Handle("/devices", api.Middleware(http.HandlerFunc(device.DeviceHandler))).Methods("GET") - apiCreate.Handle("/devices", api.Middleware(http.HandlerFunc(device.DeviceHandlerQuery))).Methods("POST") - apiCreate.Handle("/devices/new", api.Middleware(http.HandlerFunc(device.NewDeviceHandler))).Methods("POST") - apiCreate.Handle("/devices/update/{device_id}", api.Middleware(http.HandlerFunc(device.UpdateDeviceHandler))).Methods("POST") + 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.DeviceHandlerQuery))).Methods("POST") // Returns list of devices based of name query + apiCreate.Handle("/devices/new", api.Middleware(http.HandlerFunc(device.NewDeviceHandler))).Methods("POST") // create new device + apiCreate.Handle("/devices/update/{device_id}", api.Middleware(http.HandlerFunc(device.UpdateDeviceHandler))).Methods("POST") // Update Device by Object ID return r } diff --git a/api/handlers/cow.go b/api/handlers/cow.go index f00e760..b75dcb1 100644 --- a/api/handlers/cow.go +++ b/api/handlers/cow.go @@ -183,3 +183,38 @@ func (c Cow) UpdateCowHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write(b) } + +func (c Cow) AddDeviceHandler(w http.ResponseWriter, r *http.Request) { + var newDevice models.NewDeviceToCow + + cowID := mux.Vars(r)["cow_id"] + + // validate the request body + if err := json.NewDecoder(r.Body).Decode(&newDevice); err != nil { + config.ErrorStatus("failed to unpack request body", http.StatusInternalServerError, w, err) + return + } + + // use the validator library to validate required fields + if validationErr := validate.Struct(&newDevice); validationErr != nil { + config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr) + return + } + + update := bson.M{ + "cow.Devices": newDevice, + } + + dbResp, err := c.DB.UpdateOne(context.TODO(), bson.M{"_id": cowID}, bson.M{"$push": update}) + if err != nil { + config.ErrorStatus("the device could not be inserted into the cow", http.StatusNotFound, w, err) + return + } + b, err := json.Marshal(dbResp) + if err != nil { + config.ErrorStatus("failed ot marshal response", http.StatusInternalServerError, w, err) + return + } + w.WriteHeader(http.StatusOK) + w.Write(b) +} diff --git a/models/io.go b/models/io.go index 6e584fc..38b6199 100644 --- a/models/io.go +++ b/models/io.go @@ -29,3 +29,8 @@ type MessageError struct { type Query struct { Name string `json:"name"` } + +// NewDeviceToCow to add device object id to cow +type NewDeviceToCow struct { + ID string `json:"_id"` +}