From eef414cdb7f71e969767d90655f552c576a80517 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 8 Jan 2025 16:49:00 -0800 Subject: [PATCH] implemnet getStopById method --- src/repositories/ApiBasedRepository.ts | 27 ++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/repositories/ApiBasedRepository.ts b/src/repositories/ApiBasedRepository.ts index b7d5cc0..7a3f423 100644 --- a/src/repositories/ApiBasedRepository.ts +++ b/src/repositories/ApiBasedRepository.ts @@ -3,6 +3,10 @@ import { IEta, IOrderedStop, IRoute, IShuttle, IStop, ISystem } from "../entitie const baseUrl = "https://passiogo.com/mapGetData.php" +// TODO: add TTL values to everything +// TODO: remove RepositoryDataLoader and UnoptimizedInMemoryRepository +// TODO: make milleseconds (TTL) required on everything + export interface ApiBasedRepositoryCache { etasForShuttleId?: { [shuttleId: string]: IEta[], @@ -13,6 +17,9 @@ export interface ApiBasedRepositoryCache { stopsBySystemId?: { [systemId: string]: IStop[], }, + stopByStopId?: { + [stopId: string]: IStop, + }, shuttleByShuttleId?: { [shuttleId: string]: IShuttle, }, @@ -115,6 +122,7 @@ export class ApiBasedRepository implements GetterRepository { } public async updateEtasForSystemIfTTL(systemId: string) { + // TODO: check if TTL try { const stops = await this.getStopsBySystemId(systemId); await Promise.all(stops.map(async (stop) => { @@ -224,6 +232,7 @@ export class ApiBasedRepository implements GetterRepository { } public async updateShuttlesForSystemIfTTL(systemId: string) { + // TODO: check if TTL try { // TODO: update shuttlesByRouteId // Update shuttleByShuttleId, shuttlesBySystemId @@ -285,12 +294,20 @@ ${json}`); } public async getStopById(stopId: string): Promise { - // TODO: implement - return null; + if (!this.cache.stopByStopId) return null; + const oldStop = this.cache.stopByStopId[stopId]; + if (!oldStop) return null; + + await this.updateStopsForSystemIdIfTTL(oldStop.systemId); + + const newStop = this.cache.stopByStopId[stopId]; + if (!newStop) return null; + + return newStop; } public async getStopsBySystemId(systemId: string): Promise { - await this.updateStopsForSystemId(systemId); + await this.updateStopsForSystemIdIfTTL(systemId); if (!this.cache.stopsBySystemId || this.cache.stopsBySystemId[systemId]) { return []; @@ -298,7 +315,9 @@ ${json}`); return this.cache.stopsBySystemId[systemId]; } - public async updateStopsForSystemId(systemId: string) { + public async updateStopsForSystemIdIfTTL(systemId: string) { + // TODO: check if TTL + try { const params = { getStops: "2",