From 7dcff7adac410ddb5a2bd8d169e77072deb52f55 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Mon, 24 Oct 2022 09:15:05 -0700 Subject: [PATCH] add bookingHandler --- api/handlers/api.go | 4 +++ api/handlers/booking.go | 55 +++++++++++++++++++++++++++++++++++++++++ models/cow.go | 9 ++++--- 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 api/handlers/booking.go diff --git a/api/handlers/api.go b/api/handlers/api.go index f33d6a5..41bfba6 100644 --- a/api/handlers/api.go +++ b/api/handlers/api.go @@ -36,6 +36,7 @@ func (a *App) New() *mux.Router { apiCreate := r.PathPrefix("/api/v1").Subrouter() + // Data handlers, create, delete, update etc. apiCreate.Handle("/cow/{cow_id}", api.Middleware(http.HandlerFunc(cow.CowByIDHandler))).Methods("GET") // By Object ID not Cow Name apiCreate.Handle("/cows", api.Middleware(http.HandlerFunc(cow.CowHandler))).Methods("GET") // Returns all cows apiCreate.Handle("/cows", api.Middleware(http.HandlerFunc(cow.CowHandlerQuery))).Methods("POST") // Returns list of cows based of name query @@ -50,6 +51,9 @@ func (a *App) New() *mux.Router { apiCreate.Handle("/devices/new", api.Middleware(http.HandlerFunc(device.NewDeviceHandler))).Methods("POST") // create new device apiCreate.Handle("/devices/update/{device_id}", api.Middleware(http.HandlerFunc(device.UpdateDeviceHandler))).Methods("POST") // Update Device by Object ID + // Booking handling + apiCreate.Handle("/cow/book/{cow_id}", api.Middleware(http.HandlerFunc(cow.BookingHandler))).Methods("POST") // Add booking to cow by ID + return r } diff --git a/api/handlers/booking.go b/api/handlers/booking.go new file mode 100644 index 0000000..4a3a9fe --- /dev/null +++ b/api/handlers/booking.go @@ -0,0 +1,55 @@ +package handlers + +import ( + "context" + "encoding/json" + "net/http" + "time" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + + "github.com/SowinskiBraeden/SulliCartShare/config" + "github.com/SowinskiBraeden/SulliCartShare/models" + "github.com/gorilla/mux" +) + +func (c Cow) BookingHandler(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + var bookingDetails models.BookDetails // Json data will represent the cow details model + defer cancel() + + // validate the request body + if err := json.NewDecoder(r.Body).Decode(&bookingDetails); 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(&bookingDetails); validationErr != nil { + config.ErrorStatus("invalid request body", http.StatusBadRequest, w, validationErr) + return + } + + cowID := mux.Vars(r)["cow_id"] + + cID, err := primitive.ObjectIDFromHex(cowID) + if err != nil { + config.ErrorStatus("failed to get objectID from Hex", http.StatusBadRequest, w, err) + return + } + + dbResp, err := c.DB.UpdateOne(ctx, bson.M{"_id": cID}, bson.M{"$push": bson.M{"Cow.Bookings": bookingDetails}}) + if err != nil { + config.ErrorStatus("the booking could not be added to the cow", http.StatusNotFound, w, err) + return + } + + b, err := json.Marshal(models.UserResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"result": dbResp}}) + if err != nil { + config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err) + return + } + w.WriteHeader(http.StatusOK) + w.Write(b) +} diff --git a/models/cow.go b/models/cow.go index 6734e5f..f8f9ee7 100644 --- a/models/cow.go +++ b/models/cow.go @@ -12,10 +12,11 @@ type Cow struct { // 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 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 ID's + Block string `json:"block" bson:"Block"` // Block that is booked + StartDate primitive.DateTime `json:"startdate" bson:"StartDate"` // Date this booking occurs + EndDate primitive.DateTime `json:"enddate" bson:"EndDate"` // Date this booking ends } // CowDetails holds the structure for the inner cow structure as