use simplified implementation without system loading

This commit is contained in:
2025-04-06 11:21:07 -07:00
parent 95eb2c8f65
commit 8c2fb3a52a
2 changed files with 21 additions and 178 deletions

View File

@@ -1,5 +1,5 @@
import { ShuttleGetterSetterRepository } from "../repositories/ShuttleGetterSetterRepository";
import { IEntityWithId, IEta, IRoute, IShuttle, IStop, IPassioSystem } from "../entities/entities";
import { IEntityWithId, IEta, IRoute, IShuttle, IStop } from "../entities/entities";
import { ShuttleRepositoryLoader } from "./ShuttleRepositoryLoader";
export class ApiResponseError extends Error {
@@ -32,49 +32,8 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
return ids;
}
public async fetchAndUpdateSystemData() {
const params = {
getSystems: "2",
};
const query = new URLSearchParams(params).toString();
const system = await this.repository.getSystemIfExists();
try {
const response = await fetch(`${this.baseUrl}?${query}`);
const json = await response.json();
if (!response.ok) {
throw new Error(`HTTP error with status ${response.status}`)
}
if (typeof json.all === "object") {
// filter down to supported systems
const filteredSystem = json.all.find((jsonSystem: any) => jsonSystem.id === this.systemId);
if (filteredSystem !== undefined) {
const constructedSystem: IPassioSystem = {
id: filteredSystem.id,
name: filteredSystem.fullname,
};
await this.repository.updateSystem(constructedSystem);
}
} else {
throw new Error("Received JSON object does not contain `all` field")
}
} catch(e: any) {
throw new ApiResponseError(e.message);
}
}
public async fetchAndUpdateRouteDataForExistingSystemInRepository() {
const system = await this.repository.getSystemIfExists();
if (system !== null) {
await this.fetchAndUpdateRouteDataForSystemId(system.id);
}
}
public async fetchAndUpdateRouteDataForSystemId(systemId: string) {
public async fetchAndUpdateRouteDataForSystem() {
const systemId = this.systemId;
const routeIdsToPrune = await this.constructExistingEntityIdSet(async () => {
return await this.repository.getRoutesBySystemId(systemId);
});
@@ -123,14 +82,9 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
}
}
public async fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemInRepository() {
const system = await this.repository.getSystemIfExists();
if (system !== null) {
await this.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(system.id);
}
}
public async fetchAndUpdateStopAndPolylineDataForRoutesInSystem() {
const systemId = this.systemId;
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 () => {
@@ -169,16 +123,8 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
}
}
public async fetchAndUpdateShuttleDataForExistingSystemInRepository() {
const system = await this.repository.getSystemIfExists();
if (system !== null) {
const systemId = system.id;
await this.fetchAndUpdateShuttleDataForSystemId(systemId);
}
}
public async fetchAndUpdateShuttleDataForSystemId(systemId: string) {
public async fetchAndUpdateShuttleDataForSystem() {
const systemId = this.systemId;
const shuttleIdsToPrune = await this.constructExistingEntityIdSet(async () => {
return await this.repository.getShuttlesBySystemId(systemId);
});
@@ -236,16 +182,8 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
}
}
public async fetchAndUpdateEtaDataForExistingStopsForSystemInRepository() {
const system = await this.repository.getSystemIfExists()
if (system !== null) {
const systemId = system.id;
await this.fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId);
}
}
public async fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId: string) {
const stops = await this.repository.getStopsBySystemId(systemId);
public async fetchAndUpdateEtaDataForExistingStopsForSystem() {
const stops = await this.repository.getStopsBySystemId(this.systemId);
await Promise.all(stops.map(async (stop) => {
let stopId = stop.id;
await this.fetchAndUpdateEtaDataForStopId(stopId);