carpool-service/handler/management.go

85 lines
2.2 KiB
Go

package handler
import (
"errors"
"time"
"git.coopgo.io/coopgo-platform/routing-service/encoding/polylines"
"github.com/google/uuid"
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
)
func (h *CarpoolServiceHandler) CreateRegularRoutes(routes []*geojson.FeatureCollection) error {
groupid := uuid.NewString()
for _, r := range routes {
r.ExtraMembers["id"] = uuid.NewString()
r.ExtraMembers["routes_group_id"] = groupid
properties, ok := r.ExtraMembers["properties"].(map[string]any)
if !ok {
return errors.New("no properties found in feature collection")
}
polyline, ok := properties["polyline"].(string)
if !ok {
return errors.New("no polyline found in properties from feature collection")
}
lineString := geojson.NewFeature(polylines.Decode(&polyline, 5))
lineString.Properties["encoded_polyline"] = polyline
r.Append(lineString)
}
if err := h.Storage.CreateRegularRoutes(routes); err != nil {
return err
}
return nil
}
func (h *CarpoolServiceHandler) GetUserPlanning(userid string, minDepartureDate time.Time, maxDepartureDate time.Time) (map[string][]PlannedRouteSchedule, error) {
log.Debug().
Str("user_id", userid).
Time("min_departure_date", minDepartureDate).
Time("max_departure_date", maxDepartureDate).
Msg("carpool service handler - GetUserPlanning")
results := map[string][]PlannedRouteSchedule{}
current_date := minDepartureDate
for current_date.Before(maxDepartureDate) {
results[current_date.Format("2006-01-02")] = []PlannedRouteSchedule{}
current_date = current_date.Add(24 * time.Hour)
}
routes, err := h.Storage.GetUserRegularRoutes(userid)
if err != nil {
log.Error().Err(err).Msg("error in storage - GetUserRegularRoutes")
return nil, err
}
for _, r := range routes {
rr := RegularRoute(*r)
schedules, err := rr.PlannedJourneySchedules(minDepartureDate, maxDepartureDate)
if err != nil {
log.Error().Err(err)
return nil, err
}
for _, s := range schedules {
log.Debug().
Str("route id", s.Route.ExtraMembers.MustString("id")).
Any("datetime", s.DepartureDate.Format(time.RFC3339)).
Msg("new schedule")
dateString := s.DepartureDate.Format("2006-01-02")
results[dateString] = append(results[dateString], s)
}
}
return results, nil
}