From 5a5f81c0fbe7016928e28aa9bb7ab9c2e654906c Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 23 Oct 2022 16:45:11 +0200 Subject: [PATCH] Refactor --- api/destinations.go | 64 ++++++++++++++++++++++----------------------- api/schedules.go | 2 +- api/stations.go | 10 +------ 3 files changed, 34 insertions(+), 42 deletions(-) diff --git a/api/destinations.go b/api/destinations.go index 936861d..a40673d 100644 --- a/api/destinations.go +++ b/api/destinations.go @@ -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, })) diff --git a/api/schedules.go b/api/schedules.go index e92cba7..af7e960 100644 --- a/api/schedules.go +++ b/api/schedules.go @@ -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 } diff --git a/api/stations.go b/api/stations.go index 75f6db9..a33166b 100644 --- a/api/stations.go +++ b/api/stations.go @@ -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 {