add implementations for data types with id

This commit is contained in:
2025-01-22 21:36:21 -08:00
parent eb0882b2e5
commit d8119b7506

View File

@@ -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<string> = 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);
}));
}
}