This commit is contained in:
2023-03-30 08:44:58 +02:00
parent bf6453b963
commit 0ae5730e7f
17 changed files with 305 additions and 44 deletions

View File

@@ -3,7 +3,9 @@ package storage
import (
"context"
"encoding/json"
"fmt"
"git.coopgo.io/coopgo-platform/carpool-service/internal"
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
@@ -21,12 +23,14 @@ type MongoDBStorage struct {
func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
var (
mongodb_host = cfg.GetString("storage.db.mongodb.host")
mongodb_port = cfg.GetString("storage.db.mongodb.port")
mongodb_dbname = cfg.GetString("storage.db.mongodb.db_name")
mongodb_regular_routes = cfg.GetString("storage.db.mongodb.collections.regular_routes")
mongodb_punctual_routes = cfg.GetString("storage.db.mongodb.collections.punctual_routes")
mongodb_bookings = cfg.GetString("storage.db.mongodb.collections.bookings")
mongodb_host = cfg.GetString("storage.db.mongodb.host")
mongodb_port = cfg.GetString("storage.db.mongodb.port")
mongodb_dbname = cfg.GetString("storage.db.mongodb.db_name")
mongodb_regular_routes = cfg.GetString("storage.db.mongodb.collections.regular_routes")
mongodb_punctual_routes = cfg.GetString("storage.db.mongodb.collections.punctual_routes")
mongodb_bookings = cfg.GetString("storage.db.mongodb.collections.bookings")
mongodb_driver_candidate_journeys = cfg.GetString("storage.db.mongodb.collections.driver_candidate_journeys")
mongodb_passenger_candidate_journeys = cfg.GetString("storage.db.mongodb.collections.passenger_candidate_journeys")
)
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + mongodb_host + ":" + mongodb_port))
@@ -44,9 +48,11 @@ func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
Client: client,
DbName: mongodb_dbname,
Collections: map[string]string{
"regular_routes": mongodb_regular_routes,
"punctual_routes": mongodb_punctual_routes,
"bookings": mongodb_bookings,
"regular_routes": mongodb_regular_routes,
"punctual_routes": mongodb_punctual_routes,
"bookings": mongodb_bookings,
"driver_candidate_journeys": mongodb_driver_candidate_journeys,
"passenger_candidate_journeys": mongodb_passenger_candidate_journeys,
},
}
@@ -209,6 +215,53 @@ func (s MongoDBStorage) GetPassengerRegularRoutesForTile(day string, gridId int6
return results, nil
}
func (s MongoDBStorage) StoreSearchResults(driverOrPassenger string, searchresults []internal.SearchResult) error {
log.Debug().Msg("Storage - CreateRegularRoutes")
documents := []any{}
for _, sr := range searchresults {
documents = append(documents, sr)
}
collection := s.Client.Database(s.DbName).Collection(s.Collections[fmt.Sprintf("%s_candidate_journeys", driverOrPassenger)])
if _, err := collection.InsertMany(context.TODO(), documents); err != nil {
return err
}
return nil
}
func (s MongoDBStorage) GetSearchResult(driverOrPassenger, id string) (searchResult *internal.SearchResult, err error) {
collection := s.Client.Database(s.DbName).Collection(s.Collections[fmt.Sprintf("%s_candidate_journeys", driverOrPassenger)])
err = collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(searchResult)
return
}
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")

View File

@@ -3,6 +3,7 @@ package storage
import (
"fmt"
"git.coopgo.io/coopgo-platform/carpool-service/internal"
"github.com/paulmach/orb/geojson"
"github.com/spf13/viper"
)
@@ -12,6 +13,12 @@ type Storage interface {
GetUserRegularRoutes(userid string) ([]*geojson.FeatureCollection, error)
GetDriverRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error)
GetPassengerRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error)
StoreSearchResults(string, []internal.SearchResult) error
GetSearchResult(driverOrPassenger, id string) (*internal.SearchResult, error)
CreateBooking(internal.Booking) error
GetBooking(id string) (*internal.Booking, error)
}
func NewStorage(cfg *viper.Viper) (Storage, error) {