filterAndLimit
This commit is contained in:
@@ -42,12 +42,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
stopId := query["stop_id"][0]
|
stopId := query["stop_id"][0]
|
||||||
|
|
||||||
/*
|
|
||||||
var platform *string = nil
|
var platform *string = nil
|
||||||
if query["platform"] != nil && len(query["platform"]) != 0 {
|
if query["platform"] != nil && len(query["platform"]) != 0 {
|
||||||
platform = &query["platform"][0]
|
platform = &query["platform"][0]
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
c := efaClients[(efaClient.Add(1)-1)%uint64(len(efaClients))]
|
c := efaClients[(efaClient.Add(1)-1)%uint64(len(efaClients))]
|
||||||
departures, err := FetchDepartures(c, stopId)
|
departures, err := FetchDepartures(c, stopId)
|
||||||
@@ -57,6 +55,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
// TODO: Filter for platform here
|
// TODO: Filter for platform here
|
||||||
|
|
||||||
|
filterAndLimitDepartures(&departures, platform, 8)
|
||||||
|
|
||||||
// Does not handle multiple media types
|
// Does not handle multiple media types
|
||||||
if r.Header.Get("Accept") == "application/json" {
|
if r.Header.Get("Accept") == "application/json" {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
@@ -75,3 +75,21 @@ func main() {
|
|||||||
})
|
})
|
||||||
log.Fatal(http.ListenAndServe(":8000", nil))
|
log.Fatal(http.ListenAndServe(":8000", nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func filterAndLimitDepartures(departures *Departures, platform *string, limit int) {
|
||||||
|
oldDepartures := departures.Departures
|
||||||
|
|
||||||
|
departures.Departures = []Departure{}
|
||||||
|
for _, departure := range oldDepartures {
|
||||||
|
if len(departures.Departures) >= limit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
isPlatformMatch := platform == nil || departure.Platform == *platform
|
||||||
|
if !isPlatformMatch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
departures.Departures = append(departures.Departures, departure)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user