From 88dbce007d741580019abc9c6158842a63c0796e Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 8 Jan 2025 16:19:11 -0800 Subject: [PATCH] implement getStopsBySystemId and updateStopsForSystemId --- src/repositories/ApiBasedRepository.ts | 55 +++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/repositories/ApiBasedRepository.ts b/src/repositories/ApiBasedRepository.ts index 32b317e..3b8100e 100644 --- a/src/repositories/ApiBasedRepository.ts +++ b/src/repositories/ApiBasedRepository.ts @@ -266,7 +266,59 @@ ${json}`); } public async getStopsBySystemId(systemId: string): Promise { - return []; + await this.updateStopsForSystemId(systemId); + + if (!this.cache.stopsBySystemId || this.cache.stopsBySystemId[systemId]) { + return []; + } + return this.cache.stopsBySystemId[systemId]; + } + + public async updateStopsForSystemId(systemId: string) { + try { + 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(`${baseUrl}?${query}`, { + method: "POST", + body: formData, + }); + const json = await response.json(); + + // TODO: update polyline data + // TODO: update ordered stop data + + if (json.stops) { + const jsonStops = Object.values(json.stops); + + await Promise.all(jsonStops.map(async (stop: any) => { + const constructedStop: IStop = { + name: stop.name, + id: stop.id, + systemId: systemId, + coordinates: { + latitude: parseFloat(stop.latitude), + longitude: parseFloat(stop.longitude), + }, + }; + + if (this.cache.stopsBySystemId) { + this.cache.stopsBySystemId[systemId]?.push(constructedStop); + } + })); + } + } catch (e) { + + } } public async getSystemById(systemId: string): Promise { @@ -276,4 +328,5 @@ ${json}`); public async getSystems(): Promise<[]> { return Promise.resolve([]); } + } \ No newline at end of file