Refactor
This commit is contained in:
parent
da0187b74d
commit
5a5f81c0fb
@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -107,46 +106,47 @@ func getSchedules(code string) (*IDFMSchedule, error) {
|
|||||||
return &schedules, nil
|
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) {
|
func declareDestinationsRoutes(router *gin.RouterGroup) {
|
||||||
router.GET("/destinations/:type/:code", func(c *gin.Context) {
|
router.GET("/destinations/:type/:code", func(c *gin.Context) {
|
||||||
t := convertLineType(string(c.Param("type")))
|
t := convertLineType(string(c.Param("type")))
|
||||||
code := string(c.Param("code"))
|
code := convertCode(t, string(c.Param("code")))
|
||||||
|
|
||||||
if !strings.HasPrefix(code, "line:IDFM:") {
|
pgd, err := getDestinations(code)
|
||||||
if len(code) != 6 || !strings.HasPrefix(code, "C") {
|
|
||||||
code = searchLine(t, code)
|
|
||||||
}
|
|
||||||
|
|
||||||
code = "line:IDFM:" + code
|
|
||||||
}
|
|
||||||
|
|
||||||
schedule, err := getSchedules(code)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
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{
|
c.JSON(http.StatusOK, APIResult(c, map[string][]PGDestination{
|
||||||
"destinations": pgd,
|
"destinations": pgd,
|
||||||
}))
|
}))
|
||||||
|
@ -164,7 +164,7 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
|
|||||||
|
|
||||||
pgs := []PGSchedule{}
|
pgs := []PGSchedule{}
|
||||||
for _, vehicule := range schedules.NextDepartures.Data {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,15 +106,7 @@ func getStations(code string) (stations []IDFMStation, err error) {
|
|||||||
func declareStationsRoutes(router *gin.RouterGroup) {
|
func declareStationsRoutes(router *gin.RouterGroup) {
|
||||||
router.GET("/stations/:type/:code", func(c *gin.Context) {
|
router.GET("/stations/:type/:code", func(c *gin.Context) {
|
||||||
t := convertLineType(string(c.Param("type")))
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
stations, err := getStations(code)
|
stations, err := getStations(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user