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

69 lines
2.2 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.FeatureCollection().MarshalJSON()
results[k].Collection = append(results[k].Collection, &proto.CarpoolFeatureCollection{
Serialized: string(fcraw),
})
}
}
return &proto.GetUserPlanningResponse{
RoutesByDates: results,
}, nil
}