mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
implement pruning code
This commit is contained in:
@@ -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<T extends IEntityWithId>(entitySearchCallback: () => Promise<T[]>) {
|
||||
const existingEntities = await entitySearchCallback();
|
||||
const ids = new Set<string>();
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user