filterAndLimit

This commit is contained in:
Paul Brinkmeier 2026-05-03 09:50:31 +02:00
parent c836a3b201
commit 62c31bb28c
2 changed files with 28 additions and 6 deletions

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
.PHONY: format
format:
gofmt -w $$(git ls-files '*.go')

22
main.go
View File

@ -42,12 +42,10 @@ func main() {
}
stopId := query["stop_id"][0]
/*
var platform *string = nil
if query["platform"] != nil && len(query["platform"]) != 0 {
platform = &query["platform"][0]
}
*/
c := efaClients[(efaClient.Add(1)-1)%uint64(len(efaClients))]
departures, err := FetchDepartures(c, stopId)
@ -57,6 +55,8 @@ func main() {
}
// TODO: Filter for platform here
filterAndLimitDepartures(&departures, platform, 8)
// Does not handle multiple media types
if r.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-Type", "application/json")
@ -75,3 +75,21 @@ func main() {
})
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)
}
}