use errorStatus & move error structs to responses.go
This commit is contained in:
3 files changed
+18
-27
No files matched your search
+7
-15
@@ -3,7 +3,6 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -77,24 +76,19 @@ func (c Cow) NewCowHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
var cowDetails models.CowDetails // Json data will represent the cow details model
|
var cowDetails models.CowDetails // Json data will represent the cow details model
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
//validate the request body
|
// validate the request body
|
||||||
if err := json.NewDecoder(r.Body).Decode(&cowDetails); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&cowDetails); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
config.ErrorStatus("failed to unpack request body", http.StatusInternalServerError, w, err)
|
||||||
response := models.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"error": err.Error()}}
|
|
||||||
json.NewEncoder(w).Encode(response)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//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 {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr)
|
||||||
response := models.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: map[string]interface{}{"error": validationErr.Error()}}
|
|
||||||
json.NewEncoder(w).Encode(response)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(cowDetails)
|
|
||||||
|
|
||||||
newCow := models.Cow{
|
newCow := models.Cow{
|
||||||
ID: primitive.NewObjectID().Hex(),
|
ID: primitive.NewObjectID().Hex(),
|
||||||
Details: cowDetails,
|
Details: cowDetails,
|
||||||
@@ -102,9 +96,7 @@ func (c Cow) NewCowHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
result, err := c.DB.InsertOne(ctx, newCow)
|
result, err := c.DB.InsertOne(ctx, newCow)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
config.ErrorStatus("failed to insert cow", http.StatusBadRequest, w, err)
|
||||||
response := models.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"error": err.Error()}}
|
|
||||||
json.NewEncoder(w).Encode(response)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +118,7 @@ func (c Cow) UpdateCowHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
dbResp, err := c.DB.UpdateOne(context.Background(), bson.M{"_id": cowID}, update)
|
dbResp, err := c.DB.UpdateOne(context.Background(), bson.M{"_id": cowID}, update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.ErrorStatus("failed to update cow by ID", http.StatusNotFound, w, err)
|
config.ErrorStatus("the cow could not be updated", http.StatusNotFound, w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
// ErrorMessageResponse returns the error message response struct
|
|
||||||
type ErrorMessageResponse struct {
|
|
||||||
Response MessageError
|
|
||||||
}
|
|
||||||
|
|
||||||
// MessageError contains the inner details for the error message response
|
|
||||||
type MessageError struct {
|
|
||||||
Message string
|
|
||||||
Error string
|
|
||||||
}
|
|
||||||
@@ -11,3 +11,14 @@ type UserResponse struct {
|
|||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Data map[string]interface{} `json:"data"`
|
Data map[string]interface{} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrorMessageResponse returns the error message response struct
|
||||||
|
type ErrorMessageResponse struct {
|
||||||
|
Response MessageError
|
||||||
|
}
|
||||||
|
|
||||||
|
// MessageError contains the inner details for the error message response
|
||||||
|
type MessageError struct {
|
||||||
|
Message string
|
||||||
|
Error string
|
||||||
|
}
|
||||||
Reference in new issue
Block a user