add bookingHandler

This commit is contained in:
SowinskiBraeden committed 2022-10-24 09:15:05 -07:00
1 parent d70b69f79b
commit 7dcff7adac
3 files changed
+64 -4

No files matched your search

+4
View File
@@ -36,6 +36,7 @@ func (a *App) New() *mux.Router {
apiCreate := r.PathPrefix("/api/v1").Subrouter() 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("/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.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 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/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 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 return r
} }
+55
View File
@@ -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)
}
+5 -4
View File
@@ -12,10 +12,11 @@ type Cow struct {
// 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 ID's Devices []string `json:"devices" bson:"Devices"` // Array of device 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 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 // CowDetails holds the structure for the inner cow structure as