From b2be3ae583a29f499a65e95201159740b8b0592e Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 22 Jan 2025 14:55:51 -0800 Subject: [PATCH] refactor fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId into separate method --- src/loaders/ApiBasedRepositoryLoader.ts | 50 +++++++++++++------------ 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/loaders/ApiBasedRepositoryLoader.ts b/src/loaders/ApiBasedRepositoryLoader.ts index d93000a..031d30b 100644 --- a/src/loaders/ApiBasedRepositoryLoader.ts +++ b/src/loaders/ApiBasedRepositoryLoader.ts @@ -103,30 +103,34 @@ export class ApiBasedRepositoryLoader { // Pass JSON output into two different methods to update repository const systems = await this.repository.getSystems(); await Promise.all(systems.map(async (system: ISystem) => { - const params = { - getStops: "2", - }; - - const formDataJsonObject = { - "s0": system.id, - "sA": 1 - }; - const formData = new FormData(); - formData.set("json", JSON.stringify(formDataJsonObject)); - - const query = new URLSearchParams(params).toString(); - const response = await fetch(`${this.baseUrl}?${query}`, { - method: "POST", - body: formData, - }); - const json = await response.json(); - - await this.updateStopDataForSystemAndApiResponse(system, json); - await this.updateOrderedStopDataForExistingStops(json); - await this.updatePolylineDataForExistingRoutesAndApiResponse(json); + await this.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(system.id); })); } + public async fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(systemId: string) { + const params = { + getStops: "2", + }; + + const formDataJsonObject = { + "s0": systemId, + "sA": 1 + }; + const formData = new FormData(); + formData.set("json", JSON.stringify(formDataJsonObject)); + + const query = new URLSearchParams(params).toString(); + const response = await fetch(`${this.baseUrl}?${query}`, { + method: "POST", + body: formData, + }); + const json = await response.json(); + + await this.updateStopDataForSystemAndApiResponse(systemId, json); + await this.updateOrderedStopDataForExistingStops(json); + await this.updatePolylineDataForExistingRoutesAndApiResponse(json); + } + public async fetchAndUpdateShuttleDataForExistingSystems() { const systems = await this.repository.getSystems(); await Promise.all(systems.map(async (system: ISystem) => { @@ -208,7 +212,7 @@ export class ApiBasedRepositoryLoader { })) } - protected async updateStopDataForSystemAndApiResponse(system: ISystem, json: any) { + protected async updateStopDataForSystemAndApiResponse(systemId: string, json: any) { if (json.stops) { const jsonStops = Object.values(json.stops); @@ -216,7 +220,7 @@ export class ApiBasedRepositoryLoader { const constructedStop: IStop = { name: stop.name, id: stop.id, - systemId: system.id, + systemId, coordinates: { latitude: parseFloat(stop.latitude), longitude: parseFloat(stop.longitude),