-
Notifications
You must be signed in to change notification settings - Fork 2
/
stop_departure_request.go
83 lines (77 loc) · 2.26 KB
/
stop_departure_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package rest
import (
_ "embed"
"strconv"
"strings"
)
//go:embed stop_departure_request.gql
var stopDepartureQuery string
// StopDepartureRequest holds options for a /stops/_/departures request
type StopDepartureRequest struct {
StopKey string `json:"stop_key"`
ID int `json:"id,string"`
StopID string `json:"stop_id"`
FeedOnestopID string `json:"feed_onestop_id"`
OnestopID string `json:"onestop_id"`
Next int `json:"next,string"`
ServiceDate string `json:"service_date"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
IncludeGeometry bool `json:"include_geometry,string"`
IncludeAlerts bool `json:"include_alerts,string"`
UseServiceWindow *bool `json:"use_service_window,string"`
WithCursor
}
// ResponseKey returns the GraphQL response entity key.
func (r StopDepartureRequest) ResponseKey() string { return "stops" }
// IncludeNext
func (r StopDepartureRequest) IncludeNext() bool { return false }
// Query returns a GraphQL query string and variables.
func (r StopDepartureRequest) Query() (string, map[string]interface{}) {
if r.StopKey == "" {
// TODO: add a way to reject request as invalid
} else if fsid, eid, ok := strings.Cut(r.StopKey, ":"); ok {
r.FeedOnestopID = fsid
r.StopID = eid
} else if v, err := strconv.Atoi(r.StopKey); err == nil && v > 0 {
// require an actual ID, not just 0
r.ID = v
} else {
r.OnestopID = r.StopKey
}
where := hw{}
if r.OnestopID != "" {
where["onestop_id"] = r.OnestopID
}
if r.FeedOnestopID != "" {
where["feed_onestop_id"] = r.FeedOnestopID
}
if r.StopID != "" {
where["stop_id"] = r.StopID
}
//
stwhere := hw{}
if r.UseServiceWindow == nil || *r.UseServiceWindow {
stwhere["use_service_window"] = true
}
if r.ServiceDate != "" {
stwhere["service_date"] = r.ServiceDate
stwhere["start"] = r.StartTime
if r.EndTime != "" {
stwhere["end"] = r.EndTime
}
} else {
if r.Next == 0 {
r.Next = 3600
}
stwhere["next"] = r.Next
}
return stopDepartureQuery, hw{
"include_geometry": r.IncludeGeometry,
"include_alerts": r.IncludeAlerts,
"limit": r.CheckLimit(),
"ids": checkIds(r.ID),
"where": where,
"stop_time_where": stwhere,
}
}