initial commit

This commit is contained in:
2025-03-05 00:30:53 +01:00
commit 9da7b99e5d
32 changed files with 4541 additions and 0 deletions

41
handler/availabilities.go Normal file
View File

@@ -0,0 +1,41 @@
package handler
import "git.coopgo.io/coopgo-platform/solidarity-transport/types"
func (h *Handler) AddRegularAvailability(driver string, day int, startTime string, endTime string) error {
availability := types.DriverRegularAvailability{
DriverId: driver,
Day: day,
StartTime: startTime,
EndTime: endTime,
}
if err := h.Storage.CreateDriverRegularAvailability(availability); err != nil {
return err
}
return nil
}
func (h *Handler) AddDriverRegularAvailabilities(availabilities []*types.DriverRegularAvailability) error {
if err := h.Storage.CreateDriverRegularAvailabilities(availabilities); err != nil {
return err
}
return nil
}
func (h *Handler) GetDriverRegularAvailabilities(driver string) ([]*types.DriverRegularAvailability, error) {
availabilities, err := h.Storage.GetDriverRegularAvailabilities(driver)
if err != nil {
return nil, err
}
return availabilities, nil
}
func (h *Handler) DeleteDriverRegularAvailabilities(driver string, availability string) error {
if err := h.Storage.DeleteDriverRegularAvailability(driver, availability); err != nil {
return err
}
return nil
}

73
handler/bookings.go Normal file
View File

@@ -0,0 +1,73 @@
package handler
import (
"errors"
"time"
"git.coopgo.io/coopgo-platform/solidarity-transport/types"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
)
func (h Handler) BookDriverJourney(passengerid string, driverid string, journeyid string) error {
journey, err := h.Storage.GetDriverJourney(journeyid)
if err != nil {
log.Error().Err(err).Msg("could not find driver journey")
return err
}
if journey.DriverId != driverid {
return errors.New("not authorized : journey id driver and driverid mismatch")
}
booking := types.Booking{
Id: uuid.NewString(),
GroupId: uuid.NewString(),
Status: "WAITING_CONFIRMATION",
PassengerId: passengerid,
DriverId: driverid,
Journey: journey,
}
if err := h.Storage.CreateBooking(booking); err != nil {
log.Error().Err(err).Msg("error creating booking")
return err
}
return nil
}
func (h Handler) GetBookings(startdate time.Time, enddate time.Time) ([]*types.Booking, error) {
res := []*types.Booking{}
bookings, err := h.Storage.GetAllBookings()
if err != nil {
log.Error().Err(err).Msg("error retrieving bookings")
return nil, err
}
for _, b := range bookings {
if b.Journey.DriverDepartureDate.After(startdate) && b.Journey.DriverDepartureDate.Before(enddate) {
res = append(res, b)
}
}
return res, nil
}
func (h Handler) GetBooking(id string) (*types.Booking, error) {
booking, err := h.Storage.GetBooking(id)
if err != nil {
log.Error().Err(err).Msg("could not get booking")
return nil, err
}
return booking, nil
}
func (h Handler) UpdateBookingStatus(bookingid string, newStatus string) error {
if err := h.Storage.UpdateBookingStatus(bookingid, newStatus); err != nil {
log.Error().Err(err).Msg("could not update booking")
return err
}
return nil
}

21
handler/handler.go Normal file
View File

@@ -0,0 +1,21 @@
package handler
import (
"git.coopgo.io/coopgo-platform/routing-service"
"git.coopgo.io/coopgo-platform/solidarity-transport/storage"
"github.com/spf13/viper"
)
type Handler struct {
Config *viper.Viper
Storage storage.Storage
Routing routing.RoutingService
}
func NewSolidarityTransportHandler(cfg *viper.Viper, store storage.Storage, routing routing.RoutingService) (*Handler, error) {
return &Handler{
Config: cfg,
Storage: store,
Routing: routing,
}, nil
}

85
handler/journeys.go Normal file
View File

@@ -0,0 +1,85 @@
package handler
import (
"errors"
"time"
"git.coopgo.io/coopgo-platform/solidarity-transport/types"
"github.com/google/uuid"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
)
func (h *Handler) GetDriverJourneys(departure *geojson.Feature, arrival *geojson.Feature, departureDate time.Time) ([]*types.DriverJourney, error) {
day := int(departureDate.Weekday())
timeInDay := departureDate.Format("15:04")
driverJourneys := []*types.DriverJourney{}
// Get Availabilities
availabilities, err := h.Storage.GetRegularAvailabilities(day, timeInDay)
if err != nil {
log.Error().Err(err).Msg("error in GetRegularAvailabilities")
return nil, err
}
for _, a := range availabilities {
log.Debug().Any("availability", a).Msg("Availability found")
if a.Address != nil {
route, err := h.Routing.Route(
[]orb.Point{
a.Address.Point(),
departure.Point(),
arrival.Point(),
a.Address.Point(),
},
)
if err != nil {
log.Error().Err(err).Msg("failedcomputing route request")
continue
}
log.Debug().Any("route", route).Msg("debug route")
driverJourney := &types.DriverJourney{
Id: uuid.NewString(),
DriverId: a.DriverId,
PassengerPickup: departure,
PassengerDrop: arrival,
PassengerDistance: int64(route.Legs[1].Distance),
DriverDeparture: a.Address,
DriverArrival: a.Address,
DriverDistance: int64(route.Distance),
Duration: route.Legs[1].Duration,
JourneyPolyline: route.Polyline,
PassengerPickupDate: departureDate,
DriverDepartureDate: departureDate.Add(-1 * route.Legs[0].Duration),
Price: types.SolidarityTransportPrice{
Currency: "EUR",
Amount: 0,
},
}
driverJourneys = append(driverJourneys, driverJourney)
}
}
if err := h.Storage.PushDriverJourneys(driverJourneys); err != nil {
log.Error().Err(err).Msg("error storing driver journeys in database")
}
return driverJourneys, nil
}
func (h Handler) GetDriverJourney(driverid string, journeyid string) (*types.DriverJourney, error) {
journey, err := h.Storage.GetDriverJourney(journeyid)
if err != nil {
log.Error().Err(err).Msg("error retrieving journey")
return nil, err
}
if driverid != journey.DriverId {
return nil, errors.New("not allowed, driver id mismatch")
}
return journey, nil
}