first commit

This commit is contained in:
2023-10-20 13:41:39 +02:00
commit 6696cd3152
65 changed files with 11374 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,133 @@
syntax = "proto3";
option go_package = "git.coopgo.io/coopgo-platform/solidarity-service/servers/grpc/proto";
import "google/protobuf/timestamp.proto";
message Feature {
double lat = 1;
double long = 2;
string address = 3;
}
message DriverJourney {
User user = 1;
optional Car car = 2;
google.protobuf.Timestamp driver_departure_Date = 3;
optional Price price = 4;
string driver_departure_Address = 5;
oneof availabilities {
RepeatedPunctualAvailabilitySlot repeated_punctual_availabilities = 6;
RepeatedRegularAvailabilitySlot repeated_regular_availabilities = 7;
}
}
message RepeatedPunctualAvailabilitySlot {
repeated PunctualAvailabilitySlot punctual_availabilities = 1;
}
message RepeatedRegularAvailabilitySlot {
repeated RegularAvailabilitySlot regular_availabilities = 1;
}
message DriverRequest{
Feature driver_address = 1;
int32 driver_radius = 2;
User driver = 3;
optional Preferences preferences = 4;
optional Car car = 5;
}
message PunctualAvailabilitySlot {
google.protobuf.Timestamp date = 1;
string startTime = 2;
string endTime = 3;
}
message RegularAvailabilitySlot {
DayOfWeek dayOfWeek = 1;
string startTime = 2;
string endTime = 3;
}
enum DayOfWeek {
MON = 0;
TUE = 1;
WED = 3;
THU = 4;
FRI = 5;
SAT = 6;
SUN = 7;
}
message BookingRequest {
string id = 1;
string passengerId = 2;
string driverId = 3;
BookingStatus status = 4;
}
message Booking {
string id = 1;
User driver = 2;
User passenger = 3;
google.protobuf.Timestamp passengerPickupDate =4;
Feature passenger_departure_route = 5;
Feature passenger_destination_route = 6;
BookingStatus status = 7;
optional int64 duration = 8;
optional int64 distance = 9;
Price price = 10;
optional Car car = 11;
}
message Car {
optional string model = 1;
optional string brand = 2;
}
message Preferences {
optional bool smoking = 1;
optional bool animals = 2;
optional bool music = 3;
optional bool is_talker = 4;
optional int64 luggage_size = 5;
}
enum PriceType {
FREE = 0;
PAYING = 1;
UNKNOWN = 2;
}
message Price {
optional PriceType type = 1;
optional double amount = 2;
optional string currency = 3;
}
message User {
string id = 1;
string alias = 2;
optional string first_name = 3;
optional string last_name = 4;
optional int64 grade = 5;
optional string picture = 6;
optional string gender = 7;
optional bool verified_identity = 8;
}
enum BookingStatus {
INITIATED = 0;
WAITING_DRIVER_CONFIRMATION = 1;
WAITING_PASSENGER_CONFIRMATION = 2;
CONFIRMED = 3;
CANCELLED = 4;
COMPLETED_PENDING_VALIDATION = 5;
VALIDATED = 6;
}
enum UserType{
driver = 0;
passenger = 1;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,91 @@
syntax = "proto3";
option go_package = "git.coopgo.io/coopgo-platform/solidarity-service/servers/grpc/proto";
import "google/protobuf/timestamp.proto";
import "solidarity-api-types.proto";
service SolidarityService {
rpc SetDriverRegularAvailabilities(DriverRegularAvailabilities) returns (DriverAvailabilitiesResponse) {}
rpc SetDriverPunctualAvailabilities(DriverPunctualAvailabilities) returns (DriverAvailabilitiesResponse) {}
rpc CreateBooking(CreateBookingRequest) returns (CreateBookingResponse) {}
rpc UpdateBooking(UpdateBookingRequest) returns (UpdateBookingResponse) {}
rpc GetBooking(GetBookingRequest) returns (GetBookingResponse) {}
rpc GetBookingsByStatus(GetBookingsByStatusRequest) returns (GetBookingsByStatusResponse) {}
rpc DriverJourneys(DriverJourneysRequest) returns (DriverJourneysResponse) {}
rpc SetPassengerTrip(PassengerTripRequest) returns (PassengerTripResponse) {}
}
message PassengerTripRequest{
Feature passenger_departure_address = 1;
Feature passenger_destination_address = 2;
google.protobuf.Timestamp passenger_pickup_date = 3;
User passenger = 4;
optional Preferences preferences = 5;
}
message DriverRegularAvailabilities{
DriverRequest driver_request = 1;
repeated RegularAvailabilitySlot driver_availabilities = 2;
}
message DriverPunctualAvailabilities{
DriverRequest driver_request = 1;
repeated PunctualAvailabilitySlot driver_availabilities = 2;
}
message PassengerTripResponse {
bool success = 1;
optional string message = 2;
}
message DriverAvailabilitiesResponse {
bool success = 1;
optional string message = 2;
}
message CreateBookingRequest {
BookingRequest booking = 1;
}
message CreateBookingResponse {
Booking booking = 1;
}
message UpdateBookingRequest {
string booking_id = 1;
BookingStatus status = 2;
optional string message = 3;
}
message UpdateBookingResponse {
bool success = 1;
optional string message = 2;
}
message GetBookingRequest {
string booking_id = 1;
}
message GetBookingResponse {
Booking booking = 1;
}
message GetBookingsByStatusRequest{
BookingStatus status = 1;
UserType type = 2;
string user_id = 3;
}
message DriverJourneysRequest {
Feature departure = 1;
google.protobuf.Timestamp departure_date = 2;
}
message DriverJourneysResponse {
repeated DriverJourney driver_journeys = 1;
}
message GetBookingsByStatusResponse{
repeated Booking booking = 1;
}

View File

@@ -0,0 +1,368 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc v3.12.4
// source: solidarity-api.proto
package proto
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
SolidarityService_SetDriverRegularAvailabilities_FullMethodName = "/SolidarityService/SetDriverRegularAvailabilities"
SolidarityService_SetDriverPunctualAvailabilities_FullMethodName = "/SolidarityService/SetDriverPunctualAvailabilities"
SolidarityService_CreateBooking_FullMethodName = "/SolidarityService/CreateBooking"
SolidarityService_UpdateBooking_FullMethodName = "/SolidarityService/UpdateBooking"
SolidarityService_GetBooking_FullMethodName = "/SolidarityService/GetBooking"
SolidarityService_GetBookingsByStatus_FullMethodName = "/SolidarityService/GetBookingsByStatus"
SolidarityService_DriverJourneys_FullMethodName = "/SolidarityService/DriverJourneys"
SolidarityService_SetPassengerTrip_FullMethodName = "/SolidarityService/SetPassengerTrip"
)
// SolidarityServiceClient is the client API for SolidarityService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type SolidarityServiceClient interface {
SetDriverRegularAvailabilities(ctx context.Context, in *DriverRegularAvailabilities, opts ...grpc.CallOption) (*DriverAvailabilitiesResponse, error)
SetDriverPunctualAvailabilities(ctx context.Context, in *DriverPunctualAvailabilities, opts ...grpc.CallOption) (*DriverAvailabilitiesResponse, error)
CreateBooking(ctx context.Context, in *CreateBookingRequest, opts ...grpc.CallOption) (*CreateBookingResponse, error)
UpdateBooking(ctx context.Context, in *UpdateBookingRequest, opts ...grpc.CallOption) (*UpdateBookingResponse, error)
GetBooking(ctx context.Context, in *GetBookingRequest, opts ...grpc.CallOption) (*GetBookingResponse, error)
GetBookingsByStatus(ctx context.Context, in *GetBookingsByStatusRequest, opts ...grpc.CallOption) (*GetBookingsByStatusResponse, error)
DriverJourneys(ctx context.Context, in *DriverJourneysRequest, opts ...grpc.CallOption) (*DriverJourneysResponse, error)
SetPassengerTrip(ctx context.Context, in *PassengerTripRequest, opts ...grpc.CallOption) (*PassengerTripResponse, error)
}
type solidarityServiceClient struct {
cc grpc.ClientConnInterface
}
func NewSolidarityServiceClient(cc grpc.ClientConnInterface) SolidarityServiceClient {
return &solidarityServiceClient{cc}
}
func (c *solidarityServiceClient) SetDriverRegularAvailabilities(ctx context.Context, in *DriverRegularAvailabilities, opts ...grpc.CallOption) (*DriverAvailabilitiesResponse, error) {
out := new(DriverAvailabilitiesResponse)
err := c.cc.Invoke(ctx, SolidarityService_SetDriverRegularAvailabilities_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *solidarityServiceClient) SetDriverPunctualAvailabilities(ctx context.Context, in *DriverPunctualAvailabilities, opts ...grpc.CallOption) (*DriverAvailabilitiesResponse, error) {
out := new(DriverAvailabilitiesResponse)
err := c.cc.Invoke(ctx, SolidarityService_SetDriverPunctualAvailabilities_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *solidarityServiceClient) CreateBooking(ctx context.Context, in *CreateBookingRequest, opts ...grpc.CallOption) (*CreateBookingResponse, error) {
out := new(CreateBookingResponse)
err := c.cc.Invoke(ctx, SolidarityService_CreateBooking_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *solidarityServiceClient) UpdateBooking(ctx context.Context, in *UpdateBookingRequest, opts ...grpc.CallOption) (*UpdateBookingResponse, error) {
out := new(UpdateBookingResponse)
err := c.cc.Invoke(ctx, SolidarityService_UpdateBooking_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *solidarityServiceClient) GetBooking(ctx context.Context, in *GetBookingRequest, opts ...grpc.CallOption) (*GetBookingResponse, error) {
out := new(GetBookingResponse)
err := c.cc.Invoke(ctx, SolidarityService_GetBooking_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *solidarityServiceClient) GetBookingsByStatus(ctx context.Context, in *GetBookingsByStatusRequest, opts ...grpc.CallOption) (*GetBookingsByStatusResponse, error) {
out := new(GetBookingsByStatusResponse)
err := c.cc.Invoke(ctx, SolidarityService_GetBookingsByStatus_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *solidarityServiceClient) DriverJourneys(ctx context.Context, in *DriverJourneysRequest, opts ...grpc.CallOption) (*DriverJourneysResponse, error) {
out := new(DriverJourneysResponse)
err := c.cc.Invoke(ctx, SolidarityService_DriverJourneys_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *solidarityServiceClient) SetPassengerTrip(ctx context.Context, in *PassengerTripRequest, opts ...grpc.CallOption) (*PassengerTripResponse, error) {
out := new(PassengerTripResponse)
err := c.cc.Invoke(ctx, SolidarityService_SetPassengerTrip_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// SolidarityServiceServer is the server API for SolidarityService service.
// All implementations must embed UnimplementedSolidarityServiceServer
// for forward compatibility
type SolidarityServiceServer interface {
SetDriverRegularAvailabilities(context.Context, *DriverRegularAvailabilities) (*DriverAvailabilitiesResponse, error)
SetDriverPunctualAvailabilities(context.Context, *DriverPunctualAvailabilities) (*DriverAvailabilitiesResponse, error)
CreateBooking(context.Context, *CreateBookingRequest) (*CreateBookingResponse, error)
UpdateBooking(context.Context, *UpdateBookingRequest) (*UpdateBookingResponse, error)
GetBooking(context.Context, *GetBookingRequest) (*GetBookingResponse, error)
GetBookingsByStatus(context.Context, *GetBookingsByStatusRequest) (*GetBookingsByStatusResponse, error)
DriverJourneys(context.Context, *DriverJourneysRequest) (*DriverJourneysResponse, error)
SetPassengerTrip(context.Context, *PassengerTripRequest) (*PassengerTripResponse, error)
mustEmbedUnimplementedSolidarityServiceServer()
}
// UnimplementedSolidarityServiceServer must be embedded to have forward compatible implementations.
type UnimplementedSolidarityServiceServer struct {
}
func (UnimplementedSolidarityServiceServer) SetDriverRegularAvailabilities(context.Context, *DriverRegularAvailabilities) (*DriverAvailabilitiesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method SetDriverRegularAvailabilities not implemented")
}
func (UnimplementedSolidarityServiceServer) SetDriverPunctualAvailabilities(context.Context, *DriverPunctualAvailabilities) (*DriverAvailabilitiesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method SetDriverPunctualAvailabilities not implemented")
}
func (UnimplementedSolidarityServiceServer) CreateBooking(context.Context, *CreateBookingRequest) (*CreateBookingResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method CreateBooking not implemented")
}
func (UnimplementedSolidarityServiceServer) UpdateBooking(context.Context, *UpdateBookingRequest) (*UpdateBookingResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method UpdateBooking not implemented")
}
func (UnimplementedSolidarityServiceServer) GetBooking(context.Context, *GetBookingRequest) (*GetBookingResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetBooking not implemented")
}
func (UnimplementedSolidarityServiceServer) GetBookingsByStatus(context.Context, *GetBookingsByStatusRequest) (*GetBookingsByStatusResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetBookingsByStatus not implemented")
}
func (UnimplementedSolidarityServiceServer) DriverJourneys(context.Context, *DriverJourneysRequest) (*DriverJourneysResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DriverJourneys not implemented")
}
func (UnimplementedSolidarityServiceServer) SetPassengerTrip(context.Context, *PassengerTripRequest) (*PassengerTripResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method SetPassengerTrip not implemented")
}
func (UnimplementedSolidarityServiceServer) mustEmbedUnimplementedSolidarityServiceServer() {}
// UnsafeSolidarityServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to SolidarityServiceServer will
// result in compilation errors.
type UnsafeSolidarityServiceServer interface {
mustEmbedUnimplementedSolidarityServiceServer()
}
func RegisterSolidarityServiceServer(s grpc.ServiceRegistrar, srv SolidarityServiceServer) {
s.RegisterService(&SolidarityService_ServiceDesc, srv)
}
func _SolidarityService_SetDriverRegularAvailabilities_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DriverRegularAvailabilities)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SolidarityServiceServer).SetDriverRegularAvailabilities(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SolidarityService_SetDriverRegularAvailabilities_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SolidarityServiceServer).SetDriverRegularAvailabilities(ctx, req.(*DriverRegularAvailabilities))
}
return interceptor(ctx, in, info, handler)
}
func _SolidarityService_SetDriverPunctualAvailabilities_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DriverPunctualAvailabilities)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SolidarityServiceServer).SetDriverPunctualAvailabilities(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SolidarityService_SetDriverPunctualAvailabilities_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SolidarityServiceServer).SetDriverPunctualAvailabilities(ctx, req.(*DriverPunctualAvailabilities))
}
return interceptor(ctx, in, info, handler)
}
func _SolidarityService_CreateBooking_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(CreateBookingRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SolidarityServiceServer).CreateBooking(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SolidarityService_CreateBooking_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SolidarityServiceServer).CreateBooking(ctx, req.(*CreateBookingRequest))
}
return interceptor(ctx, in, info, handler)
}
func _SolidarityService_UpdateBooking_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UpdateBookingRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SolidarityServiceServer).UpdateBooking(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SolidarityService_UpdateBooking_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SolidarityServiceServer).UpdateBooking(ctx, req.(*UpdateBookingRequest))
}
return interceptor(ctx, in, info, handler)
}
func _SolidarityService_GetBooking_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetBookingRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SolidarityServiceServer).GetBooking(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SolidarityService_GetBooking_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SolidarityServiceServer).GetBooking(ctx, req.(*GetBookingRequest))
}
return interceptor(ctx, in, info, handler)
}
func _SolidarityService_GetBookingsByStatus_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetBookingsByStatusRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SolidarityServiceServer).GetBookingsByStatus(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SolidarityService_GetBookingsByStatus_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SolidarityServiceServer).GetBookingsByStatus(ctx, req.(*GetBookingsByStatusRequest))
}
return interceptor(ctx, in, info, handler)
}
func _SolidarityService_DriverJourneys_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DriverJourneysRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SolidarityServiceServer).DriverJourneys(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SolidarityService_DriverJourneys_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SolidarityServiceServer).DriverJourneys(ctx, req.(*DriverJourneysRequest))
}
return interceptor(ctx, in, info, handler)
}
func _SolidarityService_SetPassengerTrip_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(PassengerTripRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SolidarityServiceServer).SetPassengerTrip(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SolidarityService_SetPassengerTrip_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SolidarityServiceServer).SetPassengerTrip(ctx, req.(*PassengerTripRequest))
}
return interceptor(ctx, in, info, handler)
}
// SolidarityService_ServiceDesc is the grpc.ServiceDesc for SolidarityService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var SolidarityService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "SolidarityService",
HandlerType: (*SolidarityServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "SetDriverRegularAvailabilities",
Handler: _SolidarityService_SetDriverRegularAvailabilities_Handler,
},
{
MethodName: "SetDriverPunctualAvailabilities",
Handler: _SolidarityService_SetDriverPunctualAvailabilities_Handler,
},
{
MethodName: "CreateBooking",
Handler: _SolidarityService_CreateBooking_Handler,
},
{
MethodName: "UpdateBooking",
Handler: _SolidarityService_UpdateBooking_Handler,
},
{
MethodName: "GetBooking",
Handler: _SolidarityService_GetBooking_Handler,
},
{
MethodName: "GetBookingsByStatus",
Handler: _SolidarityService_GetBookingsByStatus_Handler,
},
{
MethodName: "DriverJourneys",
Handler: _SolidarityService_DriverJourneys_Handler,
},
{
MethodName: "SetPassengerTrip",
Handler: _SolidarityService_SetPassengerTrip_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "solidarity-api.proto",
}

