package grpcapi import ( context "context" "fmt" "log" "net" "git.coopgo.io/coopgo-platform/fleets/handlers" "git.coopgo.io/coopgo-platform/fleets/storage" "github.com/spf13/viper" "google.golang.org/grpc" codes "google.golang.org/grpc/codes" "google.golang.org/grpc/reflection" status "google.golang.org/grpc/status" ) type FleetsServerImpl struct { handler handlers.FleetsHandler } func NewFleetsServer(h handlers.FleetsHandler) *FleetsServerImpl { return &FleetsServerImpl{ handler: h, } } func (s FleetsServerImpl) AddVehicle(ctx context.Context, req *AddVehicleRequest) (*AddVehicleResponse, error) { v := req.Vehicle.ToStorageType() vehicle, err := s.handler.AddVehicle(v) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "vehicle creation failed : %v", err) } response, err := VehicleFromStorageType(vehicle) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving vehicle : %v", err) } return &AddVehicleResponse{Vehicle: response}, nil } func (s FleetsServerImpl) GetVehicle(ctx context.Context, req *GetVehicleRequest) (*GetVehicleResponse, error) { vehicle, err := s.handler.GetVehicle(req.Vehicleid) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving vehicle : %v", err) } response, err := VehicleFromStorageType(vehicle) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving vehicle : %v", err) } return &GetVehicleResponse{Vehicle: response}, nil } func (s FleetsServerImpl) GetVehicles(ctx context.Context, req *GetVehiclesRequest) (*GetVehiclesResponse, error) { filter := storage.VehicleFilters{ Types: req.Types, Administrators: req.Administrators, } if req.AvailabilityFrom.IsValid() { filter.AvailableFrom = req.AvailabilityFrom.AsTime() } if req.AvailabilityTo.IsValid() { filter.AvailableTo = req.AvailabilityTo.AsTime() } responses, err := s.handler.GetVehicles(req.Namespaces, filter) if err != nil { return nil, status.Errorf(codes.NotFound, "could not get vehicles : %v", err) } var vehicles []*Vehicle for _, v := range responses { vehicle, err := VehicleFromStorageType(&v) if err != nil { return nil, status.Errorf(codes.Internal, "could not get vehicle : %v", err) } vehicles = append(vehicles, vehicle) } return &GetVehiclesResponse{Vehicles: vehicles}, nil } func (s FleetsServerImpl) CreateBooking(ctx context.Context, req *CreateBookingRequest) (*CreateBookingResponse, error) { b := req.Booking.ToStorageType() booking, err := s.handler.AddBooking(b) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "booking creation failed : %v", err) } response, err := BookingFromStorageType(booking) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving booking : %v", err) } return &CreateBookingResponse{Booking: response}, nil } func (s FleetsServerImpl) GetBooking(ctx context.Context, req *GetBookingRequest) (*GetBookingResponse, error) { booking, err := s.handler.GetBooking(req.Bookingid) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving booking : %v", err) } response, err := BookingFromStorageType(booking) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving booking : %v", err) } return &GetBookingResponse{Booking: response}, nil } func (s FleetsServerImpl) UpdateBooking(ctx context.Context, req *UpdateBookingRequest) (*UpdateBookingResponse, error) { b := req.Booking.ToStorageType() booking, err := s.handler.UpdateBooking(b) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "booking update failed : %v", err) } response, err := BookingFromStorageType(booking) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving booking : %v", err) } return &UpdateBookingResponse{Booking: response}, nil } func (s FleetsServerImpl) DeleteBooking(ctx context.Context, req *DeleteBookingRequest) (*DeleteBookingResponse, error) { if err := s.handler.DeleteBooking(req.Id); err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving booking : %v", err) } return &DeleteBookingResponse{ Ok: true, }, nil } func (s FleetsServerImpl) FindVehicle(context.Context, *FindVehicleRequest) (*FindVehicleResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method FindVehicle not implemented") } func (s FleetsServerImpl) GetDriverBookings(ctx context.Context, req *GetDriverBookingsRequest) (*GetDriverBookingsResponse, error) { responses, err := s.handler.GetDriverBookings(req.Driver) if err != nil { return nil, status.Errorf(codes.NotFound, "could not get driver bookings : %v", err) } var bookings []*Booking for _, b := range responses { booking, err := BookingFromStorageType(&b) if err != nil { return nil, status.Errorf(codes.Internal, "could not get booking : %v", err) } bookings = append(bookings, booking) } return &GetDriverBookingsResponse{Bookings: bookings}, nil } func (s FleetsServerImpl) GetBookings(ctx context.Context, req *GetBookingsRequest) (*GetBookingsResponse, error) { responses, err := s.handler.GetBookings() if err != nil { return nil, status.Errorf(codes.NotFound, "could not get bookings : %v", err) } var bookings []*Booking for _, b := range responses { booking, err := BookingFromStorageType(&b) if err != nil { return nil, status.Errorf(codes.Internal, "could not get booking : %v", err) } bookings = append(bookings, booking) } return &GetBookingsResponse{Bookings: bookings}, nil } func (s FleetsServerImpl) mustEmbedUnimplementedFleetsServer() {} func Run(done chan error, cfg *viper.Viper, handler handlers.FleetsHandler) { var ( dev_env = cfg.GetBool("dev_env") address = ":" + cfg.GetString("services.grpc.port") ) fmt.Println("-> GRPC server on", address) server := grpc.NewServer() RegisterFleetsServer(server, NewFleetsServer(handler)) l, err := net.Listen("tcp", address) if err != nil { log.Fatal(err) } if dev_env { reflection.Register(server) } if err := server.Serve(l); err != nil { fmt.Println("gRPC service ended") done <- err } } /////////////////////update//////////////////////// func (s FleetsServerImpl) UpdateVehicle(ctx context.Context, req *UpdateVehicleRequest) (*UpdateVehicleResponse, error) { b := req.Vehicle.ToStorageType() vehicle, err := s.handler.UpdateVehicle(b) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.AlreadyExists, "vehicle update failed : %v", err) } response, err := VehicleFromStorageType(vehicle) if err != nil { fmt.Println(err) return nil, status.Errorf(codes.Internal, "issue while retrieving vehicle : %v", err) } return &UpdateVehicleResponse{Vehicle: response}, nil }