Add PostgreSQL database option and more booking flow functionalities

This commit is contained in:
2023-05-08 01:29:59 +02:00
parent d8346a20be
commit e2e6759dc0
40 changed files with 1594 additions and 907 deletions

View File

@@ -3,6 +3,7 @@ package storage
import (
"context"
"encoding/json"
"errors"
"fmt"
"git.coopgo.io/coopgo-platform/carpool-service/internal"
@@ -222,6 +223,61 @@ func (s MongoDBStorage) GetPassengerRegularRoutesForTile(day string, gridId int6
return results, nil
}
func (s MongoDBStorage) CreateBooking(booking internal.Booking) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["bookings"])
_, err := collection.InsertOne(context.TODO(), booking)
if err != nil {
return err
}
return nil
}
func (s MongoDBStorage) GetBooking(id string) (booking *internal.Booking, err error) {
var b internal.Booking
log.Debug().Str("booking id", id).Msg("get booking in DB")
collection := s.Client.Database(s.DbName).Collection(s.Collections["bookings"])
err = collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(&b)
if err != nil {
log.Error().Err(err).Msg("error")
return nil, err
}
return &b, nil
}
func (s MongoDBStorage) GetUserBookings(userid string) ([]internal.Booking, error) {
findOptions := options.Find()
collection := s.Client.Database(s.DbName).Collection(s.Collections["bookings"])
cur, err := collection.Find(context.TODO(), bson.M{
"$or": []bson.M{
{"driver.id": userid},
{"passenger.id": userid},
},
}, findOptions)
if err != nil {
return nil, err
}
var bookings []internal.Booking
for cur.Next(context.TODO()) {
var elem internal.Booking
if err := cur.Decode(&elem); err != nil {
log.Error().Err(err).Msg("error reading bookings response")
return nil, err
}
bookings = append(bookings, elem)
}
return bookings, nil
}
func (s MongoDBStorage) UpdateBookingStatus(bookingid string, status string) error {
//TODO implement UpdateBookingStatus
return errors.New("MongoDB Storage - UpdateBookingStatus not implemented")
}
func (s MongoDBStorage) PersistedKVPut(documents []any) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["persisted_kv"])
if _, err := collection.InsertMany(context.TODO(), documents); err != nil {
@@ -239,29 +295,6 @@ func (s MongoDBStorage) PersistedKVGet(id string, document any) error {
return nil
}
func (s MongoDBStorage) StoreSearchResults(searchresults []internal.SearchResult) error {
log.Debug().Msg("Storage - StoreSearchResults")
documents := []any{}
for _, sr := range searchresults {
documents = append(documents, sr)
}
return s.PersistedKVPut(documents)
}
func (s MongoDBStorage) GetSearchResult(id string) (*internal.SearchResult, error) {
var result internal.SearchResult
err := s.PersistedKVGet(id, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (s MongoDBStorage) StoreRouteSchedules(js []internal.PlannedRouteSchedule) error {
log.Debug().Msg("Storage - StoreRouteSchedules")
@@ -282,31 +315,18 @@ func (s MongoDBStorage) GetRouteSchedule(id string) (*internal.PlannedRouteSched
return nil, err
}
for k, v := range result.Route.ExtraMembers {
if val, ok := v.(primitive.D); ok {
em := map[string]any{}
jsonbytes, _ := bson.MarshalExtJSON(val, true, true)
json.Unmarshal(jsonbytes, &em)
result.Route.ExtraMembers[k] = em
}
}
return &result, nil
}
func (s MongoDBStorage) CreateBooking(booking internal.Booking) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["bookings"])
_, err := collection.InsertOne(context.TODO(), booking)
if err != nil {
return err
}
return nil
}
func (s MongoDBStorage) GetBooking(id string) (booking *internal.Booking, err error) {
var b internal.Booking
log.Debug().Str("booking id", id).Msg("get booking in DB")
collection := s.Client.Database(s.DbName).Collection(s.Collections["bookings"])
err = collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(&b)
if err != nil {
log.Error().Err(err).Msg("error")
return nil, err
}
return &b, nil
}
// func (s MongoDBStorage) CreatePassengerRegularTrips(trips []*geojson.FeatureCollection) error {
// log.Debug().Msg("Storage - CreatePassengerRegularTrips")
@@ -328,3 +348,7 @@ func (s MongoDBStorage) GetBooking(id string) (booking *internal.Booking, err er
// return nil
// }
func (s MongoDBStorage) Migrate() error {
return nil
}