View File

@@ -0,0 +1,27 @@
package grpcserver
import (
"solidarity-service/internal"
"solidarity-service/servers/grpc/proto"
)
func ConvertInternalToProtoBookingStatus(internalStatus internal.BookingStatus) proto.BookingStatus {
switch internalStatus {
case internal.BookingStatusINITIATED:
return proto.BookingStatus_INITIATED
case internal.BookingStatusWAITINGPASSENGERCONFIRMATION:
return proto.BookingStatus_WAITING_PASSENGER_CONFIRMATION
case internal.BookingStatusWAITINGDRIVERCONFIRMATION:
return proto.BookingStatus_WAITING_DRIVER_CONFIRMATION
case internal.BookingStatusCONFIRMED:
return proto.BookingStatus_CONFIRMED
case internal.BookingStatusCANCELLED:
return proto.BookingStatus_CANCELLED
case internal.BookingStatusCOMPLETEDPENDINGVALIDATION:
return proto.BookingStatus_COMPLETED_PENDING_VALIDATION
case internal.BookingStatusVALIDATED:
return proto.BookingStatus_VALIDATED
default:
return proto.BookingStatus_CONFIRMED
}
}

View File

@@ -0,0 +1,26 @@
package grpcserver
import (
"solidarity-service/servers/grpc/proto"
)
func ConvertToProtoDayOfTheWeek(day string) proto.DayOfWeek {
switch day {
case "MON":
return proto.DayOfWeek_MON
case "TUE":
return proto.DayOfWeek_TUE
case "WED":
return proto.DayOfWeek_WED
case "THU":
return proto.DayOfWeek_THU
case "FRI":
return proto.DayOfWeek_FRI
case "SAT":
return proto.DayOfWeek_FRI
case "SUN":
return proto.DayOfWeek_SAT
default:
return proto.DayOfWeek_MON
}
}

