From d8119b750640ba8ed571a0a7890fd2c37a511e09 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 22 Jan 2025 21:36:21 -0800 Subject: [PATCH] add implementations for data types with id --- src/loaders/ApiBasedRepositoryLoader.ts | 42 ++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/loaders/ApiBasedRepositoryLoader.ts b/src/loaders/ApiBasedRepositoryLoader.ts index f60bd0a..f863cf6 100644 --- a/src/loaders/ApiBasedRepositoryLoader.ts +++ b/src/loaders/ApiBasedRepositoryLoader.ts @@ -54,9 +54,7 @@ export class ApiBasedRepositoryLoader { }; await this.repository.addOrUpdateSystem(constructedSystem); - if (systemIds.has(constructedSystem.id)) { - systemIds.delete(constructedSystem.id); - } + systemIds.delete(constructedSystem.id); })); } else { throw new Error("Received JSON object does not contain `all` field") @@ -79,6 +77,10 @@ export class ApiBasedRepositoryLoader { } public async fetchAndUpdateRouteDataForSystemId(systemId: string) { + const routeIdsToPrune = await this.constructExistingEntityIdSet(async () => { + return await this.repository.getRoutesBySystemId(systemId); + }); + const params = { getRoutes: "2", }; @@ -110,8 +112,14 @@ export class ApiBasedRepositoryLoader { }; await this.repository.addOrUpdateRoute(constructedRoute); + + routeIdsToPrune.delete(constructedRoute.id); })) } + + await Promise.all(Array.from(routeIdsToPrune).map(async (routeId) => { + await this.repository.removeRouteIfExists(routeId); + })); } catch(e: any) { throw new ApiResponseError(e.message); } @@ -127,6 +135,10 @@ export class ApiBasedRepositoryLoader { public async fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(systemId: string) { // Fetch from the API // Pass JSON output into two different methods to update repository + const stopIdsToPrune = await this.constructExistingEntityIdSet(async () => { + return await this.repository.getStopsBySystemId(systemId); + }); + const params = { getStops: "2", }; @@ -147,9 +159,13 @@ export class ApiBasedRepositoryLoader { }); const json = await response.json(); - await this.updateStopDataForSystemAndApiResponse(systemId, json); + await this.updateStopDataForSystemAndApiResponse(systemId, json, stopIdsToPrune); await this.updateOrderedStopDataForExistingStops(json); await this.updatePolylineDataForExistingRoutesAndApiResponse(json); + + await Promise.all(Array.from(stopIdsToPrune).map(async (stopId) => { + await this.repository.removeStopIfExists(stopId); + })); } catch(e: any) { throw new ApiResponseError(e.message); } @@ -164,6 +180,10 @@ export class ApiBasedRepositoryLoader { } public async fetchAndUpdateShuttleDataForSystemId(systemId: string) { + const shuttleIdsToPrune = await this.constructExistingEntityIdSet(async () => { + return await this.repository.getShuttlesBySystemId(systemId); + }); + const params = { getBuses: "2" }; @@ -203,8 +223,14 @@ export class ApiBasedRepositoryLoader { } await this.repository.addOrUpdateShuttle(constructedShuttle); + + shuttleIdsToPrune.delete(constructedShuttle.id); })); } + + await Promise.all(Array.from(shuttleIdsToPrune).map(async (shuttleId) => { + await this.repository.removeShuttleIfExists(shuttleId); + })); } catch(e: any) { throw new ApiResponseError(e.message); } @@ -261,7 +287,11 @@ export class ApiBasedRepositoryLoader { } } - protected async updateStopDataForSystemAndApiResponse(systemId: string, json: any) { + protected async updateStopDataForSystemAndApiResponse( + systemId: string, + json: any, + setOfIdsToPrune: Set = new Set(), + ) { if (json.stops) { const jsonStops = Object.values(json.stops); @@ -277,6 +307,8 @@ export class ApiBasedRepositoryLoader { }; await this.repository.addOrUpdateStop(constructedStop); + + setOfIdsToPrune.delete(constructedStop.id); })); } }