This commit is contained in:
nemunaire 2022-10-23 16:45:11 +02:00
parent da0187b74d
commit 5a5f81c0fb
3 changed files with 34 additions and 42 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/gin-gonic/gin"
)
@ -107,46 +106,47 @@ func getSchedules(code string) (*IDFMSchedule, error) {
return &schedules, nil
}
func getDestinations(code string) ([]PGDestination, error) {
schedule, err := getSchedules(code)
if err != nil {
return nil, err
}
var pgd []PGDestination
destination:
for i, s := range schedule.Schedules {
for _, d := range pgd {
if d.Name == s.To {
continue destination
}
}
way := "R"
if i%2 == 0 {
way = "A"
}
pgd = append(pgd, PGDestination{
Name: s.To,
Way: way,
})
}
return pgd, nil
}
func declareDestinationsRoutes(router *gin.RouterGroup) {
router.GET("/destinations/:type/:code", func(c *gin.Context) {
t := convertLineType(string(c.Param("type")))
code := string(c.Param("code"))
code := convertCode(t, string(c.Param("code")))
if !strings.HasPrefix(code, "line:IDFM:") {
if len(code) != 6 || !strings.HasPrefix(code, "C") {
code = searchLine(t, code)
}
code = "line:IDFM:" + code
}
schedule, err := getSchedules(code)
pgd, err := getDestinations(code)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
pgd := []PGDestination{}
destination:
for i, s := range schedule.Schedules {
for _, d := range pgd {
if d.Name == s.To {
continue destination
}
}
way := "R"
if i%2 == 0 {
way = "A"
}
pgd = append(pgd, PGDestination{
Name: s.To,
Way: way,
})
}
c.JSON(http.StatusOK, APIResult(c, map[string][]PGDestination{
"destinations": pgd,
}))

View File

@ -164,7 +164,7 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
pgs := []PGSchedule{}
for _, vehicule := range schedules.NextDepartures.Data {
if (way == "A" && vehicule.Sens == "-1") || (way == "R" && vehicule.Sens == "1") {
if (way == "A" && vehicule.Sens == "1") || (way == "R" && vehicule.Sens == "-1") {
continue
}

View File

@ -106,15 +106,7 @@ func getStations(code string) (stations []IDFMStation, err error) {
func declareStationsRoutes(router *gin.RouterGroup) {
router.GET("/stations/:type/:code", func(c *gin.Context) {
t := convertLineType(string(c.Param("type")))
code := string(c.Param("code"))
if !strings.HasPrefix(code, "line:IDFM:") {
if len(code) != 6 || !strings.HasPrefix(code, "C") {
code = searchLine(t, code)
}
code = "line:IDFM:" + code
}
code := convertCode(t, string(c.Param("code")))
stations, err := getStations(code)
if err != nil {