mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
208 lines
6.5 KiB
TypeScript
208 lines
6.5 KiB
TypeScript
import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository";
|
|
import { IEta, IOrderedStop, IRoute, IShuttle, IStop } from "../entities/ShuttleRepositoryEntities";
|
|
import { IEntityWithId } from "../entities/SharedEntities";
|
|
|
|
/**
|
|
* An unoptimized in memory repository.
|
|
* (I would optimize it with actual data structures, but I'm
|
|
* switching to another data store later anyways)
|
|
*/
|
|
export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetterRepository {
|
|
private stops: IStop[] = [];
|
|
private routes: IRoute[] = [];
|
|
private shuttles: IShuttle[] = [];
|
|
private etas: IEta[] = [];
|
|
private orderedStops: IOrderedStop[] = [];
|
|
|
|
private subscribers: ((eta: IEta) => void)[] = [];
|
|
|
|
public async getStops(): Promise<IStop[]> {
|
|
return this.stops;
|
|
}
|
|
|
|
public async getStopById(stopId: string) {
|
|
return this.findEntityById(stopId, this.stops);
|
|
}
|
|
|
|
public async getRoutes(): Promise<IRoute[]> {
|
|
return this.routes;
|
|
}
|
|
|
|
public async getRouteById(routeId: string) {
|
|
return this.findEntityById(routeId, this.routes);
|
|
}
|
|
|
|
public async getShuttles(): Promise<IShuttle[]> {
|
|
return this.shuttles;
|
|
}
|
|
|
|
public async getShuttlesByRouteId(routeId: string) {
|
|
return this.shuttles.filter(shuttle => shuttle.routeId === routeId);
|
|
}
|
|
|
|
public async getShuttleById(shuttleId: string) {
|
|
return this.findEntityById(shuttleId, this.shuttles);
|
|
}
|
|
|
|
public async getEtasForShuttleId(shuttleId: string): Promise<IEta[]> {
|
|
return this.etas.filter(eta => eta.shuttleId === shuttleId);
|
|
}
|
|
|
|
public async getEtasForStopId(stopId: string) {
|
|
return this.etas.filter(eta => eta.stopId === stopId);
|
|
}
|
|
|
|
public subscribeToEtaUpdates(listener: (eta: IEta) => void) {
|
|
this.subscribers.push(listener);
|
|
}
|
|
|
|
public unsubscribeFromEtaUpdates(listener: (eta: IEta) => void) {
|
|
const index = this.subscribers.findIndex((existingListener) => existingListener == listener);
|
|
if (index >= 0) {
|
|
this.subscribers.splice(index, 1);
|
|
}
|
|
}
|
|
|
|
public async getEtaForShuttleAndStopId(shuttleId: string, stopId: string) {
|
|
return this.findEntityByMatcher<IEta>((value) => value.stopId === stopId && value.shuttleId === shuttleId, this.etas);
|
|
}
|
|
|
|
public async getOrderedStopByRouteAndStopId(routeId: string, stopId: string) {
|
|
return this.findEntityByMatcher<IOrderedStop>((value) => value.routeId === routeId && value.stopId === stopId, this.orderedStops)
|
|
}
|
|
|
|
public async getOrderedStopsByStopId(stopId: string) {
|
|
return this.orderedStops.filter((value) => value.stopId === stopId);
|
|
}
|
|
|
|
public async getOrderedStopsByRouteId(routeId: string) {
|
|
return this.orderedStops.filter((value) => value.routeId === routeId);
|
|
}
|
|
|
|
private findEntityById<T extends IEntityWithId>(entityId: string, arrayToSearchIn: T[]) {
|
|
return this.findEntityByMatcher((value) => value.id === entityId, arrayToSearchIn);
|
|
}
|
|
|
|
private findEntityByMatcher<T>(callback: (value: T) => boolean, arrayToSearchIn: T[]): T | null {
|
|
const entity = arrayToSearchIn.find(callback);
|
|
if (!entity) {
|
|
return null;
|
|
}
|
|
return entity;
|
|
}
|
|
|
|
public async addOrUpdateRoute(route: IRoute): Promise<void> {
|
|
const index = this.routes.findIndex((r) => r.id === route.id);
|
|
if (index !== -1) {
|
|
this.routes[index] = route;
|
|
} else {
|
|
this.routes.push(route);
|
|
}
|
|
}
|
|
|
|
public async addOrUpdateShuttle(shuttle: IShuttle): Promise<void> {
|
|
const index = this.shuttles.findIndex((s) => s.id === shuttle.id);
|
|
if (index !== -1) {
|
|
this.shuttles[index] = shuttle;
|
|
} else {
|
|
this.shuttles.push(shuttle);
|
|
}
|
|
}
|
|
|
|
public async addOrUpdateStop(stop: IStop): Promise<void> {
|
|
const index = this.stops.findIndex((s) => s.id === stop.id);
|
|
if (index !== -1) {
|
|
this.stops[index] = stop;
|
|
} else {
|
|
this.stops.push(stop);
|
|
}
|
|
}
|
|
|
|
public async addOrUpdateOrderedStop(orderedStop: IOrderedStop): Promise<void> {
|
|
const index = this.orderedStops.findIndex((value) => value.stopId === orderedStop.stopId && value.routeId === orderedStop.routeId);
|
|
if (index !== -1) {
|
|
this.orderedStops[index] = orderedStop;
|
|
} else {
|
|
this.orderedStops.push(orderedStop);
|
|
}
|
|
}
|
|
|
|
public async addOrUpdateEta(eta: IEta): Promise<void> {
|
|
const index = this.etas.findIndex((e) => e.stopId === eta.stopId && e.shuttleId === eta.shuttleId);
|
|
if (index !== -1) {
|
|
this.etas[index] = eta;
|
|
} else {
|
|
this.etas.push(eta);
|
|
}
|
|
this.publishEtaUpdateToSubscribers(eta);
|
|
}
|
|
|
|
private publishEtaUpdateToSubscribers(eta: IEta) {
|
|
this.subscribers.forEach(subscriber => {
|
|
subscriber(eta);
|
|
});
|
|
}
|
|
|
|
private async removeEntityByMatcherIfExists<T>(callback: (value: T) => boolean, arrayToSearchIn: T[]) {
|
|
const index = arrayToSearchIn.findIndex(callback);
|
|
if (index > -1) {
|
|
const entityToReturn = arrayToSearchIn[index];
|
|
arrayToSearchIn.splice(index, 1);
|
|
return entityToReturn;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private async removeEntityByIdIfExists<T extends IEntityWithId>(entityId: string, arrayToSearchIn: T[]) {
|
|
return await this.removeEntityByMatcherIfExists((value) => value.id === entityId, arrayToSearchIn);
|
|
}
|
|
|
|
public async removeRouteIfExists(routeId: string): Promise<IRoute | null> {
|
|
return await this.removeEntityByIdIfExists(routeId, this.routes);
|
|
}
|
|
|
|
public async removeShuttleIfExists(shuttleId: string): Promise<IShuttle | null> {
|
|
return await this.removeEntityByIdIfExists(shuttleId, this.shuttles);
|
|
}
|
|
|
|
public async removeStopIfExists(stopId: string): Promise<IStop | null> {
|
|
return await this.removeEntityByIdIfExists(stopId, this.stops);
|
|
}
|
|
|
|
public async removeOrderedStopIfExists(stopId: string, routeId: string): Promise<IOrderedStop | null> {
|
|
return await this.removeEntityByMatcherIfExists((orderedStop) => {
|
|
return orderedStop.stopId === stopId
|
|
&& orderedStop.routeId === routeId
|
|
}, this.orderedStops);
|
|
}
|
|
|
|
public async removeEtaIfExists(shuttleId: string, stopId: string): Promise<IEta | null> {
|
|
return await this.removeEntityByMatcherIfExists((eta) => {
|
|
return eta.stopId === stopId
|
|
&& eta.shuttleId === shuttleId
|
|
}, this.etas);
|
|
}
|
|
|
|
public async clearShuttleData(): Promise<void> {
|
|
this.shuttles = [];
|
|
}
|
|
|
|
public async clearEtaData(): Promise<void> {
|
|
this.etas = [];
|
|
}
|
|
|
|
public async clearOrderedStopData(): Promise<void> {
|
|
this.orderedStops = [];
|
|
}
|
|
|
|
public async clearRouteData(): Promise<void> {
|
|
this.routes = [];
|
|
}
|
|
|
|
public async clearStopData(): Promise<void> {
|
|
this.stops = [];
|
|
}
|
|
|
|
}
|