Initial commit
This commit is contained in:
commit
0a854f708a
17 changed files with 796 additions and 0 deletions
138
api/lines.go
Normal file
138
api/lines.go
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type IDFMLine struct {
|
||||
DatasetID string `json:"datasetid"`
|
||||
RecordIDs string `json:"recordid"`
|
||||
Fields struct {
|
||||
IdLine string `json:"id_line"`
|
||||
Name string `json:"name_line"`
|
||||
ShortName string `json:"shortname_line"`
|
||||
TransportMode string `json:"transportmode"`
|
||||
TransportSubmode string `json:"transportsubmode"`
|
||||
Type string `json:"type"`
|
||||
OperatorRef string `json:"operatorref"`
|
||||
AdditionalOperators string `json:"additionaloperators,omitempty"`
|
||||
OperatorName string `json:"operatorname"`
|
||||
NetworkName string `json:"networkname,omitempty"`
|
||||
WebColor string `json:"colourweb_hexa"`
|
||||
PrintColor string `json:"colourprint_cmjn"`
|
||||
WebTextColor string `json:"textcolourweb_hexa"`
|
||||
TextPrintColor string `json:"textcolourprint_hexa"`
|
||||
Accessibility string `json:"accessibility"`
|
||||
AudibleSignAvailable string `json:"audiblesigns_available"`
|
||||
VisualSignAvailable string `json:"visualsigns_available"`
|
||||
NoticeTitle string `json:"notice_title"`
|
||||
NoticeText string `json:"notice_text"`
|
||||
Status string `json:"status"`
|
||||
ShortNameGroup string `json:"shortname_groupoflines"`
|
||||
IdGroup string `json:"id_groupoflines"`
|
||||
ExternalCode string `json:"externalcode_line"`
|
||||
} `json:"fields"`
|
||||
RecordTimestamp time.Time `json:"record_timestamp"`
|
||||
}
|
||||
|
||||
type PGLine struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Directions string `json:"directions"`
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
var IDFMLines []IDFMLine
|
||||
|
||||
func init() {
|
||||
fd, err := os.Open("referentiel-des-lignes.json")
|
||||
if err != nil {
|
||||
log.Fatal("Unable to open `referentiel-des-lignes.json`:", err.Error())
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
dec := json.NewDecoder(fd)
|
||||
if err = dec.Decode(&IDFMLines); err != nil {
|
||||
log.Fatal("Unable to decode `referentiel-des-lignes.json`:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func convertLineType(old string) string {
|
||||
switch old {
|
||||
case "buses":
|
||||
return "bus"
|
||||
case "metros":
|
||||
return "metro"
|
||||
case "noctiliens":
|
||||
return "noctilien"
|
||||
case "rers":
|
||||
return "rail"
|
||||
case "tramways":
|
||||
return "tram"
|
||||
default:
|
||||
return old
|
||||
}
|
||||
}
|
||||
|
||||
func declareLinesRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/lines", func(c *gin.Context) {
|
||||
var modes []string
|
||||
for _, line := range IDFMLines {
|
||||
mode := line.Fields.TransportMode
|
||||
|
||||
found := false
|
||||
for _, m := range modes {
|
||||
if mode == m {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
modes = append(modes, mode)
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, APIResult(c, modes))
|
||||
})
|
||||
|
||||
router.GET("/lines/:type", func(c *gin.Context) {
|
||||
t := convertLineType(string(c.Param("type")))
|
||||
|
||||
var lines []PGLine
|
||||
for _, line := range IDFMLines {
|
||||
if line.Fields.TransportMode == t || strings.ToLower(line.Fields.NetworkName) == t {
|
||||
name := line.Fields.Name
|
||||
|
||||
if line.Fields.ShortNameGroup != "" {
|
||||
if strings.Contains(line.Fields.ShortNameGroup, name) {
|
||||
name = line.Fields.ShortNameGroup
|
||||
} else if name == line.Fields.ShortName {
|
||||
name = fmt.Sprintf("%s - %s", line.Fields.Name, line.Fields.ShortNameGroup)
|
||||
}
|
||||
}
|
||||
|
||||
pgline := PGLine{
|
||||
Code: fmt.Sprintf("IDFM:%s", line.Fields.IdLine),
|
||||
Name: name,
|
||||
Directions: "",
|
||||
Id: line.Fields.IdLine,
|
||||
}
|
||||
|
||||
lines = append(lines, pgline)
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, APIResult(c, map[string][]PGLine{
|
||||
t: lines,
|
||||
}))
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue