update responses and add query search endpoint

This commit is contained in:
SowinskiBraeden committed 2022-09-28 13:10:29 -07:00
1 parent c08a83558b
commit aaab06e14c
4 files changed
+59 -19

No files matched your search

+35 -6
View File
@@ -10,22 +10,16 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/go-playground/validator/v10"
"github.com/SowinskiBraeden/SulliCartShare/config"
"github.com/SowinskiBraeden/SulliCartShare/databases"
"github.com/SowinskiBraeden/SulliCartShare/models"
)
var validate = validator.New()
// Cow exported for testing purposes
type Cow struct {
DB databases.CowDatabase
}
// TODO: update api return messages
// CowHandler returns all cows
func (c Cow) CowHandler(w http.ResponseWriter, r *http.Request) {
dbResp, err := c.DB.Find(context.TODO(), bson.M{})
@@ -47,6 +41,41 @@ func (c Cow) CowHandler(w http.ResponseWriter, r *http.Request) {
w.Write(b)
}
// CowHandlerQuery is the same as CowHanlder, but queries a specific list of objects by Name
func (c Cow) CowHandlerQuery(w http.ResponseWriter, r *http.Request) {
var query models.Query // Json data will represent the query model
// validate the request body
if err := json.NewDecoder(r.Body).Decode(&query); 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(&query); validationErr != nil {
config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr)
return
}
dbResp, err := c.DB.Find(context.TODO(), bson.M{"Detials.Name": query.Name}) // Search by cow name
if err != nil {
config.ErrorStatus("failed to get cows", http.StatusNotFound, w, err)
return
}
// If len == 0 then we will just return an empty data object
if len(dbResp) == 0 {
dbResp = []models.Cow{}
}
b, err := json.Marshal(dbResp)
if err != nil {
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
}
// CowByIDHandler returns a cow by ID
func (c Cow) CowByIDHandler(w http.ResponseWriter, r *http.Request) {
cowID := mux.Vars(r)["cow_id"]