fleets/grpcapi/grpcapi.go

199 lines
6.5 KiB
Go
Raw Permalink Normal View History

2022-08-12 12:49:16 +00:00
package grpcapi
import (
context "context"
"fmt"
"log"
"net"
"git.coopgo.io/coopgo-platform/fleets/handlers"
2022-11-07 00:33:53 +00:00
"git.coopgo.io/coopgo-platform/fleets/storage"
2022-08-12 12:49:16 +00:00
"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) {
2022-11-07 00:33:53 +00:00
filter := storage.VehicleFilters{
2023-03-10 14:44:46 +00:00
Types: req.Types,
Administrators: req.Administrators,
2022-11-07 00:33:53 +00:00
}
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)
2022-08-12 12:49:16 +00:00
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) UpdateVehicle(context.Context, *UpdateVehicleRequest) (*UpdateVehicleResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method UpdateVehicle not implemented")
}
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
}
2022-09-05 05:26:43 +00:00
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
2022-08-12 12:49:16 +00:00
}
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")
}
2022-09-05 05:26:43 +00:00
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
}
2022-08-12 12:49:16 +00:00
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
}
}