golifehk/datasources/mtr/bus/query.go

60 lines
1.4 KiB
Go
Raw Normal View History

2022-09-14 09:12:48 +00:00
package bus
2022-09-13 13:42:51 +00:00
import (
"strings"
2022-09-16 20:33:47 +00:00
query "github.com/tgckpg/golifehk/query"
2022-09-13 13:42:51 +00:00
)
2022-09-16 20:33:47 +00:00
func Query( lang string, message string ) query.IQueryResult {
2022-09-14 09:12:48 +00:00
2022-09-16 20:33:47 +00:00
var qBusStops *query.QueryObject
var err error
2022-09-14 13:21:39 +00:00
2022-09-15 12:00:49 +00:00
qr := QueryResult{ Lang: lang }
2022-09-16 20:33:47 +00:00
busStops, err := getBusStops()
2022-09-14 13:21:39 +00:00
if err != nil {
2022-09-15 12:00:49 +00:00
qr.Error = err
2022-09-16 20:33:47 +00:00
goto qrReturn
2022-09-14 13:21:39 +00:00
}
2022-09-16 20:33:47 +00:00
qBusStops, err = query.Parse( strings.ToUpper( message ), busStops )
if err != nil {
qr.Error = err
goto qrReturn
}
2022-09-14 14:54:51 +00:00
2022-09-16 20:33:47 +00:00
qr.Query = qBusStops
if 0 < len( *qBusStops.Results ) && 1 < len( *qBusStops.SearchTerms ) {
schedules, err := getSchedule( lang, qBusStops.Key )
2022-09-14 13:21:39 +00:00
if err != nil {
2022-09-15 12:00:49 +00:00
qr.Error = err
2022-09-16 20:33:47 +00:00
goto qrReturn
2022-09-14 13:21:39 +00:00
}
if len( schedules.BusStops ) == 0 {
2022-09-16 20:33:47 +00:00
qr.Schedules = &map[*BusStop] *BusStopBuses{}
goto qrReturn
2022-09-14 13:21:39 +00:00
}
2022-09-16 20:33:47 +00:00
matches := map[*BusStop] *BusStopBuses{}
for _, entry := range *qBusStops.Results {
busStop := any( entry ).( *BusStop )
2022-09-14 13:21:39 +00:00
for _, busStopSch := range schedules.BusStops {
if busStopSch.BusStopId == busStop.StationId {
matches[busStop] = &busStopSch
break
}
}
}
2022-09-15 12:00:49 +00:00
qr.Schedules = &matches
2022-09-13 13:42:51 +00:00
}
2022-09-16 20:33:47 +00:00
qrReturn:
var iqr query.IQueryResult
iqr = &qr
return iqr
2022-09-13 13:42:51 +00:00
}