diff --git a/api/handlers/booking.go b/api/handlers/booking.go index 4a3a9fe..ca9678f 100644 --- a/api/handlers/booking.go +++ b/api/handlers/booking.go @@ -3,6 +3,7 @@ package handlers import ( "context" "encoding/json" + "fmt" "net/http" "time" @@ -12,6 +13,7 @@ import ( "github.com/SowinskiBraeden/SulliCartShare/config" "github.com/SowinskiBraeden/SulliCartShare/models" "github.com/gorilla/mux" + "github.com/thanhpk/randstr" ) func (c Cow) BookingHandler(w http.ResponseWriter, r *http.Request) { @@ -39,6 +41,8 @@ func (c Cow) BookingHandler(w http.ResponseWriter, r *http.Request) { return } + bookingDetails.ID = fmt.Sprintf("%s.%s", cowID, randstr.Hex(16)) + 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) diff --git a/go.mod b/go.mod index 323305b..eaced57 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/go-playground/validator/v10 v10.11.1 github.com/gorilla/mux v1.8.0 github.com/joho/godotenv v1.4.0 + github.com/thanhpk/randstr v1.0.4 go.mongodb.org/mongo-driver v1.10.2 go.uber.org/zap v1.23.0 ) diff --git a/go.sum b/go.sum index 2e84ebc..2fcb4b8 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/thanhpk/randstr v1.0.4 h1:IN78qu/bR+My+gHCvMEXhR/i5oriVHcTB/BJJIRTsNo= +github.com/thanhpk/randstr v1.0.4/go.mod h1:M/H2P1eNLZzlDwAzpkkkUvoyNNMbzRGhESZuEQk3r0U= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= diff --git a/models/cow.go b/models/cow.go index f8f9ee7..cff1e18 100644 --- a/models/cow.go +++ b/models/cow.go @@ -12,6 +12,7 @@ type Cow struct { // BookDetails holds the checkout details type BookDetails struct { + ID string `json:"id" bson:"ID"` // Generated ID -> cow_id.random_str 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