mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
implement method to retrieve parking structures
This commit is contained in:
@@ -1,16 +1,59 @@
|
||||
import { ParkingRepositoryLoader } from "./ParkingRepositoryLoader";
|
||||
import { ParkingGetterSetterRepository } from "../../repositories/ParkingGetterSetterRepository";
|
||||
import { createHash } from "node:crypto";
|
||||
import { ApiResponseError } from "../ApiResponseError";
|
||||
import { IParkingStructure } from "../../entities/ParkingRepositoryEntities";
|
||||
|
||||
class ApiParseError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "ApiParseError";
|
||||
}
|
||||
}
|
||||
|
||||
export class ChapmanTimedApiBasedParkingRepositoryLoader implements ParkingRepositoryLoader {
|
||||
public static readonly id = "chapman-parking-loader";
|
||||
private readonly fetchUrl = "https://webfarm.chapman.edu/ParkingService/ParkingService/counts";
|
||||
|
||||
constructor(
|
||||
public repository: ParkingGetterSetterRepository
|
||||
) {}
|
||||
|
||||
async fetchAndUpdateParkingStructures(): Promise<void> {
|
||||
// TODO
|
||||
let json: any;
|
||||
|
||||
try {
|
||||
const response = await fetch(this.fetchUrl);
|
||||
json = await response.json();
|
||||
} catch(e: any) {
|
||||
throw new ApiResponseError(e.message);
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof json.Structures === "object") {
|
||||
const parkingStructures: IParkingStructure[] = json.Structures.map((jsonStructure: any) => {
|
||||
const structureToReturn: IParkingStructure = {
|
||||
capacity: jsonStructure.Capacity,
|
||||
coordinates: {
|
||||
latitude: jsonStructure.Latitude,
|
||||
longitude: jsonStructure.Longitude,
|
||||
},
|
||||
id: ChapmanTimedApiBasedParkingRepositoryLoader.generateId(jsonStructure.Address),
|
||||
name: jsonStructure.Name,
|
||||
spotsAvailable: jsonStructure.CurrentCount,
|
||||
address: jsonStructure.Address
|
||||
}
|
||||
|
||||
return structureToReturn;
|
||||
});
|
||||
|
||||
await Promise.all(parkingStructures.map(async (structure: IParkingStructure) => {
|
||||
this.repository.addOrUpdateParkingStructure(structure);
|
||||
}));
|
||||
}
|
||||
} catch(e: any) {
|
||||
throw new ApiParseError(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
private static normalizeAddress(address: string): string {
|
||||
|
||||
Reference in New Issue
Block a user