filterAndLimit
This commit is contained in:
parent
c836a3b201
commit
62c31bb28c
4
Makefile
Normal file
4
Makefile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.PHONY: format
|
||||||
|
|
||||||
|
format:
|
||||||
|
gofmt -w $$(git ls-files '*.go')
|
||||||
30
main.go
30
main.go
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user