Skip to content

Commit

Permalink
Limit to one vehicule type
Browse files Browse the repository at this point in the history
  • Loading branch information
craftlion committed Aug 6, 2024
1 parent 3592c27 commit 66900f3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
20 changes: 12 additions & 8 deletions communautofinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const dateFormat = "2006-01-02T15:04:05" // time format accepted by communauto A

// As soon as at least one car is found return the number of cars found

func SearchStationCar(cityId CityId, currentCoordinate Coordinate, marginInKm float64, startDate time.Time, endDate time.Time, vehiculeTypes []VehiculeType) int {
func SearchStationCar(cityId CityId, currentCoordinate Coordinate, marginInKm float64, startDate time.Time, endDate time.Time, vehiculeType VehiculeType) int {
responseChannel := make(chan int, 1)
ctx, cancel := context.WithCancel(context.Background())
nbCarFound := searchCar(SearchingStation, cityId, currentCoordinate, marginInKm, startDate, endDate, vehiculeTypes, responseChannel, ctx, cancel)
nbCarFound := searchCar(SearchingStation, cityId, currentCoordinate, marginInKm, startDate, endDate, vehiculeType, responseChannel, ctx, cancel)
cancel()

return nbCarFound
Expand All @@ -30,14 +30,14 @@ func SearchStationCar(cityId CityId, currentCoordinate Coordinate, marginInKm fl
func SearchFlexCar(cityId CityId, currentCoordinate Coordinate, marginInKm float64) int {
responseChannel := make(chan int, 1)
ctx, cancel := context.WithCancel(context.Background())
nbCarFound := searchCar(SearchingFlex, cityId, currentCoordinate, marginInKm, time.Time{}, time.Time{}, []VehiculeType{}, responseChannel, ctx, cancel)
nbCarFound := searchCar(SearchingFlex, cityId, currentCoordinate, marginInKm, time.Time{}, time.Time{}, AllTypes, responseChannel, ctx, cancel)
cancel()

return nbCarFound
}

// This function is designed to be called as a goroutine. As soon as at least one car is found return the number of cars found. Or can be cancelled by the context
func SearchStationCarForGoRoutine(cityId CityId, currentCoordinate Coordinate, marginInKm float64, startDate time.Time, endDate time.Time, vehiculeTypes []VehiculeType, responseChannel chan<- int, ctx context.Context, cancelCtxFunc context.CancelFunc) int {
func SearchStationCarForGoRoutine(cityId CityId, currentCoordinate Coordinate, marginInKm float64, startDate time.Time, endDate time.Time, vehiculeType VehiculeType, responseChannel chan<- int, ctx context.Context, cancelCtxFunc context.CancelFunc) int {

defer func() {
if r := recover(); r != nil {
Expand All @@ -46,7 +46,7 @@ func SearchStationCarForGoRoutine(cityId CityId, currentCoordinate Coordinate, m
}
}()

return searchCar(SearchingStation, cityId, currentCoordinate, marginInKm, startDate, endDate, vehiculeTypes, responseChannel, ctx, cancelCtxFunc)
return searchCar(SearchingStation, cityId, currentCoordinate, marginInKm, startDate, endDate, vehiculeType, responseChannel, ctx, cancelCtxFunc)
}

// This function is designed to be called as a goroutine. As soon as at least one car is found return the number of cars found. Or can be cancelled by the context
Expand All @@ -59,11 +59,11 @@ func SearchFlexCarForGoRoutine(cityId CityId, currentCoordinate Coordinate, marg
}
}()

return searchCar(SearchingFlex, cityId, currentCoordinate, marginInKm, time.Time{}, time.Time{}, []VehiculeType{}, responseChannel, ctx, cancelCtxFunc)
return searchCar(SearchingFlex, cityId, currentCoordinate, marginInKm, time.Time{}, time.Time{}, AllTypes, responseChannel, ctx, cancelCtxFunc)
}

// Loop until a result is found. Return the number of cars found or can be cancelled by the context
func searchCar(searchingType SearchType, cityId CityId, currentCoordinate Coordinate, marginInKm float64, startDate time.Time, endDate time.Time, vehiculeTypes []VehiculeType, responseChannel chan<- int, ctx context.Context, cancelCtxFunc context.CancelFunc) int {
func searchCar(searchingType SearchType, cityId CityId, currentCoordinate Coordinate, marginInKm float64, startDate time.Time, endDate time.Time, vehiculeType VehiculeType, responseChannel chan<- int, ctx context.Context, cancelCtxFunc context.CancelFunc) int {
minCoordinate, maxCoordinate := currentCoordinate.ExpandCoordinate(marginInKm)

var urlCalled string
Expand All @@ -74,7 +74,11 @@ func searchCar(searchingType SearchType, cityId CityId, currentCoordinate Coordi
startDateFormat := startDate.Format(dateFormat)
endDataFormat := endDate.Format(dateFormat)

urlCalled = fmt.Sprintf("https://restapifrontoffice.reservauto.net/api/v2/StationAvailability?CityId=%d&MaxLatitude=%f&MinLatitude=%f&MaxLongitude=%f&MinLongitude=%f&StartDate=%s&EndDate=%s&VehicleTypes=%v", cityId, maxCoordinate.latitude, minCoordinate.latitude, maxCoordinate.longitude, minCoordinate.longitude, url.QueryEscape(startDateFormat), url.QueryEscape(endDataFormat), vehiculeTypes)
urlCalled = fmt.Sprintf("https://restapifrontoffice.reservauto.net/api/v2/StationAvailability?CityId=%d&MaxLatitude=%f&MinLatitude=%f&MaxLongitude=%f&MinLongitude=%f&StartDate=%s&EndDate=%s", cityId, maxCoordinate.latitude, minCoordinate.latitude, maxCoordinate.longitude, minCoordinate.longitude, url.QueryEscape(startDateFormat), url.QueryEscape(endDataFormat))

if vehiculeType != AllTypes {
urlCalled += fmt.Sprintf("&VehicleTypes=%d", vehiculeType)
}
}

msSecondeToSleep := 0
Expand Down
4 changes: 2 additions & 2 deletions communautofinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestUseExemple(t *testing.T) {
fmt.Printf("Flex cars found : %d \n", nbCarFoundFlex)

// Search station car
nbCarFoundStation := SearchStationCar(cityId, currentCoordinate, 10, startDate, endDate, []VehiculeType{3, 5})
nbCarFoundStation := SearchStationCar(cityId, currentCoordinate, 10, startDate, endDate, FamilyCar)
fmt.Printf("Station cars found : %d \n", nbCarFoundStation)

/////////////////////////////////
Expand All @@ -39,7 +39,7 @@ func TestUseExemple(t *testing.T) {
fmt.Printf("Flex cars found : %d \n", nbCarFoundFlex)

// Search station car with go routine
go SearchStationCarForGoRoutine(cityId, currentCoordinate, 10, startDate, endDate, []VehiculeType{3, 5}, resultsChannelStation, ctx, cancel)
go SearchStationCarForGoRoutine(cityId, currentCoordinate, 10, startDate, endDate, FamilyCar, resultsChannelStation, ctx, cancel)
nbCarFoundStation = <-resultsChannelStation
fmt.Printf("Station cars found : %d \n", nbCarFoundStation)

Expand Down
1 change: 1 addition & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
type VehiculeType int

const (
AllTypes VehiculeType = 0
FamilyCar VehiculeType = 1
UtilityVehicle VehiculeType = 2
MidSize VehiculeType = 3
Expand Down

0 comments on commit 66900f3

Please sign in to comment.