From d48c1ccf8efc3a7871fc2b6c8127b9b0663d314d Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Mon, 7 Nov 2022 01:33:53 +0100 Subject: [PATCH] Improve vehicles management --- grpcapi/grpcapi.go | 12 +++++++++++- handlers/vehicles.go | 6 +++++- storage/vehicles.go | 42 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/grpcapi/grpcapi.go b/grpcapi/grpcapi.go index 035ab6e..6938c09 100644 --- a/grpcapi/grpcapi.go +++ b/grpcapi/grpcapi.go @@ -7,6 +7,7 @@ import ( "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" @@ -53,7 +54,16 @@ func (s FleetsServerImpl) GetVehicle(ctx context.Context, req *GetVehicleRequest } func (s FleetsServerImpl) GetVehicles(ctx context.Context, req *GetVehiclesRequest) (*GetVehiclesResponse, error) { - responses, err := s.handler.GetVehicles(req.Namespaces) + filter := storage.VehicleFilters{ + Types: req.Types, + } + 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) } diff --git a/handlers/vehicles.go b/handlers/vehicles.go index 3dfd460..eba2647 100644 --- a/handlers/vehicles.go +++ b/handlers/vehicles.go @@ -37,7 +37,7 @@ func (h FleetsHandler) GetVehicle(id string) (vehicle *storage.Vehicle, err erro return vehicle, nil } -func (h FleetsHandler) GetVehicles(namespaces []string) ([]storage.Vehicle, error) { +func (h FleetsHandler) GetVehicles(namespaces []string, filters storage.VehicleFilters) ([]storage.Vehicle, error) { vehicles, err := h.storage.GetVehicles(namespaces) newvehicles := []storage.Vehicle{} for _, v := range vehicles { @@ -48,6 +48,10 @@ func (h FleetsHandler) GetVehicles(namespaces []string) ([]storage.Vehicle, erro } v.Bookings = bookings + if !v.MatchesFilters(filters) { + continue + } + newvehicles = append(newvehicles, v) } return newvehicles, err diff --git a/storage/vehicles.go b/storage/vehicles.go index d5b285f..2661cf2 100644 --- a/storage/vehicles.go +++ b/storage/vehicles.go @@ -1,6 +1,9 @@ package storage -import "time" +import ( + "fmt" + "time" +) type Vehicle struct { ID string `json:"id" bson:"_id"` @@ -13,13 +16,44 @@ type Vehicle struct { Bookings []Booking } +type VehicleFilters struct { + Types []string + AvailableFrom time.Time + AvailableTo time.Time +} + func (v Vehicle) Free(start time.Time, end time.Time) bool { for _, b := range v.Bookings { - if (start.After(b.Unavailablefrom) && start.Before(b.Unavailableto)) || - (end.After(b.Unavailablefrom) && end.Before(b.Unavailableto)) || - (start.Before(b.Unavailablefrom) && end.After(b.Unavailableto)) { + fmt.Println("Bookings for", v) + fmt.Println(b) + if (start.Before(b.Unavailablefrom) && end.After(b.Unavailablefrom.Add(24*time.Hour))) || + (start.Before(b.Unavailableto) && end.After(b.Unavailableto.Add(24*time.Hour))) || + (start.After(b.Unavailablefrom) && end.Before(b.Unavailableto.Add(24*time.Hour))) { return false } } return true } + +func (v Vehicle) MatchesFilters(filters VehicleFilters) bool { + if len(filters.Types) > 0 { + found := false + for _, t := range filters.Types { + if t == v.Type { + found = true + break + } + } + if !found { + return false + } + } + + if !v.Free(filters.AvailableFrom, filters.AvailableTo) { + return false + } + + fmt.Println(filters) + + return true +}