package grpcserver import ( "context" "errors" "github.com/golang/protobuf/ptypes/timestamp" "github.com/paulmach/orb" "github.com/paulmach/orb/geojson" "solidarity-service/internal" "solidarity-service/servers/grpc/proto" ) func (s *SolidarityServiceServerImpl) SetDriverRegularAvailabilities(ctx context.Context, req *proto.DriverRegularAvailabilities) (resp *proto.DriverAvailabilitiesResponse, err error) { if req.DriverRequest.Driver.Id == "" || req.DriverRequest.Driver.Alias == "" || req.DriverAvailabilities == nil || req.DriverRequest.DriverAddress == nil || req.DriverRequest.DriverRadius == 0 { return &proto.DriverAvailabilitiesResponse{ Success: false, }, errors.New("missing required fields") } driver := internal.Driver{ Driver_departure_address: &geojson.Feature{ Type: "Feature", Geometry: orb.Geometry(orb.Point{req.DriverRequest.DriverAddress.Lat, req.DriverRequest.DriverAddress.Long}), Properties: geojson.Properties{ "name": req.DriverRequest.DriverAddress.Address, }, }, Radius: req.DriverRequest.DriverRadius, Driver: internal.User{ ID: req.DriverRequest.Driver.Id, Alias: req.DriverRequest.Driver.Alias, }, AvailabilitiesType: internal.Regular, } if req.DriverRequest.Driver.FirstName != nil { driver.Driver.FirstName = *req.DriverRequest.Driver.FirstName } if req.DriverRequest.Driver.LastName != nil { driver.Driver.LastName = *req.DriverRequest.Driver.LastName } if req.DriverRequest.Driver.Grade != nil { driver.Driver.Grade = *req.DriverRequest.Driver.Grade } if req.DriverRequest.Driver.Picture != nil { driver.Driver.Picture = *req.DriverRequest.Driver.Picture } if req.DriverRequest.Driver.Gender != nil { driver.Driver.Gender = *req.DriverRequest.Driver.Gender } if req.DriverRequest.Driver.VerifiedIdentity != nil { driver.Driver.VerifiedIdentity = *req.DriverRequest.Driver.VerifiedIdentity } if req.DriverRequest.Car != nil { driver.Car = internal.Car{ Model: *req.DriverRequest.Car.Model, Brand: *req.DriverRequest.Car.Brand, } } if req.DriverRequest.Preferences != nil { driver.Preferences = internal.Preferences{ Smoking: *req.DriverRequest.Preferences.Smoking, Animals: *req.DriverRequest.Preferences.Animals, Music: *req.DriverRequest.Preferences.Music, Is_talker: *req.DriverRequest.Preferences.IsTalker, Luggage_size: *req.DriverRequest.Preferences.LuggageSize, } } for _, v := range req.DriverAvailabilities { driver.RegularAvailabilities = append(driver.RegularAvailabilities, internal.RegularAvailabilities{ DayOfWeek: v.DayOfWeek.String(), StartTime: v.StartTime, EndTime: v.EndTime, }) } err = s.Handler.SetDriverAvailabilities(context.Background(), driver) if err != nil { return &proto.DriverAvailabilitiesResponse{ Success: false, }, err } return &proto.DriverAvailabilitiesResponse{ Success: true, }, nil } func (s *SolidarityServiceServerImpl) SetDriverPunctualAvailabilities(ctx context.Context, req *proto.DriverPunctualAvailabilities) (resp *proto.DriverAvailabilitiesResponse, err error) { if req.DriverRequest.Driver.Id == "" || req.DriverRequest.Driver.Alias == "" || req.DriverAvailabilities == nil || req.DriverRequest.DriverAddress == nil || req.DriverRequest.DriverRadius == 0 { return &proto.DriverAvailabilitiesResponse{ Success: false, }, errors.New("missing required fields") } driver := internal.Driver{ Driver_departure_address: &geojson.Feature{ Type: "Feature", Geometry: orb.Geometry(orb.Point{req.DriverRequest.DriverAddress.Lat, req.DriverRequest.DriverAddress.Long}), Properties: geojson.Properties{ "name": req.DriverRequest.DriverAddress.Address, }, }, Radius: req.DriverRequest.DriverRadius, Driver: internal.User{ ID: req.DriverRequest.Driver.Id, Alias: req.DriverRequest.Driver.Alias, }, AvailabilitiesType: internal.Punctual, } if req.DriverRequest.Driver.FirstName != nil { driver.Driver.FirstName = *req.DriverRequest.Driver.FirstName } if req.DriverRequest.Driver.LastName != nil { driver.Driver.LastName = *req.DriverRequest.Driver.LastName } if req.DriverRequest.Driver.Grade != nil { driver.Driver.Grade = *req.DriverRequest.Driver.Grade } if req.DriverRequest.Driver.Picture != nil { driver.Driver.Picture = *req.DriverRequest.Driver.Picture } if req.DriverRequest.Driver.Gender != nil { driver.Driver.Gender = *req.DriverRequest.Driver.Gender } if req.DriverRequest.Driver.VerifiedIdentity != nil { driver.Driver.VerifiedIdentity = *req.DriverRequest.Driver.VerifiedIdentity } if req.DriverRequest.Car != nil { driver.Car = internal.Car{ Model: *req.DriverRequest.Car.Model, Brand: *req.DriverRequest.Car.Brand, } } if req.DriverRequest.Preferences != nil { driver.Preferences = internal.Preferences{ Smoking: *req.DriverRequest.Preferences.Smoking, Animals: *req.DriverRequest.Preferences.Animals, Music: *req.DriverRequest.Preferences.Music, Is_talker: *req.DriverRequest.Preferences.IsTalker, Luggage_size: *req.DriverRequest.Preferences.LuggageSize, } } for _, v := range req.DriverAvailabilities { driver.PunctualAvailabilities = append(driver.PunctualAvailabilities, internal.PunctualAvailabilities{ Date: v.Date.Seconds, StartTime: v.StartTime, EndTime: v.EndTime, }) } err = s.Handler.SetDriverAvailabilities(context.Background(), driver) if err != nil { return &proto.DriverAvailabilitiesResponse{ Success: false, }, err } return &proto.DriverAvailabilitiesResponse{ Success: true, }, nil } func (s *SolidarityServiceServerImpl) CreateBooking(ctx context.Context, req *proto.CreateBookingRequest) (resp *proto.CreateBookingResponse, err error) { if req.Booking.DriverId == "" || req.Booking.PassengerId == "" || req.Booking.Id == "" || req.Booking.Status.String() == "" { return nil, errors.New("missing required fields") } bookingRequest := internal.BookingRequest{ ID: req.Booking.Id, Passenger_id: req.Booking.PassengerId, Driver_id: req.Booking.DriverId, Status: internal.BookingStatus(req.Booking.Status), } passenger, driver, err := s.Handler.CreateBooking(context.Background(), bookingRequest) if err != nil { return nil, err } duration, err := s.Handler.CalculateDurationBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address) if err != nil { duration = 0 } distance := s.Handler.CalculateDistanceBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address) priceType := proto.PriceType_FREE resp.Booking = &proto.Booking{ Id: bookingRequest.ID, Driver: &proto.User{ Id: driver.Driver.ID, Alias: driver.Driver.Alias, FirstName: &driver.Driver.FirstName, LastName: &driver.Driver.LastName, Grade: &driver.Driver.Grade, Picture: &driver.Driver.Picture, Gender: &driver.Driver.Gender, VerifiedIdentity: &driver.Driver.VerifiedIdentity, }, Passenger: &proto.User{ Id: passenger.Passenger.ID, Alias: passenger.Passenger.Alias, FirstName: &passenger.Passenger.FirstName, LastName: &passenger.Passenger.LastName, Grade: &passenger.Passenger.Grade, Picture: &passenger.Passenger.Picture, Gender: &passenger.Passenger.Gender, VerifiedIdentity: &passenger.Passenger.VerifiedIdentity, }, PassengerPickupDate: ×tamp.Timestamp{ Seconds: passenger.Passenger_pickup_date, }, PassengerDepartureRoute: &proto.Feature{ Lat: passenger.Passenger_departure_address.Point().Lat(), Long: passenger.Passenger_departure_address.Point().Lon(), Address: passenger.Passenger_departure_address.Properties.MustString("name"), }, PassengerDestinationRoute: &proto.Feature{ Lat: passenger.Passenger_destination_address.Point().Lat(), Long: passenger.Passenger_destination_address.Point().Lon(), Address: passenger.Passenger_destination_address.Properties.MustString("name"), }, Status: ConvertInternalToProtoBookingStatus(bookingRequest.Status), Duration: &duration, Distance: &distance, Price: &proto.Price{ Type: &priceType, }, Car: &proto.Car{ Model: &driver.Car.Model, Brand: &driver.Car.Brand, }, } return resp, nil } func (s *SolidarityServiceServerImpl) UpdateBooking(ctx context.Context, req *proto.UpdateBookingRequest) (resp *proto.UpdateBookingResponse, err error) { if req.BookingId == "" || req.Status.String() == "" { return &proto.UpdateBookingResponse{ Success: false, }, errors.New("missing required fields") } bookingStatus := internal.BookingStatus(req.Status.String()) err = s.Handler.UpdateBooking(context.Background(), req.BookingId, bookingStatus) if err != nil { return &proto.UpdateBookingResponse{ Success: false, }, err } return &proto.UpdateBookingResponse{ Success: true, }, nil } func (s *SolidarityServiceServerImpl) GetBooking(ctx context.Context, req *proto.GetBookingRequest) (resp *proto.GetBookingResponse, err error) { if req.BookingId == "" { return nil, errors.New("empty booking ID") } booking, passenger, driver, err := s.Handler.GetBooking(context.Background(), req.BookingId) if err != nil { return nil, err } duration, err := s.Handler.CalculateDurationBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address) if err != nil { duration = 0 } distance := s.Handler.CalculateDistanceBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address) priceType := proto.PriceType_FREE resp.Booking = &proto.Booking{ Id: booking.ID, Driver: &proto.User{ Id: driver.Driver.ID, Alias: driver.Driver.Alias, FirstName: &driver.Driver.FirstName, LastName: &driver.Driver.LastName, Grade: &driver.Driver.Grade, Picture: &driver.Driver.Picture, Gender: &driver.Driver.Gender, VerifiedIdentity: &driver.Driver.VerifiedIdentity, }, Passenger: &proto.User{ Id: passenger.Passenger.ID, Alias: passenger.Passenger.Alias, FirstName: &passenger.Passenger.FirstName, LastName: &passenger.Passenger.LastName, Grade: &passenger.Passenger.Grade, Picture: &passenger.Passenger.Picture, Gender: &passenger.Passenger.Gender, VerifiedIdentity: &passenger.Passenger.VerifiedIdentity, }, PassengerPickupDate: ×tamp.Timestamp{ Seconds: passenger.Passenger_pickup_date, }, PassengerDepartureRoute: &proto.Feature{ Lat: passenger.Passenger_departure_address.Point().Lat(), Long: passenger.Passenger_departure_address.Point().Lon(), Address: passenger.Passenger_departure_address.Properties.MustString("name"), }, PassengerDestinationRoute: &proto.Feature{ Lat: passenger.Passenger_destination_address.Point().Lat(), Long: passenger.Passenger_destination_address.Point().Lon(), Address: passenger.Passenger_destination_address.Properties.MustString("name"), }, Status: ConvertInternalToProtoBookingStatus(booking.Status), Duration: &duration, Distance: &distance, Price: &proto.Price{ Type: &priceType, }, Car: &proto.Car{ Model: &driver.Car.Model, Brand: &driver.Car.Brand, }, } return resp, nil } func (s *SolidarityServiceServerImpl) GetBookingsByStatus(ctx context.Context, req *proto.GetBookingsByStatusRequest) (resp *proto.GetBookingsByStatusResponse, err error) { if req.Type.String() == "" || req.Status.String() == "" || req.UserId == "" { return nil, errors.New("missing required fields") } bookings, err := s.Handler.GetBookingsByStatus(context.Background(), req.Status.String(), req.Type.String(), req.UserId) if err != nil { return nil, err } responses := []*proto.Booking{} for _, v := range bookings { passenger, err := s.Handler.GetPassenger(context.Background(), v.Passenger.ID) if err != nil { return nil, err } driver, err := s.Handler.GetDriver(context.Background(), v.Driver.ID) if err != nil { return nil, err } duration, err := s.Handler.CalculateDurationBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address) if err != nil { duration = 0 } priceType := proto.PriceType_FREE distance := s.Handler.CalculateDistanceBetweenFeatures(passenger.Passenger_departure_address, passenger.Passenger_destination_address) responses = append(responses, &proto.Booking{ Id: v.ID, Status: ConvertInternalToProtoBookingStatus(v.Status), Driver: &proto.User{ Id: v.Driver.ID, Alias: driver.Driver.Alias, FirstName: &driver.Driver.FirstName, LastName: &driver.Driver.LastName, Grade: &driver.Driver.Grade, Picture: &driver.Driver.Picture, Gender: &driver.Driver.Gender, VerifiedIdentity: &driver.Driver.VerifiedIdentity, }, Passenger: &proto.User{ Id: v.Passenger.ID, Alias: passenger.Passenger.Alias, FirstName: &passenger.Passenger.FirstName, LastName: &passenger.Passenger.LastName, Grade: &passenger.Passenger.Grade, Picture: &passenger.Passenger.Picture, Gender: &passenger.Passenger.Gender, VerifiedIdentity: &passenger.Passenger.VerifiedIdentity, }, PassengerPickupDate: ×tamp.Timestamp{ Seconds: passenger.Passenger_pickup_date, }, PassengerDepartureRoute: &proto.Feature{ Lat: passenger.Passenger_departure_address.Point().Lat(), Long: passenger.Passenger_departure_address.Point().Lon(), Address: passenger.Passenger_departure_address.Properties.MustString("name"), }, PassengerDestinationRoute: &proto.Feature{ Lat: passenger.Passenger_destination_address.Point().Lat(), Long: passenger.Passenger_destination_address.Point().Lon(), Address: passenger.Passenger_destination_address.Properties.MustString("name"), }, Duration: &duration, Distance: &distance, Car: &proto.Car{ Model: &driver.Car.Model, Brand: &driver.Car.Brand, }, Price: &proto.Price{ Type: &priceType, }, }) } resp.Booking = responses return resp, nil } func (s *SolidarityServiceServerImpl) DriverJourneys(ctx context.Context, req *proto.DriverJourneysRequest) (resp *proto.DriverJourneysResponse, err error) { if req.DepartureDate.Seconds == 0 || req.Departure == nil { return nil, errors.New("missing required fields") } drivers, err := s.Handler.GetDriverJourneys(context.Background(), &geojson.Feature{ Type: "Feature", Geometry: orb.Geometry(orb.Point{req.Departure.Lat, req.Departure.Long}), Properties: geojson.Properties{ "name": req.Departure.Address, }}, req.DepartureDate.Seconds) if err != nil { return nil, err } response := []*proto.DriverJourney{} for _, v := range drivers { temp := &proto.DriverJourney{} if v.AvailabilitiesType == internal.Regular && v.RegularAvailabilities != nil { regularAvailabilities := make([]*proto.RegularAvailabilitySlot, 0) // Initialize an empty slice for _, v := range v.RegularAvailabilities { day := ConvertToProtoDayOfTheWeek(v.DayOfWeek) regularAvailabilities = append(regularAvailabilities, &proto.RegularAvailabilitySlot{ DayOfWeek: day, StartTime: v.StartTime, EndTime: v.EndTime, }) } convertedAvailability := &proto.RepeatedRegularAvailabilitySlot{ RegularAvailabilities: regularAvailabilities, } temp.Availabilities = &proto.DriverJourney_RepeatedRegularAvailabilities{ RepeatedRegularAvailabilities: convertedAvailability, } } else if v.AvailabilitiesType == internal.Punctual && v.PunctualAvailabilities != nil { punctualAvailabilities := make([]*proto.PunctualAvailabilitySlot, 0) // Initialize an empty slice for _, v := range v.PunctualAvailabilities { punctualAvailabilities = append(punctualAvailabilities, &proto.PunctualAvailabilitySlot{ Date: ×tamp.Timestamp{ Seconds: v.Date, }, StartTime: v.StartTime, EndTime: v.EndTime, }) } convertedAvailability := &proto.RepeatedPunctualAvailabilitySlot{ PunctualAvailabilities: punctualAvailabilities, } temp.Availabilities = &proto.DriverJourney_RepeatedPunctualAvailabilities{ RepeatedPunctualAvailabilities: convertedAvailability, } } priceType := proto.PriceType_FREE tamp := &proto.DriverJourney{ User: &proto.User{ Id: v.Driver.ID, Alias: v.Driver.Alias, FirstName: &v.Driver.FirstName, LastName: &v.Driver.LastName, Grade: &v.Driver.Grade, Picture: &v.Driver.Picture, Gender: &v.Driver.Gender, VerifiedIdentity: &v.Driver.VerifiedIdentity, }, Car: &proto.Car{ Model: &v.Car.Model, Brand: &v.Car.Brand, }, DriverDeparture_Date: ×tamp.Timestamp{ Seconds: req.DepartureDate.Seconds, }, Price: &proto.Price{ Type: &priceType, }, DriverDeparture_Address: v.Driver_departure_address.Properties.MustString("name"), } temp.DriverDeparture_Address = tamp.DriverDeparture_Address temp.Car = tamp.Car temp.User = tamp.User temp.DriverDeparture_Date = tamp.DriverDeparture_Date temp.Price = tamp.Price response = append(response, temp) } resp.DriverJourneys = response return resp, nil } func (s *SolidarityServiceServerImpl) SetPassengerTrip(ctx context.Context, req *proto.PassengerTripRequest) (resp *proto.PassengerTripResponse, err error) { if req.Passenger.Id == "" || req.Passenger.Alias == "" || req.PassengerPickupDate.Seconds == 0 || req.PassengerDestinationAddress == nil || req.PassengerDestinationAddress == nil { return &proto.PassengerTripResponse{ Success: false, }, errors.New("missing required fields") } passenger := internal.Passenger{ Passenger_departure_address: &geojson.Feature{ Type: "Feature", Geometry: orb.Geometry(orb.Point{req.PassengerDepartureAddress.Lat, req.PassengerDepartureAddress.Long}), Properties: geojson.Properties{ "name": req.PassengerDepartureAddress.Address, }, }, Passenger_destination_address: &geojson.Feature{ Type: "Feature", Geometry: orb.Geometry(orb.Point{req.PassengerDestinationAddress.Lat, req.PassengerDestinationAddress.Long}), Properties: geojson.Properties{ "name": req.PassengerDestinationAddress.Address, }, }, Passenger_pickup_date: req.PassengerPickupDate.Seconds, Passenger: internal.User{ ID: req.Passenger.Id, Alias: req.Passenger.Alias, }, } if req.Passenger.FirstName != nil { passenger.Passenger.FirstName = *req.Passenger.FirstName } if req.Passenger.LastName != nil { passenger.Passenger.LastName = *req.Passenger.LastName } if req.Passenger.Grade != nil { passenger.Passenger.Grade = *req.Passenger.Grade } if req.Passenger.Picture != nil { passenger.Passenger.Picture = *req.Passenger.Picture } if req.Passenger.Gender != nil { passenger.Passenger.Gender = *req.Passenger.Gender } if req.Passenger.VerifiedIdentity != nil { passenger.Passenger.VerifiedIdentity = *req.Passenger.VerifiedIdentity } if req.Preferences != nil { passenger.Preferences = internal.Preferences{ Smoking: *req.Preferences.Smoking, Animals: *req.Preferences.Animals, Music: *req.Preferences.Music, Is_talker: *req.Preferences.IsTalker, Luggage_size: *req.Preferences.LuggageSize, } } err = s.Handler.SetPassengerTrip(context.Background(), passenger) if err != nil { return &proto.PassengerTripResponse{ Success: false, }, nil } return &proto.PassengerTripResponse{ Success: true, }, nil }