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
|
||||
}
|
||||
Reference in New Issue
Block a user