From be2efe23536a57369bf349ff031c6cfc506c99d6 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 22 Jan 2025 20:41:24 -0800 Subject: [PATCH] implement rest of methods --- .../UnoptimizedInMemoryRepository.ts | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/repositories/UnoptimizedInMemoryRepository.ts b/src/repositories/UnoptimizedInMemoryRepository.ts index eb0385b..938f4ea 100644 --- a/src/repositories/UnoptimizedInMemoryRepository.ts +++ b/src/repositories/UnoptimizedInMemoryRepository.ts @@ -86,10 +86,6 @@ export class UnoptimizedInMemoryRepository implements GetterSetterRepository { return entity; } - private findEntityIndexById(entityId: string, arrayToSearchIn: T[]) { - return arrayToSearchIn.findIndex((value) => value.id === entityId); - } - public async addOrUpdateSystem(system: ISystem): Promise { const index = this.systems.findIndex((s) => s.id === system.id); if (index !== -1) { @@ -144,42 +140,49 @@ export class UnoptimizedInMemoryRepository implements GetterSetterRepository { } } - public async removeSystemIfExists(systemId: string): Promise { - const index = this.findEntityIndexById(systemId, this.systems); + private async removeEntityByMatcherIfExists(callback: (value: T) => boolean, arrayToSearchIn: T[]) { + const index = arrayToSearchIn.findIndex(callback); if (index > -1) { - const systemToReturn = this.systems[index]; - this.systems.splice(index, 1); - return systemToReturn; + const entityToReturn = arrayToSearchIn[index]; + arrayToSearchIn.splice(index, 1); + return entityToReturn; } return null; } + private async removeEntityByIdIfExists(entityId: string, arrayToSearchIn: T[]) { + return await this.removeEntityByMatcherIfExists((value) => value.id === entityId, arrayToSearchIn); + } + + public async removeSystemIfExists(systemId: string): Promise { + return await this.removeEntityByIdIfExists(systemId, this.systems); + } + public async removeRouteIfExists(routeId: string): Promise { - const index = this.findEntityIndexById(routeId, this.routes); - if (index > -1) { - const routeToReturn = this.routes[index]; - this.routes.splice(index, 1); - return routeToReturn; - } - - return null; + return await this.removeEntityByIdIfExists(routeId, this.routes); } public async removeShuttleIfExists(shuttleId: string): Promise { - return null; + return await this.removeEntityByIdIfExists(shuttleId, this.shuttles); } public async removeStopIfExists(stopId: string): Promise { - return null; + return await this.removeEntityByIdIfExists(stopId, this.stops); } public async removeOrderedStopIfExists(stopId: string, routeId: string): Promise { - return null; + return await this.removeEntityByMatcherIfExists((orderedStop) => { + return orderedStop.stopId === stopId + && orderedStop.routeId === routeId + }, this.orderedStops); } public async removeEtaIfExists(shuttleId: string, stopId: string): Promise { - return null; + return await this.removeEntityByMatcherIfExists((eta) => { + return eta.stopId === stopId + && eta.shuttleId === shuttleId + }, this.etas); } public async clearSystemData() {