update api responses
This commit is contained in:
2 files changed
+31
-21
No files matched your search
+15
-10
@@ -33,7 +33,7 @@ func (c Cow) CowHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
if len(dbResp) == 0 {
|
if len(dbResp) == 0 {
|
||||||
dbResp = []models.Cow{}
|
dbResp = []models.Cow{}
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(dbResp)
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
@@ -68,7 +68,7 @@ func (c Cow) CowHandlerQuery(w http.ResponseWriter, r *http.Request) {
|
|||||||
if len(dbResp) == 0 {
|
if len(dbResp) == 0 {
|
||||||
dbResp = []models.Cow{}
|
dbResp = []models.Cow{}
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(dbResp)
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
@@ -93,9 +93,9 @@ func (c Cow) CowByIDHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.Marshal(dbResp)
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to marshal responce", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@@ -131,9 +131,13 @@ func (c Cow) NewCowHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusCreated, Message: "success", Data: map[string]interface{}{"result": result}})
|
||||||
|
if err != nil {
|
||||||
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
response := models.UserResponse{Status: http.StatusCreated, Message: "success", Data: map[string]interface{}{"result": result}}
|
w.Write(b)
|
||||||
json.NewEncoder(w).Encode(response)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -175,9 +179,9 @@ func (c Cow) UpdateCowHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.Marshal(dbResp)
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed ot marshal response", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@@ -210,9 +214,10 @@ func (c Cow) AddDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
config.ErrorStatus("the device could not be inserted into the cow", http.StatusNotFound, w, err)
|
config.ErrorStatus("the device could not be inserted into the cow", http.StatusNotFound, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(dbResp)
|
|
||||||
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed ot marshal response", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|||||||
+16
-11
@@ -33,7 +33,8 @@ func (d Device) DeviceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
if len(dbResp) == 0 {
|
if len(dbResp) == 0 {
|
||||||
dbResp = []models.Device{}
|
dbResp = []models.Device{}
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(dbResp)
|
|
||||||
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
@@ -60,7 +61,7 @@ func (d Device) DeviceHandlerQuery(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
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 cow name
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to get cows", http.StatusNotFound, w, err)
|
config.ErrorStatus("failed to get cow(s)", http.StatusNotFound, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +69,8 @@ func (d Device) DeviceHandlerQuery(w http.ResponseWriter, r *http.Request) {
|
|||||||
if len(dbResp) == 0 {
|
if len(dbResp) == 0 {
|
||||||
dbResp = []models.Device{}
|
dbResp = []models.Device{}
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(dbResp)
|
|
||||||
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
@@ -87,9 +89,9 @@ func (d Device) DeviceByIDHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.Marshal(dbResp)
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to marshal responce", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@@ -111,7 +113,6 @@ func (d Device) NewDeviceHandler(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(&deviceDetails); validationErr != nil {
|
if validationErr := validate.Struct(&deviceDetails); validationErr != nil {
|
||||||
config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr)
|
config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,13 +123,17 @@ func (d Device) NewDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
result, err := d.DB.InsertOne(ctx, newDevice)
|
result, err := d.DB.InsertOne(ctx, newDevice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to insert device", http.StatusBadRequest, w, err)
|
config.ErrorStatus("failed to insert device", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusCreated, Message: "success", Data: map[string]interface{}{"result": result}})
|
||||||
|
if err != nil {
|
||||||
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
response := models.UserResponse{Status: http.StatusCreated, Message: "success", Data: map[string]interface{}{"result": result}}
|
w.Write(b)
|
||||||
json.NewEncoder(w).Encode(response)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -169,9 +174,9 @@ func (d Device) UpdateDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.Marshal(dbResp)
|
b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed ot marshal response", http.StatusInternalServerError, w, err)
|
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|||||||
Reference in new issue
Block a user