parcoursmob/services/fleets.go

74 lines
1.7 KiB
Go
Raw Normal View History

package services
import (
"context"
"sort"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
"git.coopgo.io/coopgo-platform/fleets/storage"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)
type FleetsService struct {
fleets.FleetsClient
}
func NewFleetsService(dial string) (*FleetsService, error) {
conn, err := grpc.Dial(dial, grpc.WithInsecure())
client := fleets.NewFleetsClient(conn)
if err != nil {
return nil, err
}
return &FleetsService{
FleetsClient: client,
}, nil
}
func (s *ServicesHandler) GetBooking(bookingid string) (booking storage.Booking, err error) {
request := &fleets.GetBookingRequest{
Bookingid: bookingid,
}
resp, err := s.GRPC.Fleets.GetBooking(context.TODO(), request)
if err == nil {
booking = resp.Booking.ToStorageType()
}
return
}
func (s *ServicesHandler) GetBookings() (bookings []storage.Booking, err error) {
bookings = []storage.Booking{}
request := &fleets.GetBookingsRequest{}
resp, err := s.GRPC.Fleets.GetBookings(context.TODO(), request)
if err == nil {
for _, booking := range resp.Bookings {
bookings = append(bookings, booking.ToStorageType())
}
sort.Sort(sorting.BookingsByStartdate(bookings))
}
return
}
2023-01-17 07:31:07 +00:00
func (s *ServicesHandler) GetVehiclesMap() (vehicles map[string]storage.Vehicle, err error) {
vehicles = map[string]storage.Vehicle{}
2023-12-08 12:29:25 +00:00
request := &fleets.GetVehiclesRequest{
Namespaces: []string{"parcoursmob"},
}
2023-01-17 07:31:07 +00:00
resp, err := s.GRPC.Fleets.GetVehicles(context.TODO(), request)
2023-12-08 12:29:25 +00:00
if err != nil {
log.Error().Err(err).Msg("")
2023-12-08 12:29:25 +00:00
} else {
2023-01-17 07:31:07 +00:00
for _, vehicle := range resp.Vehicles {
vehicles[vehicle.Id] = vehicle.ToStorageType()
}
}
2023-12-08 12:29:25 +00:00
return vehicles, err
2023-01-17 07:31:07 +00:00
}