View File

@@ -0,0 +1,40 @@
package grpcserver
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"google.golang.org/grpc"
"net"
"solidarity-service/handler"
"solidarity-service/servers/grpc/proto"
)
type SolidarityServiceServerImpl struct {
Handler *handler.SolidarityServiceHandler
proto.UnimplementedSolidarityServiceServer
}
func NewSolidarityServiceServer(handler *handler.SolidarityServiceHandler) *SolidarityServiceServerImpl {
return &SolidarityServiceServerImpl{
Handler: handler,
}
}
func Run(done chan error, cfg *viper.Viper, handler *handler.SolidarityServiceHandler) {
var (
address = ":" + cfg.GetString("services.grpc.port")
)
server := grpc.NewServer()
proto.RegisterSolidarityServiceServer(server, NewSolidarityServiceServer(handler))
l, err := net.Listen("tcp", address)
if err != nil {
log.Fatal().Err(err)
return
}
if err := server.Serve(l); err != nil {
fmt.Println("gRPC service ended")
done <- err
}
}

View File

@@ -0,0 +1,543 @@
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: &timestamp.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: &timestamp.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: &timestamp.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: &timestamp.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: &timestamp.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
}

22
servers/utils/utils.go Normal file
View File

@@ -0,0 +1,22 @@
package utils
import "math"
func Haversine(lat1, lon1, lat2, lon2 float64) float64 {
// Radius of the Earth in kilometers
R := 6371.0
lat1 = lat1 * math.Pi / 180
lon1 = lon1 * math.Pi / 180
lat2 = lat2 * math.Pi / 180
lon2 = lon2 * math.Pi / 180
dlat := lat2 - lat1
dlon := lon2 - lon1
a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Cos(lat1)*math.Cos(lat2)*math.Sin(dlon/2)*math.Sin(dlon/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
distance := R * c
return distance
}