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

@@ -1,64 +0,0 @@
package handler
import (
"errors"
"git.coopgo.io/coopgo-platform/carpool-service/internal"
"git.coopgo.io/coopgo-platform/carpool-service/interoperability/ocss"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
)
func (h *CarpoolServiceHandler) Book(booking ocss.Booking) (*internal.Booking, error) {
futureBooking := internal.Booking{}
futureBooking.Roles = []string{}
if booking.Driver.Operator == "ridygo.fr" {
futureBooking.Roles = append(futureBooking.Roles, "driver")
previous_search_result, err := h.Storage.GetSearchResult(booking.DriverJourneyID)
if err == nil {
futureBooking.DriverJourney = &internal.PlannedJourney{
Route: internal.RegularRoute(*previous_search_result.Route),
DepartureDate: previous_search_result.DepartureDate,
}
}
}
if booking.Passenger.Operator == "ridygo.fr" {
futureBooking.Roles = append(futureBooking.Roles, "passenger")
previous_search_result, err := h.Storage.GetSearchResult(booking.PassengerJourneyID)
if err == nil {
futureBooking.PassengerJourney = &internal.PlannedJourney{
Route: internal.RegularRoute(*previous_search_result.Route),
DepartureDate: previous_search_result.DepartureDate,
}
}
}
if len(futureBooking.Roles) == 0 {
return nil, errors.New("couldn't find the right operator id : \"ridygo.fr\" should be set for driver or passenger")
}
if _, err := uuid.Parse(booking.ID); err != nil {
return nil, errors.New("bookingid is not a valid uuid")
}
futureBooking.ID = booking.ID
futureBooking.BookingDefinition = booking
err := h.Storage.CreateBooking(futureBooking)
if err != nil {
log.Error().Err(err).Msg("issue creating booking in database")
return nil, err
}
return &futureBooking, nil
}
func (h *CarpoolServiceHandler) GetBooking(id string) (*internal.Booking, error) {
booking, err := h.Storage.GetBooking(id)
if err != nil {
log.Error().Err(err).Msg("issue retrieving booking in storage")
return nil, errors.New("booking id not found")
}
return booking, nil
}

View File

