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