idfm-api/api/stations.go

74 lines
1.7 KiB
Go

package api
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
type IDFMStation struct {
DatasetID string `json:"datasetid"`
RecordIDs string `json:"recordid"`
Fields struct {
Id string `json:"id"`
PointGeo []float64 `json:"pointgeo"`
StopId string `json:"stop_id"`
StopName string `json:"stop_name"`
OperatorName string `json:"operatorname"`
NomCommune string `json:"nom_commune"`
RouteLongName string `json:"route_long_name"`
StopLat string `json:"stop_lat"`
StopLon string `json:"stop_lon"`
CodeINSEE string `json:"code_insee"`
} `json:"fields"`
Geometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
} `json:"geometry"`
}
type PGStation struct {
Id string `json:"slug"`
Name string `json:"name"`
}
var IDFMStations []IDFMStation
func init() {
fd, err := os.Open("arrets-lignes.json")
if err != nil {
log.Fatal("Unable to open `arrets-lignes.json`:", err.Error())
}
defer fd.Close()
dec := json.NewDecoder(fd)
if err = dec.Decode(&IDFMStations); err != nil {
log.Fatal("Unable to decode `arrets-lignes.json`:", err.Error())
}
}
func declareStationsRoutes(router *gin.RouterGroup) {
router.GET("/stations/:type/:code", func(c *gin.Context) {
code := convertLineType(string(c.Param("code")))
var stations []PGStation
for _, station := range IDFMStations {
if station.Fields.Id == code {
pgstation := PGStation{
Id: station.Fields.StopId,
Name: station.Fields.StopName,
}
stations = append(stations, pgstation)
}
}
c.JSON(http.StatusOK, APIResult(c, map[string][]PGStation{
"stations": stations,
}))
})
}