This commit is contained in:
2023-09-26 16:42:46 +02:00
parent f5938d23df
commit e0b7d73e74
13 changed files with 348 additions and 298 deletions

View File

@@ -11,6 +11,7 @@ import (
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
// BookingStatuus describes the status of a booking
type BookingStatus int64
const (
@@ -46,6 +47,7 @@ var bookingStatustoString = map[BookingStatus]string{
BookingValidated: "VALIDATED",
}
// MarshalJSON transforms a booking status to its JSON representation
func (s BookingStatus) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(bookingStatustoString[s])
@@ -53,10 +55,12 @@ func (s BookingStatus) MarshalJSON() ([]byte, error) {
return buffer.Bytes(), nil
}
// MarshalBSON transforms a booking status to its BSON representation for MongoDB storage
func (s BookingStatus) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(bookingStatustoString[s])
}
// UnmarshalJSON transforms JSON to a BookingStatus
func (bs *BookingStatus) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
@@ -67,6 +71,7 @@ func (bs *BookingStatus) UnmarshalJSON(b []byte) error {
return nil
}
// UnmarshalBSONValue transforms BSON from MongoDB to a BookingStatus
func (bs *BookingStatus) UnmarshalBSONValue(t bsontype.Type, b []byte) error {
if t == bsontype.Null || len(b) == 0 {
return nil
@@ -79,6 +84,7 @@ func (bs *BookingStatus) UnmarshalBSONValue(t bsontype.Type, b []byte) error {
return nil
}
// String returns the string corresponding to the BookingStatus
func (bs *BookingStatus) String() string {
if bs == nil {
return ""
@@ -86,10 +92,12 @@ func (bs *BookingStatus) String() string {
return bookingStatustoString[*bs]
}
// BookingStatusFromString creates a new BookingStatus from the given string
func BookingStatusFromString(bs string) BookingStatus {
return bookingStatustoID[bs]
}
// Booking describes the booking parameters and status, in the context of a "Booking by API" OCSS flow
type Booking struct {
ID string `json:"id" bson:"_id"`
Driver User `json:"driver"`

View File

@@ -9,6 +9,7 @@ import (
"go.mongodb.org/mongo-driver/bson/bsontype"
)
// CarpoolBookingStatus describes he state of the booking event
type CarpoolBookingStatus int64
const (