mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
refactor route fetching logic for single system ID to external function
This commit is contained in:
@@ -9,8 +9,8 @@ export class ApiResponseError extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class ApiBasedRepositoryLoader {
|
export class ApiBasedRepositoryLoader {
|
||||||
readonly supportedSystemIds = ["263"];
|
supportedSystemIds = ["263"];
|
||||||
readonly baseUrl = "https://passiogo.com/mapGetData.php";
|
baseUrl = "https://passiogo.com/mapGetData.php";
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public repository: GetterSetterRepository,
|
public repository: GetterSetterRepository,
|
||||||
@@ -51,43 +51,47 @@ export class ApiBasedRepositoryLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateRouteDataForExistingSystems() {
|
public async fetchAndUpdateRouteDataForExistingSystemsInRepository() {
|
||||||
const systems = await this.repository.getSystems();
|
const systems = await this.repository.getSystems();
|
||||||
await Promise.all(systems.map(async (system) => {
|
await Promise.all(systems.map(async (system) => {
|
||||||
const params = {
|
await this.fetchAndUpdateRouteDataForSystemId(system.id);
|
||||||
getRoutes: "2",
|
|
||||||
};
|
|
||||||
|
|
||||||
const formDataJsonObject = {
|
|
||||||
"systemSelected0": system.id,
|
|
||||||
"amount": "1",
|
|
||||||
}
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.set("json", JSON.stringify(formDataJsonObject));
|
|
||||||
|
|
||||||
const query = new URLSearchParams(params).toString();
|
|
||||||
const response = await fetch(`${this.baseUrl}?${query}`, {
|
|
||||||
method: "POST",
|
|
||||||
body: formData,
|
|
||||||
});
|
|
||||||
const json = await response.json();
|
|
||||||
|
|
||||||
if (typeof json.all === "object") {
|
|
||||||
await Promise.all(json.all.map(async (jsonRoute: any) => {
|
|
||||||
const constructedRoute: IRoute = {
|
|
||||||
name: jsonRoute.name,
|
|
||||||
color: jsonRoute.color,
|
|
||||||
id: jsonRoute.myid,
|
|
||||||
polylineCoordinates: [],
|
|
||||||
systemId: system.id,
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.repository.addOrUpdateRoute(constructedRoute);
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async fetchAndUpdateRouteDataForSystemId(systemId: string) {
|
||||||
|
const params = {
|
||||||
|
getRoutes: "2",
|
||||||
|
};
|
||||||
|
|
||||||
|
const formDataJsonObject = {
|
||||||
|
"systemSelected0": systemId,
|
||||||
|
"amount": "1",
|
||||||
|
}
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.set("json", JSON.stringify(formDataJsonObject));
|
||||||
|
|
||||||
|
const query = new URLSearchParams(params).toString();
|
||||||
|
const response = await fetch(`${this.baseUrl}?${query}`, {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
const json = await response.json();
|
||||||
|
|
||||||
|
if (typeof json.all === "object") {
|
||||||
|
await Promise.all(json.all.map(async (jsonRoute: any) => {
|
||||||
|
const constructedRoute: IRoute = {
|
||||||
|
name: jsonRoute.name,
|
||||||
|
color: jsonRoute.color,
|
||||||
|
id: jsonRoute.myid,
|
||||||
|
polylineCoordinates: [],
|
||||||
|
systemId: systemId,
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.repository.addOrUpdateRoute(constructedRoute);
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystems() {
|
public async fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystems() {
|
||||||
// Fetch from the API
|
// Fetch from the API
|
||||||
// Pass JSON output into two different methods to update repository
|
// Pass JSON output into two different methods to update repository
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ export class TimedApiBasedRepositoryLoader extends ApiBasedRepositoryLoader {
|
|||||||
await this.repository.clearSystemData();
|
await this.repository.clearSystemData();
|
||||||
await this.fetchAndUpdateSystemData();
|
await this.fetchAndUpdateSystemData();
|
||||||
await this.repository.clearRouteData();
|
await this.repository.clearRouteData();
|
||||||
await this.fetchAndUpdateRouteDataForExistingSystems();
|
await this.fetchAndUpdateRouteDataForExistingSystemsInRepository();
|
||||||
await this.repository.clearStopData();
|
await this.repository.clearStopData();
|
||||||
await this.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystems();
|
await this.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystems();
|
||||||
await this.repository.clearShuttleData();
|
await this.repository.clearShuttleData();
|
||||||
|
|||||||
Reference in New Issue
Block a user