From 695fec1fcec3db6690431c065a5d4f77a957c332 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Sun, 6 Apr 2025 09:51:13 -0700 Subject: [PATCH] rename update method and update return types --- src/loaders/ApiBasedShuttleRepositoryLoader.ts | 2 +- src/loaders/loadShuttleTestData.ts | 2 +- src/repositories/ShuttleGetterRepository.ts | 3 +-- .../ShuttleGetterSetterRepository.ts | 3 +-- .../UnoptimizedInMemoryShuttleRepository.ts | 6 +++--- ...piBasedShuttleRepositoryLoaderTests.test.ts | 10 +++++----- ...mizedInMemoryShuttleRepositoryTests.test.ts | 18 +++++++++--------- test/resolvers/QueryResolverTests.test.ts | 2 +- test/resolvers/SystemResolverTests.test.ts | 6 +++--- test/testHelpers/repositorySetupHelpers.ts | 2 +- 10 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/loaders/ApiBasedShuttleRepositoryLoader.ts b/src/loaders/ApiBasedShuttleRepositoryLoader.ts index 30f033e..a7fd0f8 100644 --- a/src/loaders/ApiBasedShuttleRepositoryLoader.ts +++ b/src/loaders/ApiBasedShuttleRepositoryLoader.ts @@ -59,7 +59,7 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader name: system.fullname, }; - await this.repository.addOrUpdateSystem(constructedSystem); + await this.repository.updateSystem(constructedSystem); systemIds.delete(constructedSystem.id); })); } else { diff --git a/src/loaders/loadShuttleTestData.ts b/src/loaders/loadShuttleTestData.ts index 76cce74..9b1f8f3 100644 --- a/src/loaders/loadShuttleTestData.ts +++ b/src/loaders/loadShuttleTestData.ts @@ -4456,7 +4456,7 @@ const etas: IEta[] = [ export async function loadShuttleTestData(repository: ShuttleGetterSetterRepository) { await Promise.all(systems.map(async (system) => { - await repository.addOrUpdateSystem(system); + await repository.updateSystem(system); })); await Promise.all(routes.map(async (route) => { await repository.addOrUpdateRoute(route); diff --git a/src/repositories/ShuttleGetterRepository.ts b/src/repositories/ShuttleGetterRepository.ts index 28a9de2..063f6e2 100644 --- a/src/repositories/ShuttleGetterRepository.ts +++ b/src/repositories/ShuttleGetterRepository.ts @@ -1,8 +1,7 @@ import { IEta, IOrderedStop, IRoute, IShuttle, IStop, ISystem } from "../entities/entities"; export interface ShuttleGetterRepository { - getSystemIfExists(): Promise; - getSystemById(systemId: string): Promise; + getSystemIfExists(): Promise; getStopsBySystemId(systemId: string): Promise; getStopById(stopId: string): Promise; diff --git a/src/repositories/ShuttleGetterSetterRepository.ts b/src/repositories/ShuttleGetterSetterRepository.ts index 0c0bf90..5153609 100644 --- a/src/repositories/ShuttleGetterSetterRepository.ts +++ b/src/repositories/ShuttleGetterSetterRepository.ts @@ -12,14 +12,13 @@ import { IEta, IOrderedStop, IRoute, IShuttle, IStop, ISystem } from "../entitie */ export interface ShuttleGetterSetterRepository extends ShuttleGetterRepository { // Setter methods - addOrUpdateSystem(system: ISystem): Promise; + updateSystem(system: ISystem): Promise; addOrUpdateRoute(route: IRoute): Promise; addOrUpdateShuttle(shuttle: IShuttle): Promise; addOrUpdateStop(stop: IStop): Promise; addOrUpdateOrderedStop(orderedStop: IOrderedStop): Promise; addOrUpdateEta(eta: IEta): Promise; - removeSystemIfExists(systemId: string): Promise; removeRouteIfExists(routeId: string): Promise; removeShuttleIfExists(shuttleId: string): Promise; removeStopIfExists(stopId: string): Promise; diff --git a/src/repositories/UnoptimizedInMemoryShuttleRepository.ts b/src/repositories/UnoptimizedInMemoryShuttleRepository.ts index d807556..90e088a 100644 --- a/src/repositories/UnoptimizedInMemoryShuttleRepository.ts +++ b/src/repositories/UnoptimizedInMemoryShuttleRepository.ts @@ -7,7 +7,7 @@ import { IEntityWithId, IEta, IOrderedStop, IRoute, IShuttle, IStop, ISystem } f * switching to another data store later anyways) */ export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetterRepository { - private systems: ISystem[] = []; + private system: ISystem | null = null; private stops: IStop[] = []; private routes: IRoute[] = []; private shuttles: IShuttle[] = []; @@ -17,7 +17,7 @@ export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetter private subscribers: ((eta: IEta) => void)[] = []; public async getSystemIfExists() { - return this.systems; + return this.system; } public async getSystemById(systemId: string) { @@ -99,7 +99,7 @@ export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetter return entity; } - public async addOrUpdateSystem(system: ISystem): Promise { + public async updateSystem(system: ISystem): Promise { const index = this.systems.findIndex((s) => s.id === system.id); if (index !== -1) { this.systems[index] = system; // Update existing diff --git a/test/loaders/ApiBasedShuttleRepositoryLoaderTests.test.ts b/test/loaders/ApiBasedShuttleRepositoryLoaderTests.test.ts index 5cf30f1..93489dc 100644 --- a/test/loaders/ApiBasedShuttleRepositoryLoaderTests.test.ts +++ b/test/loaders/ApiBasedShuttleRepositoryLoaderTests.test.ts @@ -35,7 +35,7 @@ describe("ApiBasedRepositoryLoader", () => { // Arrange const systemsToPrune = generateMockSystems(); await Promise.all(systemsToPrune.map(async (system) => { - await loader.repository.addOrUpdateSystem(system); + await loader.repository.updateSystem(system); })); const numberOfSystemsInResponse = fetchSystemDataSuccessfulResponse.all.length; @@ -78,7 +78,7 @@ describe("ApiBasedRepositoryLoader", () => { const systems = generateMockSystems(); await Promise.all(systems.map(async (system) => { - await loader.repository.addOrUpdateSystem(system); + await loader.repository.updateSystem(system); })); await loader.fetchAndUpdateRouteDataForExistingSystemsInRepository(); @@ -128,7 +128,7 @@ describe("ApiBasedRepositoryLoader", () => { const systems = generateMockSystems(); await Promise.all(systems.map(async (system) => { - await loader.repository.addOrUpdateSystem(system); + await loader.repository.updateSystem(system); })); await loader.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository(); @@ -183,7 +183,7 @@ describe("ApiBasedRepositoryLoader", () => { const systems = generateMockSystems(); await Promise.all(systems.map(async (system) => { - await loader.repository.addOrUpdateSystem(system); + await loader.repository.updateSystem(system); })) await loader.fetchAndUpdateShuttleDataForExistingSystemsInRepository(); @@ -226,7 +226,7 @@ describe("ApiBasedRepositoryLoader", () => { const systems = generateMockSystems(); await Promise.all(systems.map(async (system) => { - await loader.repository.addOrUpdateSystem(system); + await loader.repository.updateSystem(system); })); await loader.fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository(); diff --git a/test/repositories/UnoptimizedInMemoryShuttleRepositoryTests.test.ts b/test/repositories/UnoptimizedInMemoryShuttleRepositoryTests.test.ts index 9801037..a634373 100644 --- a/test/repositories/UnoptimizedInMemoryShuttleRepositoryTests.test.ts +++ b/test/repositories/UnoptimizedInMemoryShuttleRepositoryTests.test.ts @@ -24,7 +24,7 @@ describe("UnoptimizedInMemoryRepository", () => { describe("getSystems", () => { test("gets the system stored in the repository", async () => { const mockSystems = generateMockSystems(); - await repository.addOrUpdateSystem(mockSystems[0]); + await repository.updateSystem(mockSystems[0]); const result = await repository.getSystemIfExists(); @@ -42,7 +42,7 @@ describe("UnoptimizedInMemoryRepository", () => { test("gets a system by the ID if it exists", async () => { const mockSystems = generateMockSystems(); for (const system of mockSystems) { - await repository.addOrUpdateSystem(system); + await repository.updateSystem(system); } const result = await repository.getSystemById("2"); @@ -327,7 +327,7 @@ describe("UnoptimizedInMemoryRepository", () => { const mockSystems = generateMockSystems(); const newSystem = mockSystems[0]; - await repository.addOrUpdateSystem(newSystem); + await repository.updateSystem(newSystem); const result = await repository.getSystemIfExists(); expect(result).toEqual([newSystem]); @@ -339,8 +339,8 @@ describe("UnoptimizedInMemoryRepository", () => { const updatedSystem = structuredClone(existingSystem); updatedSystem.name = "Updated System"; - await repository.addOrUpdateSystem(existingSystem); - await repository.addOrUpdateSystem(updatedSystem); + await repository.updateSystem(existingSystem); + await repository.updateSystem(updatedSystem); const result = await repository.getSystemIfExists(); expect(result).toEqual([updatedSystem]); @@ -476,7 +476,7 @@ describe("UnoptimizedInMemoryRepository", () => { test("removes system given ID", async () => { const mockSystems = generateMockSystems(); await Promise.all(mockSystems.map(async (system) => { - await repository.addOrUpdateSystem(system); + await repository.updateSystem(system); })); const systemToRemove = mockSystems[0]; @@ -489,7 +489,7 @@ describe("UnoptimizedInMemoryRepository", () => { test("does nothing if system doesn't exist", async () => { const mockSystems = generateMockSystems(); await Promise.all(mockSystems.map(async (system) => { - await repository.addOrUpdateSystem(system); + await repository.updateSystem(system); })); await repository.removeSystemIfExists("nonexistent-id"); @@ -664,7 +664,7 @@ describe("UnoptimizedInMemoryRepository", () => { test("clears all systems from the repository", async () => { const mockSystems = generateMockSystems(); for (const system of mockSystems) { - await repository.addOrUpdateSystem(system); + await repository.updateSystem(system); } await repository.clearSystemData(); @@ -676,7 +676,7 @@ describe("UnoptimizedInMemoryRepository", () => { test("clears system data when the repository has data", async () => { const mockSystems = generateMockSystems(); const system = mockSystems[0]; - await repository.addOrUpdateSystem(system); + await repository.updateSystem(system); await repository.clearSystemData(); diff --git a/test/resolvers/QueryResolverTests.test.ts b/test/resolvers/QueryResolverTests.test.ts index 52d6605..5d90c57 100644 --- a/test/resolvers/QueryResolverTests.test.ts +++ b/test/resolvers/QueryResolverTests.test.ts @@ -15,7 +15,7 @@ describe("QueryResolvers", () => { async function addMockSystems() { const systems = generateMockSystems(); await Promise.all(systems.map(async (system) => { - await context.shuttleRepository.addOrUpdateSystem(system); + await context.shuttleRepository.updateSystem(system); })); return systems; } diff --git a/test/resolvers/SystemResolverTests.test.ts b/test/resolvers/SystemResolverTests.test.ts index 1eb817c..c9de4a1 100644 --- a/test/resolvers/SystemResolverTests.test.ts +++ b/test/resolvers/SystemResolverTests.test.ts @@ -133,7 +133,7 @@ describe("SystemResolvers", () => { ...mockSystem, id: "2", } - await context.shuttleRepository.addOrUpdateSystem(updatedSystem); + await context.shuttleRepository.updateSystem(updatedSystem); const mockStop = await addMockStopToRepository(context.shuttleRepository, updatedSystem.id); @@ -199,7 +199,7 @@ describe("SystemResolvers", () => { ...mockSystem, id: "2", } - await context.shuttleRepository.addOrUpdateSystem(updatedSystem); + await context.shuttleRepository.updateSystem(updatedSystem); const mockRoute = await addMockRouteToRepository(context.shuttleRepository, updatedSystem.id); @@ -265,7 +265,7 @@ describe("SystemResolvers", () => { ...mockSystem, id: "2", } - await context.shuttleRepository.addOrUpdateSystem(updatedSystem); + await context.shuttleRepository.updateSystem(updatedSystem); const mockShuttle = await addMockShuttleToRepository(context.shuttleRepository, updatedSystem.id); diff --git a/test/testHelpers/repositorySetupHelpers.ts b/test/testHelpers/repositorySetupHelpers.ts index 58caea3..357d652 100644 --- a/test/testHelpers/repositorySetupHelpers.ts +++ b/test/testHelpers/repositorySetupHelpers.ts @@ -11,7 +11,7 @@ export async function addMockSystemToRepository(repository: ShuttleGetterSetterR const mockSystems = generateMockSystems(); const mockSystem = mockSystems[0]; mockSystem.id = "1"; - await repository.addOrUpdateSystem(mockSystem); + await repository.updateSystem(mockSystem); return mockSystem; }