@@ -1,3 +1,4 @@
// Package handler provides the business logic for the carpool service
package handler
import (
@@ -8,17 +9,20 @@ import (
)
type CarpoolServiceHandler struct {
Config *viper.Viper
Storage storage.Storage
Tiles *tiles.TilesHandler
Routing routing.RoutingService
Config *viper.Viper
Storage storage.Storage
Tiles *tiles.TilesHandler
Routing routing.RoutingService
InternalOperatorID string
}
func NewCarpoolServiceHandler(cfg *viper.Viper, storage storage.Storage, tilesHandler *tiles.TilesHandler, routing routing.RoutingService) (*CarpoolServiceHandler, error) {
operator := cfg.GetString("interoperability.internal_operator_id")
return &CarpoolServiceHandler{
Config: cfg,
Storage: storage,
Tiles: tilesHandler,
Routing: routing,
Config: cfg,
Storage: storage,
Tiles: tilesHandler,
Routing: routing,
InternalOperatorID: operator,
}, nil
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/rs/zerolog/log"
)
// CreateRegularRoutes creates and stores a regular route
func (h *CarpoolServiceHandler) CreateRegularRoutes(routes []*geojson.FeatureCollection) error {
groupid := uuid.NewString()
for _, r := range routes {
@@ -57,6 +58,7 @@ func (h *CarpoolServiceHandler) CreateRegularRoutes(routes []*geojson.FeatureCol
return nil
}
// GetUserPlanning returns the planned routes for a user between 2 dates. It transforms regular routes to multiple indivual planned route schedules
func (h *CarpoolServiceHandler) GetUserPlanning(userid string, minDepartureDate time.Time, maxDepartureDate time.Time) (map[string][]internal.PlannedRouteSchedule, error) {
log.Debug().
Str("user_id", userid).
@@ -82,9 +84,9 @@ func (h *CarpoolServiceHandler) GetUserPlanning(userid string, minDepartureDate
for _, r := range routes {
rr := internal.RegularRoute(*r)
schedules, err := rr.PlannedJourneySchedules(minDepartureDate, maxDepartureDate)
schedules, err := rr.PlannedRouteSchedules(minDepartureDate, maxDepartureDate)
if err != nil {
log.Error().Err(err)
log.Error().Err(err).Msg("PlannedRouteSchedules error")
return nil, err
}
@@ -112,6 +114,7 @@ func (h *CarpoolServiceHandler) GetUserPlanning(userid string, minDepartureDate
return results, nil
}
// GetPlannedTrip returns a planned trip, given an ID. This should be called after getting the user planning from regular routes
func (h *CarpoolServiceHandler) GetPlannedTrip(id string) (*internal.PlannedRouteSchedule, error) {
planned_trip, err := h.Storage.GetRouteSchedule(id)
if err != nil {

View File

@@ -10,7 +10,8 @@ import (
"github.com/rs/zerolog/log"
)
func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.SearchResult, error) {
// GetDriverJourneys searches for matching punctual planned driver journeys.
func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.PlannedRouteSchedule, error) {
log.Debug().
Any("departure", departure).
@@ -51,14 +52,12 @@ func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival o
candidate_routes := tileset.GetTiledRoutes()
journeys := []internal.SearchResult{}
journeys := []internal.PlannedRouteSchedule{}
counted := int64(0)
for _, r := range candidate_routes {
ls := r.Route.Features[2].Geometry.(orb.LineString)
// distanceFromDeparture, indexDeparture := planar.DistanceFromWithIndex(ls, departure)
// distanceFromArrival, indexArrival := planar.DistanceFromWithIndex(ls, arrival)
distanceFromDeparture, indexDeparture := geoutils.DistanceFromLineString(departure, ls)
distanceFromArrival, indexArrival := geoutils.DistanceFromLineString(arrival, ls)
@@ -71,7 +70,7 @@ func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival o
log.Error().Err(err).Msg("error getting route with viapoints")
continue
}
journeys = append(journeys, internal.SearchResult{
journeys = append(journeys, internal.PlannedRouteSchedule{
ID: r.ID,
Route: r.Route,
DepartureDate: r.DepartureDate,
@@ -85,7 +84,7 @@ func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival o
}
if len(journeys) > 0 {
err = h.Storage.StoreSearchResults(journeys)
err = h.Storage.StoreRouteSchedules(journeys)
if err != nil {
log.Error().Err(err).Msg("Error saving search results")
return nil, err
@@ -95,7 +94,8 @@ func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival o
return journeys, nil
}
func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.SearchResult, error) {
// GetPassengerJourneys searches for matching punctual planned passenger journeys.
func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.PlannedRouteSchedule, error) {
log.Debug().
Any("departure", departure).
@@ -151,7 +151,7 @@ func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arriva
candidate_routes := tileset.GetTiledRoutes()
journeys := []internal.SearchResult{}
journeys := []internal.PlannedRouteSchedule{}
counted := int64(0)
for _, r := range candidate_routes {
@@ -167,7 +167,7 @@ func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arriva
log.Error().Err(err).Msg("error getting route with viapoints")
continue
}
journeys = append(journeys, internal.SearchResult{
journeys = append(journeys, internal.PlannedRouteSchedule{
ID: r.ID,
Route: r.Route,
DepartureDate: r.DepartureDate,
@@ -181,7 +181,7 @@ func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arriva
}
if len(journeys) > 0 {
err = h.Storage.StoreSearchResults(journeys)
err = h.Storage.StoreRouteSchedules(journeys)
if err != nil {
log.Error().Err(err).Msg("Error saving search results")
return nil, err