initial commit
This commit is contained in:
152
storage/memory.go
Normal file
152
storage/memory.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/solidarity-transport/types"
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type MemoryStorage struct {
|
||||
DriverRegularAvailabilities map[string]*types.DriverRegularAvailability
|
||||
DriverJourneys map[string]*types.DriverJourney
|
||||
Bookings map[string]*types.Booking
|
||||
}
|
||||
|
||||
func NewMemoryStorage(cfg *viper.Viper) (MemoryStorage, error) {
|
||||
return MemoryStorage{
|
||||
DriverRegularAvailabilities: map[string]*types.DriverRegularAvailability{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) CreateDriverRegularAvailability(availability types.DriverRegularAvailability) error {
|
||||
if availability.ID == "" {
|
||||
availability.ID = uuid.NewString()
|
||||
}
|
||||
|
||||
if s.DriverRegularAvailabilities[availability.ID] != nil {
|
||||
return errors.New("availability already exists")
|
||||
}
|
||||
|
||||
s.DriverRegularAvailabilities[availability.ID] = &availability
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) CreateDriverRegularAvailabilities(availabilities []*types.DriverRegularAvailability) error {
|
||||
for _, availability := range availabilities {
|
||||
if availability != nil {
|
||||
if err := s.CreateDriverRegularAvailability(*availability); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) DeleteDriverRegularAvailability(driverid string, availabilityid string) error {
|
||||
if s.DriverRegularAvailabilities[availabilityid] == nil {
|
||||
return errors.New("availability doesn't exist")
|
||||
}
|
||||
if s.DriverRegularAvailabilities[availabilityid].DriverId != driverid {
|
||||
return errors.New("unallowed to delete this availability : driver mismatch")
|
||||
}
|
||||
s.DriverRegularAvailabilities[availabilityid] = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) GetDriverRegularAvailability(driverid string, availabilityid string) (*types.DriverRegularAvailability, error) {
|
||||
if s.DriverRegularAvailabilities[availabilityid] == nil {
|
||||
return nil, errors.New("availability doesn't exist")
|
||||
}
|
||||
|
||||
if s.DriverRegularAvailabilities[availabilityid].DriverId != driverid {
|
||||
return nil, errors.New("unallowed to get this availability : driver mismatch")
|
||||
}
|
||||
|
||||
return s.DriverRegularAvailabilities[availabilityid], nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) GetDriverRegularAvailabilities(driverid string) ([]*types.DriverRegularAvailability, error) {
|
||||
result := []*types.DriverRegularAvailability{}
|
||||
|
||||
for _, a := range s.DriverRegularAvailabilities {
|
||||
if a.DriverId == driverid {
|
||||
result = append(result, a)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) GetRegularAvailabilities(day int, timeInDay string) ([]*types.DriverRegularAvailability, error) {
|
||||
results := []*types.DriverRegularAvailability{}
|
||||
|
||||
for _, a := range s.DriverRegularAvailabilities {
|
||||
if a.Day == day && strings.Compare(a.StartTime, timeInDay) <= 0 && strings.Compare(a.EndTime, timeInDay) >= 0 {
|
||||
results = append(results, a)
|
||||
}
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) PushDriverJourneys(driverjourneys []*types.DriverJourney) error {
|
||||
for _, j := range driverjourneys {
|
||||
s.DriverJourneys[j.Id] = j
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) GetDriverJourney(id string) (res *types.DriverJourney, err error) {
|
||||
res = s.DriverJourneys[id]
|
||||
|
||||
if res == nil {
|
||||
err = errors.New("not found")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s MemoryStorage) CreateBooking(booking types.Booking) error {
|
||||
if booking.Id == "" {
|
||||
booking.Id = uuid.NewString()
|
||||
}
|
||||
|
||||
if s.Bookings[booking.Id] != nil {
|
||||
return errors.New("booking already exists")
|
||||
}
|
||||
|
||||
s.Bookings[booking.Id] = &booking
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) GetAllBookings() ([]*types.Booking, error) {
|
||||
res := []*types.Booking{}
|
||||
|
||||
for _, b := range s.Bookings {
|
||||
res = append(res, b)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s MemoryStorage) GetBooking(id string) (res *types.Booking, err error) {
|
||||
res = s.Bookings[id]
|
||||
|
||||
if res == nil {
|
||||
err = errors.New("not found")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s MemoryStorage) UpdateBookingStatus(bookingid string, newStatus string) error {
|
||||
b := s.Bookings[bookingid]
|
||||
|
||||
if b == nil {
|
||||
return errors.New("booking doesn't exist")
|
||||
}
|
||||
|
||||
b.Status = newStatus
|
||||
|
||||
return nil
|
||||
}
|
||||
214
storage/mongodb.go
Normal file
214
storage/mongodb.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/solidarity-transport/types"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
type MongoDBCollections struct {
|
||||
DriversRegularAvailabilities *mongo.Collection
|
||||
Bookings *mongo.Collection
|
||||
DriverJourneys *mongo.Collection
|
||||
}
|
||||
|
||||
type MongoDBStorage struct {
|
||||
Client *mongo.Client
|
||||
DbName string
|
||||
Collections MongoDBCollections
|
||||
}
|
||||
|
||||
func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
|
||||
var (
|
||||
mongodb_uri = cfg.GetString("storage.db.mongodb.uri")
|
||||
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_drivers_regular_availabilities = cfg.GetString("storage.db.mongodb.collections.drivers_regular_availabilities")
|
||||
mongodb_bookings = cfg.GetString("storage.db.mongodb.collections.bookings")
|
||||
mongodb_driver_journeys = cfg.GetString("storage.db.mongodb.collections.driver_journeys")
|
||||
)
|
||||
|
||||
if mongodb_uri == "" {
|
||||
mongodb_uri = fmt.Sprintf("mongodb://%s:%s/%s", mongodb_host, mongodb_port, mongodb_dbname)
|
||||
}
|
||||
|
||||
client, err := mongo.Connect(options.Client().ApplyURI(mongodb_uri))
|
||||
if err != nil {
|
||||
return MongoDBStorage{}, err
|
||||
}
|
||||
|
||||
log.Info().Str("regular availabilities", mongodb_drivers_regular_availabilities).Str("bookings", mongodb_bookings).Msg("mongodb collections")
|
||||
|
||||
return MongoDBStorage{
|
||||
Client: client,
|
||||
DbName: mongodb_dbname,
|
||||
Collections: MongoDBCollections{
|
||||
DriversRegularAvailabilities: client.Database(mongodb_dbname).Collection(mongodb_drivers_regular_availabilities),
|
||||
Bookings: client.Database(mongodb_dbname).Collection(mongodb_bookings),
|
||||
DriverJourneys: client.Database(mongodb_dbname).Collection(mongodb_driver_journeys),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) CreateDriverRegularAvailability(availability types.DriverRegularAvailability) error {
|
||||
if availability.ID == "" {
|
||||
availability.ID = uuid.NewString()
|
||||
}
|
||||
|
||||
if _, err := s.Collections.DriversRegularAvailabilities.InsertOne(context.Background(), availability); err != nil {
|
||||
log.Error().Err(err).Msg("CreateDriverAvailability error")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) CreateDriverRegularAvailabilities(availabilities []*types.DriverRegularAvailability) error {
|
||||
for _, availability := range availabilities {
|
||||
if availability.ID == "" {
|
||||
availability.ID = uuid.NewString()
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := s.Collections.DriversRegularAvailabilities.InsertMany(context.Background(), availabilities); err != nil {
|
||||
log.Error().Err(err).Msg("CreateDriverAvailabilities error")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) DeleteDriverRegularAvailability(driverid string, availabilityid string) error {
|
||||
if _, err := s.Collections.DriversRegularAvailabilities.DeleteOne(context.Background(), bson.M{"_id": availabilityid, "driver_id": driverid}); err != nil {
|
||||
log.Error().Err(err).Msg("DeleteDriverRegularAvailability error")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetDriverRegularAvailability(driverid string, availabilityid string) (*types.DriverRegularAvailability, error) {
|
||||
var result types.DriverRegularAvailability
|
||||
if err := s.Collections.DriversRegularAvailabilities.FindOne(context.Background(), bson.M{"_id": availabilityid, "driver_id": driverid}).Decode(&result); err != nil {
|
||||
log.Error().Err(err).Msg("GetDriverRegularAvailability - error")
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetDriverRegularAvailabilities(driverid string) ([]*types.DriverRegularAvailability, error) {
|
||||
result := []*types.DriverRegularAvailability{}
|
||||
|
||||
cur, err := s.Collections.DriversRegularAvailabilities.Find(context.Background(), bson.M{"driver_id": driverid})
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("GetDriverAvailabilities DB find - error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = cur.All(context.Background(), &result); err != nil {
|
||||
log.Error().Err(err).Msg("GetDriverAvailabilities cursor decoding - error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetRegularAvailabilities(day int, timeInDay string) ([]*types.DriverRegularAvailability, error) {
|
||||
result := []*types.DriverRegularAvailability{}
|
||||
|
||||
cur, err := s.Collections.DriversRegularAvailabilities.Find(context.Background(),
|
||||
bson.M{
|
||||
"day": day,
|
||||
"start_time": bson.M{"$lt": timeInDay},
|
||||
"end_time": bson.M{"$gt": timeInDay},
|
||||
})
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("GetDriverAvailabilities DB find - error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = cur.All(context.Background(), &result); err != nil {
|
||||
log.Error().Err(err).Msg("GetDriverAvailabilities cursor decoding - error")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) PushDriverJourneys(driverjourneys []*types.DriverJourney) error {
|
||||
for _, journey := range driverjourneys {
|
||||
if journey.Id == "" {
|
||||
journey.Id = uuid.NewString()
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := s.Collections.DriverJourneys.InsertMany(context.Background(), driverjourneys); err != nil {
|
||||
log.Error().Err(err).Msg("PushDriverJourneys error")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetDriverJourney(id string) (*types.DriverJourney, error) {
|
||||
var res types.DriverJourney
|
||||
err := s.Collections.DriverJourneys.FindOne(context.Background(), bson.M{"_id": id}).Decode(&res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) CreateBooking(booking types.Booking) error {
|
||||
if booking.Id == "" {
|
||||
booking.Id = uuid.NewString()
|
||||
}
|
||||
|
||||
if _, err := s.Collections.Bookings.InsertOne(context.Background(), booking); err != nil {
|
||||
log.Error().Err(err).Msg("CreateBooking error")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetAllBookings() ([]*types.Booking, error) {
|
||||
res := []*types.Booking{}
|
||||
|
||||
cur, err := s.Collections.Bookings.Find(context.Background(), bson.M{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = cur.All(context.Background(), &res)
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetBooking(id string) (*types.Booking, error) {
|
||||
var res types.Booking
|
||||
err := s.Collections.Bookings.FindOne(context.Background(), bson.M{"_id": id}).Decode(&res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) UpdateBookingStatus(bookingid string, newStatus string) error {
|
||||
// filter := bson.M{"_id": bookingid}
|
||||
replacement := bson.M{"$set": bson.M{"status": newStatus}}
|
||||
|
||||
if _, err := s.Collections.Bookings.UpdateByID(context.Background(), bookingid, replacement); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
40
storage/storage.go
Normal file
40
storage/storage.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/solidarity-transport/types"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
CreateDriverRegularAvailability(types.DriverRegularAvailability) error
|
||||
CreateDriverRegularAvailabilities([]*types.DriverRegularAvailability) error
|
||||
DeleteDriverRegularAvailability(driverid string, availabilityid string) error
|
||||
GetDriverRegularAvailability(driverid string, availabilityid string) (*types.DriverRegularAvailability, error)
|
||||
GetDriverRegularAvailabilities(driverid string) ([]*types.DriverRegularAvailability, error)
|
||||
|
||||
GetRegularAvailabilities(day int, timeInDay string) ([]*types.DriverRegularAvailability, error)
|
||||
|
||||
PushDriverJourneys([]*types.DriverJourney) error
|
||||
GetDriverJourney(id string) (*types.DriverJourney, error)
|
||||
|
||||
CreateBooking(types.Booking) error
|
||||
GetAllBookings() ([]*types.Booking, error)
|
||||
GetBooking(id string) (*types.Booking, error)
|
||||
UpdateBookingStatus(bookingid string, newStatus string) error
|
||||
}
|
||||
|
||||
func NewStorage(cfg *viper.Viper) (Storage, error) {
|
||||
storage_type := cfg.GetString("storage.db.type")
|
||||
|
||||
switch storage_type {
|
||||
case "mongodb":
|
||||
return NewMongoDBStorage(cfg)
|
||||
case "memory":
|
||||
return NewMemoryStorage(cfg)
|
||||
default:
|
||||
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user