initial commit

This commit is contained in:
2023-03-27 20:54:56 +02:00
commit 77c8576254
40 changed files with 7460 additions and 0 deletions

138
storage/mongodb.go Normal file
View File

@@ -0,0 +1,138 @@
package storage
import (
"context"
"encoding/json"
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MongoDBStorage struct {
*mongo.Client
DbName string
Collections map[string]string
}
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")
)
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + mongodb_host + ":" + mongodb_port))
if err != nil {
return MongoDBStorage{}, err
}
err = client.Connect(context.TODO())
if err != nil {
return MongoDBStorage{}, err
}
storage := MongoDBStorage{
Client: client,
DbName: mongodb_dbname,
Collections: map[string]string{
"regular_routes": mongodb_regular_routes,
"punctual_routes": mongodb_punctual_routes,
"bookings": mongodb_bookings,
},
}
return storage, nil
}
func (s MongoDBStorage) CreateRegularRoutes(routes []*geojson.FeatureCollection) error {
log.Debug().Msg("Storage - CreateRegularRoutes")
documents := []any{}
for _, fc := range routes {
if fc != nil {
fc.ExtraMembers["_id"] = fc.ExtraMembers.MustString("id")
delete(fc.ExtraMembers, "id")
documents = append(documents, fc)
}
}
collection := s.Client.Database(s.DbName).Collection(s.Collections["regular_routes"])
if _, err := collection.InsertMany(context.TODO(), documents); err != nil {
return err
}
return nil
}
func (s MongoDBStorage) GetUserRegularRoutes(userid string) ([]*geojson.FeatureCollection, error) {
findOptions := options.Find()
collection := s.Client.Database(s.DbName).Collection(s.Collections["regular_routes"])
cur, err := collection.Find(context.TODO(), bson.M{"properties.user.id": userid}, findOptions)
if err != nil {
return nil, err
}
results := []*geojson.FeatureCollection{}
for cur.Next(context.TODO()) {
var elem bson.M
if err := cur.Decode(&elem); err != nil {
return nil, err
}
bsonBytes, _ := bson.Marshal(elem)
fc := geojson.NewFeatureCollection()
err := fc.UnmarshalBSON(bsonBytes)
if err != nil {
return nil, err
}
fc.ExtraMembers["id"] = fc.ExtraMembers.MustString("_id")
delete(fc.ExtraMembers, "_id")
for k, v := range fc.ExtraMembers {
if val, ok := v.(primitive.D); ok {
em := map[string]any{}
jsonbytes, _ := bson.MarshalExtJSON(val, true, true)
json.Unmarshal(jsonbytes, &em)
fc.ExtraMembers[k] = em
}
}
results = append(results, fc)
}
return results, nil
}
// func (s MongoDBStorage) CreatePassengerRegularTrips(trips []*geojson.FeatureCollection) error {
// log.Debug().Msg("Storage - CreatePassengerRegularTrips")
// documents := []any{}
// for _, fc := range trips {
// if fc != nil {
// fc.ExtraMembers["_id"] = fc.ExtraMembers.MustString("id")
// delete(fc.ExtraMembers, "id")
// documents = append(documents, fc)
// }
// }
// collection := s.Client.Database(s.DbName).Collection(s.Collections["passenger_regular_trips"])
// if _, err := collection.InsertMany(context.TODO(), documents); err != nil {
// return err
// }
// return nil
// }

27
storage/storage.go Normal file
View File

@@ -0,0 +1,27 @@
package storage
import (
"fmt"
"github.com/paulmach/orb/geojson"
"github.com/spf13/viper"
)
type Storage interface {
CreateRegularRoutes([]*geojson.FeatureCollection) error
GetUserRegularRoutes(userid string) ([]*geojson.FeatureCollection, error)
}
func NewStorage(cfg *viper.Viper) (Storage, error) {
var (
storage_type = cfg.GetString("storage.db.type")
)
switch storage_type {
case "mongodb":
s, err := NewMongoDBStorage(cfg)
return s, err
default:
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
}
}

37
storage/trips.go Normal file
View File

@@ -0,0 +1,37 @@
package storage
import (
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
)
type RegularTrip struct {
Id string `bson:"_id"`
GroupId string `bson:"group_id"` // Identifier to group trips together (for example "outward and return", or "driver and passenger" trips) : trips created in a same "CreateTrips" function will have the same group_id
User User `bson:"user"`
Departure *geojson.Feature `bson:"departure"` // GeoJSON feature of the departure (geometry must be of type "Point")
Arrival *geojson.Feature `bson:"arrival"` // GeoJSON feature of the destination (geometry must be of type "Point")
EncodedPolyline string `bson:"encoded_polyline"` // Polyline encoded (Google Polyline Algorithm) string (should be 5 digits representation)
DecodedPolyline orb.LineString `bson:"decoded_polyline"` // geometry of the linestring/polyline describing the trip route
Schedules []Schedule `bson:"schedules"`
Properties map[string]any `bson:"properties"` // Properties map for flexible parameters to be set by the application implementing this service
Removed bool // Trip was removed, do not consider it
}
type Schedule struct {
Day string `bson:"day"`
TimeOfDay string `bson:"time_of_day"`
}
type User struct {
Id string `bson:"_id"`
Alias string `bson:"alias"`
FirsName *string `bson:"first_name,omitempty"`
LastName *string `bson:"last_name,omitempty"`
Grade *int64 `bson:"grade,omitempty"`
Picture *string `bson:"picture,omitempty"`
Gender *string `bson:"gender,omitempty"`
VerifiedIdentity *bool `bson:"verified_identity,omitempty"`
}