diff --git a/src/loaders/ApiBasedRepositoryLoader.ts b/src/loaders/ApiBasedRepositoryLoader.ts index de48179..f60bd0a 100644 --- a/src/loaders/ApiBasedRepositoryLoader.ts +++ b/src/loaders/ApiBasedRepositoryLoader.ts @@ -1,5 +1,5 @@ import { GetterSetterRepository } from "../repositories/GetterSetterRepository"; -import { IEta, IRoute, IShuttle, IStop, ISystem } from "../entities/entities"; +import { IEntityWithId, IEta, IRoute, IShuttle, IStop, ISystem } from "../entities/entities"; export class ApiResponseError extends Error { constructor(message: string) { @@ -17,12 +17,25 @@ export class ApiBasedRepositoryLoader { ) { } + private async constructExistingEntityIdSet(entitySearchCallback: () => Promise) { + const existingEntities = await entitySearchCallback(); + const ids = new Set(); + existingEntities.forEach((entity) => { + ids.add(entity.id); + }); + return ids; + } + public async fetchAndUpdateSystemData() { const params = { getSystems: "2", }; const query = new URLSearchParams(params).toString(); + const systemIds = await this.constructExistingEntityIdSet(async () => { + return await this.repository.getSystems(); + }) + try { const response = await fetch(`${this.baseUrl}?${query}`); const json = await response.json(); @@ -41,10 +54,18 @@ export class ApiBasedRepositoryLoader { }; await this.repository.addOrUpdateSystem(constructedSystem); + if (systemIds.has(constructedSystem.id)) { + systemIds.delete(constructedSystem.id); + } })); } else { throw new Error("Received JSON object does not contain `all` field") } + + // Prune systems + await Promise.all(Array.from(systemIds).map(async (systemId) => { + await this.repository.removeSystemIfExists(systemId); + })); } catch(e: any) { throw new ApiResponseError(e.message); }