implement rest of methods

This commit is contained in:
2025-01-22 20:41:24 -08:00
parent e9703e53ae
commit be2efe2353

View File

@@ -86,10 +86,6 @@ export class UnoptimizedInMemoryRepository implements GetterSetterRepository {
return entity; return entity;
} }
private findEntityIndexById<T extends IEntityWithId>(entityId: string, arrayToSearchIn: T[]) {
return arrayToSearchIn.findIndex((value) => value.id === entityId);
}
public async addOrUpdateSystem(system: ISystem): Promise<void> { public async addOrUpdateSystem(system: ISystem): Promise<void> {
const index = this.systems.findIndex((s) => s.id === system.id); const index = this.systems.findIndex((s) => s.id === system.id);
if (index !== -1) { if (index !== -1) {
@@ -144,42 +140,49 @@ export class UnoptimizedInMemoryRepository implements GetterSetterRepository {
} }
} }
public async removeSystemIfExists(systemId: string): Promise<ISystem | null> { private async removeEntityByMatcherIfExists<T>(callback: (value: T) => boolean, arrayToSearchIn: T[]) {
const index = this.findEntityIndexById(systemId, this.systems); const index = arrayToSearchIn.findIndex(callback);
if (index > -1) { if (index > -1) {
const systemToReturn = this.systems[index]; const entityToReturn = arrayToSearchIn[index];
this.systems.splice(index, 1); arrayToSearchIn.splice(index, 1);
return systemToReturn; return entityToReturn;
} }
return null; return null;
} }
private async removeEntityByIdIfExists<T extends IEntityWithId>(entityId: string, arrayToSearchIn: T[]) {
return await this.removeEntityByMatcherIfExists((value) => value.id === entityId, arrayToSearchIn);
}
public async removeSystemIfExists(systemId: string): Promise<ISystem | null> {
return await this.removeEntityByIdIfExists(systemId, this.systems);
}
public async removeRouteIfExists(routeId: string): Promise<IRoute | null> { public async removeRouteIfExists(routeId: string): Promise<IRoute | null> {
const index = this.findEntityIndexById(routeId, this.routes); return await this.removeEntityByIdIfExists(routeId, this.routes);
if (index > -1) {
const routeToReturn = this.routes[index];
this.routes.splice(index, 1);
return routeToReturn;
}
return null;
} }
public async removeShuttleIfExists(shuttleId: string): Promise<IShuttle | null> { public async removeShuttleIfExists(shuttleId: string): Promise<IShuttle | null> {
return null; return await this.removeEntityByIdIfExists(shuttleId, this.shuttles);
} }
public async removeStopIfExists(stopId: string): Promise<IStop | null> { public async removeStopIfExists(stopId: string): Promise<IStop | null> {
return null; return await this.removeEntityByIdIfExists(stopId, this.stops);
} }
public async removeOrderedStopIfExists(stopId: string, routeId: string): Promise<IOrderedStop | null> { public async removeOrderedStopIfExists(stopId: string, routeId: string): Promise<IOrderedStop | null> {
return 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> { public async removeEtaIfExists(shuttleId: string, stopId: string): Promise<IEta | null> {
return null; return await this.removeEntityByMatcherIfExists((eta) => {
return eta.stopId === stopId
&& eta.shuttleId === shuttleId
}, this.etas);
} }
public async clearSystemData() { public async clearSystemData() {