diff --git a/api/handlers/cow.go b/api/handlers/cow.go index e2823b4..f00e760 100644 --- a/api/handlers/cow.go +++ b/api/handlers/cow.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "net/http" + "reflect" "time" "github.com/gorilla/mux" @@ -116,7 +117,6 @@ func (c Cow) NewCowHandler(w http.ResponseWriter, r *http.Request) { // use the validator library to validate required fields if validationErr := validate.Struct(&cowDetails); validationErr != nil { config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr) - return } @@ -137,17 +137,39 @@ func (c Cow) NewCowHandler(w http.ResponseWriter, r *http.Request) { } // UpdateCowHandler gets updates the data for an existing cow and returns a result and error +// This function can only handle updating Name, DeviceTotal, Collection func (c Cow) UpdateCowHandler(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + var newDetails models.CowDetails // Json data will represent the cow details model + defer cancel() + cowID := mux.Vars(r)["cow_id"] - // TODO: Collect data to update from passed json - update := bson.M{ - "$set": bson.M{ - "cow.CowCode": "123", - }, + // validate the request body + if err := json.NewDecoder(r.Body).Decode(&newDetails); err != nil { + config.ErrorStatus("failed to unpack request body", http.StatusInternalServerError, w, err) + return } - dbResp, err := c.DB.UpdateOne(context.Background(), bson.M{"_id": cowID}, update) + // use the validator library to validate required fields + if validationErr := validate.Struct(&newDetails); validationErr != nil { + config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr) + return + } + + e := reflect.ValueOf(&newDetails).Elem() + var update bson.M = bson.M{} + + // Only get provided values to update + for i := 0; i < e.NumField(); i++ { + varName := e.Type().Field(i).Name + varValue := e.Field(i).Interface() + if varValue != nil && varValue != "" && varName != "Bookings" && varName != "Devices" { + update["Cow."+varName] = varValue + } + } + + dbResp, err := c.DB.UpdateOne(ctx, bson.M{"_id": cowID}, bson.M{"$set": update}) if err != nil { config.ErrorStatus("the cow could not be updated", http.StatusNotFound, w, err) return diff --git a/api/handlers/device.go b/api/handlers/device.go index eeadd58..f790a9a 100644 --- a/api/handlers/device.go +++ b/api/handlers/device.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "net/http" + "reflect" "time" "github.com/gorilla/mux" @@ -132,16 +133,37 @@ func (d Device) NewDeviceHandler(w http.ResponseWriter, r *http.Request) { // UpdateCowHandler gets updates the data for an existing cow and returns a result and error func (d Device) UpdateDeviceHandler(w http.ResponseWriter, r *http.Request) { - deviceID := mux.Vars(r)["cow_id"] + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + var newDetails models.DeviceDetails // Json data will represent the cow details model + defer cancel() - // TODO: Collect data to update from passed json - update := bson.M{ - "$set": bson.M{ - "cow.CowCode": "123", - }, + deviceID := mux.Vars(r)["device_id"] + + // validate the request body + if err := json.NewDecoder(r.Body).Decode(&newDetails); err != nil { + config.ErrorStatus("failed to unpack request body", http.StatusInternalServerError, w, err) + return } - dbResp, err := d.DB.UpdateOne(context.Background(), bson.M{"_id": deviceID}, update) + // use the validator library to validate required fields + if validationErr := validate.Struct(&newDetails); validationErr != nil { + config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr) + return + } + + e := reflect.ValueOf(&newDetails).Elem() + var update bson.M = bson.M{} + + // Only get provided values to update + for i := 0; i < e.NumField(); i++ { + varName := e.Type().Field(i).Name + varValue := e.Field(i).Interface() + if varValue != nil && varValue != "" { + update["Device."+varName] = varValue + } + } + + dbResp, err := d.DB.UpdateOne(ctx, bson.M{"_id": deviceID}, bson.M{"$set": update}) if err != nil { config.ErrorStatus("the device could not be updated", http.StatusNotFound, w, err) return diff --git a/models/cow.go b/models/cow.go index cd96dfb..7f5e380 100644 --- a/models/cow.go +++ b/models/cow.go @@ -7,23 +7,23 @@ import ( // Cow holds the structure for the cow collection in mongo type Cow struct { ID string `json:"_id" bson:"_id"` // MongoDB ID - Details CowDetails `json:"cow" bson:"cow"` // Details + Details CowDetails `json:"cow" bson:"Cow"` // Details } // BookDetails holds the checkout details type BookDetails struct { - Author string `json:"author" bson:"author"` // User who booked - Devices []string `json:"devices" bson:"devices"` // Array of device mongo ID's - Block string `json:"block" bson:"block"` // Block that is booked - Date primitive.DateTime `json:"date" bson:"date"` // Date this booking occurs + Author string `json:"author" bson:"Author"` // User who booked + Devices []string `json:"devices" bson:"Devices"` // Array of device mongo ID's + Block string `json:"block" bson:"Block"` // Block that is booked + Date primitive.DateTime `json:"date" bson:"Date"` // Date this booking occurs } // CowDetails holds the structure for the inner cow structure as // defined in the cow collection in mongo type CowDetails struct { - Name string `json:"name" bson:"name"` // eg. CA-01 - Collection string `json:"collection" bson:"collection"` // eg. Laptop, Ipad, etc - TotalDevices int `json:"totalDevices" bson:"totalDevices"` // # of devices in that cart collection - Bookings []BookDetails `json:"bookings" bson:"bookings"` // An array of all active bookings (send top 10) - Devices []string `json:"devices" bson:"devices"` // Array of device mongo ID's + Name string `json:"name" bson:"Name"` // eg. CA-01 + Collection string `json:"collection" bson:"Collection"` // eg. Laptop, Ipad, etc + DeviceTotal int `json:"deviceTotal" bson:"DeviceTotal"` // # of devices in that cart collection + Bookings []BookDetails `json:"bookings" bson:"Bookings"` // An array of all active bookings (send top 10) + Devices []string `json:"devices" bson:"Devices"` // Array of device mongo ID's } diff --git a/models/device.go b/models/device.go index f05278e..ce0f09f 100644 --- a/models/device.go +++ b/models/device.go @@ -2,13 +2,13 @@ package models // Device holds the structure for the device collection in mongo type Device struct { - ID string `json:"_id" bson:"_id"` // MongoDB ID - Details DeviceDetails `json:"details" bson:"details"` // Details + ID string `json:"_id" bson:"_id"` // MongoDB ID + Details DeviceDetails `json:"device" bson:"Device"` // Details } // Device holds the structure for the Device collection in mongo type DeviceDetails struct { - Type string `json:"type" bson:"type"` // eg. Laptop, Ipdad, etc - Name string `json:"name" bson:"name"` // eg. SULH-LAP-01 - Parent string `json:"parent" bson:"parent"` // Parent cow Mongo ID + Type string `json:"type" bson:"Type"` // eg. Laptop, Ipdad, etc + Name string `json:"name" bson:"Name"` // eg. SULH-LAP-01 + Parent string `json:"parent" bson:"Parent"` // Parent cow Mongo ID }