carpool-service/servers/grpc/server/management.go

91 lines
3.0 KiB
Go

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
fcraw, _ := s.Route.MarshalJSON()
results[k].Collection = append(results[k].Collection, &proto.CarpoolFeatureCollection{
Serialized: string(fcraw),
})
}
}
return &proto.GetUserPlanningResponse{
RoutesByDates: results,
}, nil
}
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())
}
planned_trip.Route.ExtraMembers["id"] = planned_trip.ID
planned_trip.Route.ExtraMembers["departure_date"] = planned_trip.DepartureDate
serialized, err := planned_trip.Route.MarshalJSON()
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
}