diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9d9fc62 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: format + +format: + gofmt -w $$(git ls-files '*.go') diff --git a/main.go b/main.go index 8498fbb..ef645c2 100644 --- a/main.go +++ b/main.go @@ -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] - } - */ + 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) + } +}