fix/update handler
This commit is contained in:
4 files changed
+72
-28
No files matched your search
+29
-7
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"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
|
// use the validator library to validate required fields
|
||||||
if validationErr := validate.Struct(&cowDetails); validationErr != nil {
|
if validationErr := validate.Struct(&cowDetails); validationErr != nil {
|
||||||
config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr)
|
config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr)
|
||||||
|
|
||||||
return
|
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
|
// 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) {
|
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"]
|
cowID := mux.Vars(r)["cow_id"]
|
||||||
|
|
||||||
// TODO: Collect data to update from passed json
|
// validate the request body
|
||||||
update := bson.M{
|
if err := json.NewDecoder(r.Body).Decode(&newDetails); err != nil {
|
||||||
"$set": bson.M{
|
config.ErrorStatus("failed to unpack request body", http.StatusInternalServerError, w, err)
|
||||||
"cow.CowCode": "123",
|
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 {
|
if err != nil {
|
||||||
config.ErrorStatus("the cow could not be updated", http.StatusNotFound, w, err)
|
config.ErrorStatus("the cow could not be updated", http.StatusNotFound, w, err)
|
||||||
return
|
return
|
||||||
|
|||||||
+29
-7
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"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
|
// 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) {
|
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
|
deviceID := mux.Vars(r)["device_id"]
|
||||||
update := bson.M{
|
|
||||||
"$set": bson.M{
|
// validate the request body
|
||||||
"cow.CowCode": "123",
|
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 {
|
if err != nil {
|
||||||
config.ErrorStatus("the device could not be updated", http.StatusNotFound, w, err)
|
config.ErrorStatus("the device could not be updated", http.StatusNotFound, w, err)
|
||||||
return
|
return
|
||||||
|
|||||||
+10
-10
@@ -7,23 +7,23 @@ import (
|
|||||||
// Cow holds the structure for the cow collection in mongo
|
// Cow holds the structure for the cow collection in mongo
|
||||||
type Cow struct {
|
type Cow struct {
|
||||||
ID string `json:"_id" bson:"_id"` // MongoDB ID
|
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
|
// BookDetails holds the checkout details
|
||||||
type BookDetails struct {
|
type BookDetails struct {
|
||||||
Author string `json:"author" bson:"author"` // User who booked
|
Author string `json:"author" bson:"Author"` // User who booked
|
||||||
Devices []string `json:"devices" bson:"devices"` // Array of device mongo ID's
|
Devices []string `json:"devices" bson:"Devices"` // Array of device mongo ID's
|
||||||
Block string `json:"block" bson:"block"` // Block that is booked
|
Block string `json:"block" bson:"Block"` // Block that is booked
|
||||||
Date primitive.DateTime `json:"date" bson:"date"` // Date this booking occurs
|
Date primitive.DateTime `json:"date" bson:"Date"` // Date this booking occurs
|
||||||
}
|
}
|
||||||
|
|
||||||
// CowDetails holds the structure for the inner cow structure as
|
// CowDetails holds the structure for the inner cow structure as
|
||||||
// defined in the cow collection in mongo
|
// defined in the cow collection in mongo
|
||||||
type CowDetails struct {
|
type CowDetails struct {
|
||||||
Name string `json:"name" bson:"name"` // eg. CA-01
|
Name string `json:"name" bson:"Name"` // eg. CA-01
|
||||||
Collection string `json:"collection" bson:"collection"` // eg. Laptop, Ipad, etc
|
Collection string `json:"collection" bson:"Collection"` // eg. Laptop, Ipad, etc
|
||||||
TotalDevices int `json:"totalDevices" bson:"totalDevices"` // # of devices in that cart collection
|
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)
|
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
|
Devices []string `json:"devices" bson:"Devices"` // Array of device mongo ID's
|
||||||
}
|
}
|
||||||
+4
-4
@@ -3,12 +3,12 @@ package models
|
|||||||
// Device holds the structure for the device collection in mongo
|
// Device holds the structure for the device collection in mongo
|
||||||
type Device struct {
|
type Device struct {
|
||||||
ID string `json:"_id" bson:"_id"` // MongoDB ID
|
ID string `json:"_id" bson:"_id"` // MongoDB ID
|
||||||
Details DeviceDetails `json:"details" bson:"details"` // Details
|
Details DeviceDetails `json:"device" bson:"Device"` // Details
|
||||||
}
|
}
|
||||||
|
|
||||||
// Device holds the structure for the Device collection in mongo
|
// Device holds the structure for the Device collection in mongo
|
||||||
type DeviceDetails struct {
|
type DeviceDetails struct {
|
||||||
Type string `json:"type" bson:"type"` // eg. Laptop, Ipdad, etc
|
Type string `json:"type" bson:"Type"` // eg. Laptop, Ipdad, etc
|
||||||
Name string `json:"name" bson:"name"` // eg. SULH-LAP-01
|
Name string `json:"name" bson:"Name"` // eg. SULH-LAP-01
|
||||||
Parent string `json:"parent" bson:"parent"` // Parent cow Mongo ID
|
Parent string `json:"parent" bson:"Parent"` // Parent cow Mongo ID
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user