From 678fdf8759a7a6ed6628194d085a47b5490e68ea Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 10 Sep 2021 17:26:23 +0200 Subject: Finished not so simple refactoring --- internal/webui/webui.go | 6 +++--- pkg/database/stop.go | 8 ++++---- pkg/navitia_api_client/client.go | 4 ++-- pkg/navitia_api_client/departures.go | 8 ++++---- pkg/navitia_api_client/stops.go | 8 ++++---- pkg/navitia_api_client/stops_test.go | 16 ++++++++-------- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/internal/webui/webui.go b/internal/webui/webui.go index fa59f68..b18bc5c 100644 --- a/internal/webui/webui.go +++ b/internal/webui/webui.go @@ -21,9 +21,9 @@ func Run(c *config.Config, dbEnv *database.DBEnv) { if i, err := dbEnv.CountStops(); err == nil && i == 0 { log.Printf("No trains stops data found, updating...") - if trainStops, err := e.navitia.GetStops(); err == nil { - log.Printf("Updated trains stops data from navitia api, got %d results", len(trainStops)) - if err = dbEnv.ReplaceAndImportStops(trainStops); err != nil { + if stops, err := e.navitia.GetStops(); err == nil { + log.Printf("Updated trains stops data from navitia api, got %d results", len(stops)) + if err = dbEnv.ReplaceAndImportStops(stops); err != nil { if dberr, ok := err.(*database.QueryError); ok { log.Printf("%+v", dberr.Unwrap()) } diff --git a/pkg/database/stop.go b/pkg/database/stop.go index 0c2547c..8315c6d 100644 --- a/pkg/database/stop.go +++ b/pkg/database/stop.go @@ -45,7 +45,7 @@ func (env *DBEnv) GetStops() (stops []model.Stop, err error) { return } -func (env *DBEnv) ReplaceAndImportStops(trainStops []model.Stop) error { +func (env *DBEnv) ReplaceAndImportStops(stops []model.Stop) error { pre_query := `DELETE FROM stops;` query := ` INSERT INTO stops @@ -61,11 +61,11 @@ func (env *DBEnv) ReplaceAndImportStops(trainStops []model.Stop) error { tx.Rollback() return newQueryError("Could not run database query: most likely the schema is corrupted", err) } - for i := 0; i < len(trainStops); i++ { + for i := 0; i < len(stops); i++ { _, err = tx.Exec( query, - trainStops[i].Id, - trainStops[i].Name, + stops[i].Id, + stops[i].Name, ) if err != nil { tx.Rollback() diff --git a/pkg/navitia_api_client/client.go b/pkg/navitia_api_client/client.go index 2ca0e5d..bd36c6a 100644 --- a/pkg/navitia_api_client/client.go +++ b/pkg/navitia_api_client/client.go @@ -10,8 +10,8 @@ import ( ) type Client interface { - GetDepartures(trainStop string) (departures []model.Departure, err error) - GetStops() (trainStops []model.Stop, err error) + GetDepartures(stop string) (departures []model.Departure, err error) + GetStops() (stops []model.Stop, err error) } type NavitiaClient struct { diff --git a/pkg/navitia_api_client/departures.go b/pkg/navitia_api_client/departures.go index 1932767..969a88e 100644 --- a/pkg/navitia_api_client/departures.go +++ b/pkg/navitia_api_client/departures.go @@ -45,8 +45,8 @@ type DeparturesResponse struct { } `json:"context"` } -func (c *NavitiaClient) GetDepartures(trainStop string) (departures []model.Departure, err error) { - request := fmt.Sprintf("%s/coverage/sncf/stop_areas/%s/departures", c.baseURL, trainStop) +func (c *NavitiaClient) GetDepartures(stop string) (departures []model.Departure, err error) { + request := fmt.Sprintf("%s/coverage/sncf/stop_areas/%s/departures", c.baseURL, stop) start := time.Now() c.mutex.Lock() defer c.mutex.Unlock() @@ -67,7 +67,7 @@ func (c *NavitiaClient) GetDepartures(trainStop string) (departures []model.Depa if resp.StatusCode == http.StatusOK { var data DeparturesResponse if err = json.NewDecoder(resp.Body).Decode(&data); err != nil { - return nil, newJsonDecodeError("GetDepartures "+trainStop, err) + return nil, newJsonDecodeError("GetDepartures "+stop, err) } // TODO test for no json error // TODO handle pagination @@ -83,7 +83,7 @@ func (c *NavitiaClient) GetDepartures(trainStop string) (departures []model.Depa result: departures, } } else { - err = newApiError(resp.StatusCode, "GetDepartures "+trainStop) + err = newApiError(resp.StatusCode, "GetDepartures "+stop) } return } diff --git a/pkg/navitia_api_client/stops.go b/pkg/navitia_api_client/stops.go index f0cbcf8..2c424b3 100644 --- a/pkg/navitia_api_client/stops.go +++ b/pkg/navitia_api_client/stops.go @@ -31,11 +31,11 @@ type StopsResponse struct { Context interface{} `json:"context"` } -func (c *NavitiaClient) GetStops() (trainStops []model.Stop, err error) { +func (c *NavitiaClient) GetStops() (stops []model.Stop, err error) { return getStopsPage(c, 0) } -func getStopsPage(c *NavitiaClient, i int) (trainStops []model.Stop, err error) { +func getStopsPage(c *NavitiaClient, i int) (stops []model.Stop, err error) { request := fmt.Sprintf("%s/coverage/sncf/stop_areas?count=1000&start_page=%d", c.baseURL, i) req, err := http.NewRequest("GET", request, nil) if err != nil { @@ -53,7 +53,7 @@ func getStopsPage(c *NavitiaClient, i int) (trainStops []model.Stop, err error) } for i := 0; i < len(data.StopAreas); i++ { if data.StopAreas[i].Label != "" { - trainStops = append(trainStops, model.Stop{data.StopAreas[i].ID, data.StopAreas[i].Label}) + stops = append(stops, model.Stop{data.StopAreas[i].ID, data.StopAreas[i].Label}) } } if data.Pagination.ItemsOnPage+data.Pagination.ItemsPerPage*data.Pagination.StartPage < data.Pagination.TotalResult { @@ -61,7 +61,7 @@ func getStopsPage(c *NavitiaClient, i int) (trainStops []model.Stop, err error) if err != nil { return nil, err } - trainStops = append(trainStops, tss...) + stops = append(stops, tss...) } } else { err = newApiError(resp.StatusCode, "GetStops") diff --git a/pkg/navitia_api_client/stops_test.go b/pkg/navitia_api_client/stops_test.go index 0b94765..c3a91f2 100644 --- a/pkg/navitia_api_client/stops_test.go +++ b/pkg/navitia_api_client/stops_test.go @@ -72,13 +72,13 @@ func TestGetStops(t *testing.T) { // normal working request client, ts = newTestClientFromFilename(t, "test_data/4-train-stops.json") defer ts.Close() - trainStops, err := client.GetStops() + stops, err := client.GetStops() if err != nil { t.Fatalf("could not get train stops : %s", err) } // 4 records but one is empty (navitia api quirk) - if len(trainStops) != 3 { - t.Fatalf("did not decode train stops properly, got %d train stops when expected 4", len(trainStops)) + if len(stops) != 3 { + t.Fatalf("did not decode train stops properly, got %d train stops when expected 4", len(stops)) } // normal request in multiple pages client, ts = newTestClientFromFilenames(t, []testClientCase{ @@ -87,13 +87,13 @@ func TestGetStops(t *testing.T) { testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=2", "test_data/4-train-stops-page-2.json"}, }) defer ts.Close() - trainStops, err = client.GetStops() + stops, err = client.GetStops() if err != nil { t.Fatalf("could not get train stops : %+v", err) } // 12 records but one is empty (navitia api quirk) - if len(trainStops) != 11 { - t.Fatalf("did not decode train stops properly, got %d train stops when expected 4", len(trainStops)) + if len(stops) != 11 { + t.Fatalf("did not decode train stops properly, got %d train stops when expected 4", len(stops)) } // failing request in multiple pages with last one missing client, ts = newTestClientFromFilenames(t, []testClientCase{ @@ -101,8 +101,8 @@ func TestGetStops(t *testing.T) { testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=1", "test_data/4-train-stops-page-1.json"}, }) defer ts.Close() - trainStops, err = client.GetStops() + stops, err = client.GetStops() if err == nil { - t.Fatalf("should not be able to get train stops : %+v", trainStops) + t.Fatalf("should not be able to get train stops : %+v", stops) } } -- cgit v1.2.3