2023-04-03 18:35:12 +00:00
|
|
|
package grpcserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-platform/carpool-service/servers/grpc/proto"
|
|
|
|
"github.com/paulmach/orb/geojson"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *CarpoolServiceServerImpl) CreateRegularRoutes(ctx context.Context, req *proto.CreateRegularRoutesRequest) (*proto.CreateRegularRoutesResponse, error) {
|
|
|
|
log.Debug().Msg("grpc CarpoolService - CreateTrips")
|
|
|
|
routes := []*geojson.FeatureCollection{}
|
|
|
|
|
|
|
|
for _, r := range req.Routes {
|
|
|
|
route, err := geojson.UnmarshalFeatureCollection([]byte(r.Serialized))
|
|
|
|
if err == nil {
|
|
|
|
routes = append(routes, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) == 0 {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "no route to create")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Handler.CreateRegularRoutes(routes); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not create routes in CreateRegularRoutes : %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.CreateRegularRoutesResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CarpoolServiceServerImpl) DeleteRegularRoutes(context.Context, *proto.DeleteRegularRoutesRequest) (*proto.DeleteRegularRoutesResponse, error) {
|
|
|
|
return nil, status.Errorf(codes.Unimplemented, "method DeleteTrips not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CarpoolServiceServerImpl) GetUserPlanning(ctx context.Context, req *proto.GetUserPlanningRequest) (*proto.GetUserPlanningResponse, error) {
|
|
|
|
log.Debug().Msg("grpc CarpoolService - GetRegularUserRoutes")
|
|
|
|
|
|
|
|
planned_schedules, err := s.Handler.GetUserPlanning(req.UserId, req.MinDepartureDate.AsTime(), req.MaxDepartureDate.AsTime())
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not get user planning : %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
results := map[string]*proto.CarpoolRoutesCollection{}
|
|
|
|
|
|
|
|
for k, scheds := range planned_schedules {
|
|
|
|
|
|
|
|
results[k] = &proto.CarpoolRoutesCollection{
|
|
|
|
Collection: []*proto.CarpoolFeatureCollection{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scheds {
|
|
|
|
s.Route.ExtraMembers["departure_date"] = s.DepartureDate
|
|
|
|
s.Route.ExtraMembers["id"] = s.ID
|
2023-05-07 23:29:59 +00:00
|
|
|
fcraw, _ := s.Route.MarshalJSON()
|
2023-04-03 18:35:12 +00:00
|
|
|
results[k].Collection = append(results[k].Collection, &proto.CarpoolFeatureCollection{
|
|
|
|
Serialized: string(fcraw),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.GetUserPlanningResponse{
|
|
|
|
RoutesByDates: results,
|
|
|
|
}, nil
|
|
|
|
}
|
2023-04-05 18:57:27 +00:00
|
|
|
|
|
|
|
func (s *CarpoolServiceServerImpl) GetPlannedTrip(ctx context.Context, req *proto.GetPlannedTripRequest) (*proto.GetPlannedTripResponse, error) {
|
|
|
|
planned_trip, err := s.Handler.GetPlannedTrip(req.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not retrieve planned trip - %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-05-07 23:29:59 +00:00
|
|
|
planned_trip.Route.ExtraMembers["id"] = planned_trip.ID
|
2023-04-05 18:57:27 +00:00
|
|
|
planned_trip.Route.ExtraMembers["departure_date"] = planned_trip.DepartureDate
|
|
|
|
|
2023-05-07 23:29:59 +00:00
|
|
|
serialized, err := planned_trip.Route.MarshalJSON()
|
2023-04-05 18:57:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("grpc GetPlannedTrip - could not serialize feature collection")
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not serialize feature collection")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.GetPlannedTripResponse{
|
|
|
|
PlannedTrip: &proto.CarpoolFeatureCollection{
|
|
|
|
Serialized: string(serialized),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|