diff --git a/src/Contexts/Mooc/Courses/application/CourseCreator.ts b/src/Contexts/Mooc/Courses/application/CourseCreator.ts index db2b81b..98b28bd 100644 --- a/src/Contexts/Mooc/Courses/application/CourseCreator.ts +++ b/src/Contexts/Mooc/Courses/application/CourseCreator.ts @@ -22,11 +22,7 @@ export class CourseCreator { } async run({ courseId, courseName, courseDuration }: Params): Promise { - const course = Course.create( - courseId, - courseName, - courseDuration - ); + const course = Course.create(courseId, courseName, courseDuration); await this.repository.save(course); await this.eventBus.publish(course.pullDomainEvents()); diff --git a/src/Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory.ts b/src/Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory.ts new file mode 100644 index 0000000..60d48ca --- /dev/null +++ b/src/Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory.ts @@ -0,0 +1,10 @@ +import config from '../../../../../../apps/mooc_backend/config/config'; +import MongoConfig from '../../../../../Shared/infrastructure/persistence/mongo/MongoConfig'; + +export class MongoConfigFactory { + static createConfig(): MongoConfig { + return { + url: config.get('mongo.url') + }; + } +} diff --git a/src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts b/src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts index 1de5766..b93d08b 100644 --- a/src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts +++ b/src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts @@ -1,15 +1,15 @@ import { MongoClient } from 'mongodb'; -import config from '../../../../../apps/mooc_backend/config/config'; import { Nullable } from '../../../domain/Nullable'; +import MongoConfig from './MongoConfig'; export class MongoClientFactory { private static clients: { [key: string]: MongoClient } = {}; - static async createClient(contextName: string): Promise { + static async createClient(contextName: string, config: MongoConfig): Promise { let client = MongoClientFactory.getClient(contextName); if (!client) { - client = await MongoClientFactory.createAndConnectClient(); + client = await MongoClientFactory.createAndConnectClient(config); MongoClientFactory.registerClient(client, contextName); } @@ -21,8 +21,8 @@ export class MongoClientFactory { return MongoClientFactory.clients[contextName]; } - private static async createAndConnectClient(): Promise { - const client = new MongoClient(config.get('mongo.url'), { useUnifiedTopology: true, ignoreUndefined: true }); + private static async createAndConnectClient(config: MongoConfig): Promise { + const client = new MongoClient(config.url, { useUnifiedTopology: true, ignoreUndefined: true }); await client.connect(); diff --git a/src/Contexts/Shared/infrastructure/persistence/mongo/MongoConfig.ts b/src/Contexts/Shared/infrastructure/persistence/mongo/MongoConfig.ts new file mode 100644 index 0000000..ce64e4d --- /dev/null +++ b/src/Contexts/Shared/infrastructure/persistence/mongo/MongoConfig.ts @@ -0,0 +1,3 @@ +type MongoConfig = { url: string }; + +export default MongoConfig; diff --git a/src/apps/backoffice/frontend/config/dependency-injection/Shared/application.yaml b/src/apps/backoffice/frontend/config/dependency-injection/Shared/application.yaml index 043d1ed..0df3702 100644 --- a/src/apps/backoffice/frontend/config/dependency-injection/Shared/application.yaml +++ b/src/apps/backoffice/frontend/config/dependency-injection/Shared/application.yaml @@ -1,30 +1,35 @@ services: Shared.Logger: - class: ../../../../../../Contexts/Shared/infrastructure/WinstonLogger + class: ../../../../../../Contexts/Shared/infrastructure/WinstonLogger arguments: [] + Shared.MongoConfig: + factory: + class: ../../../../../../Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory + method: 'createConfig' + Shared.ConnectionManager: factory: - class: ../../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory + class: ../../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory method: 'createClient' - arguments: ['mooc'] + arguments: ['mooc', '@Shared.MongoConfig'] Shared.EventBus: - class: ../../../../../../Contexts/Shared/infrastructure/EventBus/InMemorySyncEventBus + class: ../../../../../../Contexts/Shared/infrastructure/EventBus/InMemoryAsyncEventBus arguments: [] Shared.QueryBus: - class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus + class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus arguments: ['@Shared.QueryHandlersInformation'] Shared.CommandBus: - class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus + class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus arguments: ['@Shared.CommandHandlersInformation'] Shared.QueryHandlersInformation: - class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation + class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation arguments: ['!tagged queryHandler'] Shared.CommandHandlersInformation: - class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation + class: ../../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation arguments: ['!tagged commandHandler'] diff --git a/src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml b/src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml index b3f8a05..ba5254a 100644 --- a/src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml +++ b/src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml @@ -1,9 +1,14 @@ services: + Shared.MongoConfig: + factory: + class: ../../../../../Contexts/Mooc/Shared/infrastructure/persistence/mongo/MongoConfigFactory + method: 'createConfig' + Shared.ConnectionManager: factory: class: ../../../../../Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory method: 'createClient' - arguments: ['mooc'] + arguments: ['mooc', '@Shared.MongoConfig'] Shared.Logger: class: ../../../../../Contexts/Shared/infrastructure/WinstonLogger diff --git a/tests/Contexts/Shared/infrastructure/MongoClientFactory.test.ts b/tests/Contexts/Shared/infrastructure/MongoClientFactory.test.ts index bb1de8b..6643ecb 100644 --- a/tests/Contexts/Shared/infrastructure/MongoClientFactory.test.ts +++ b/tests/Contexts/Shared/infrastructure/MongoClientFactory.test.ts @@ -6,7 +6,7 @@ describe('Create a client', () => { let client: MongoClient; beforeEach(async () => { - client = await factory.createClient('test'); + client = await factory.createClient('test', { url: 'mongodb://localhost:27017/mooc-backend-test' }); }); afterEach(async () => { @@ -19,7 +19,7 @@ describe('Create a client', () => { }); it('creates a new client if it does not exist a client with the given name', async () => { - const newClient = await factory.createClient('test2'); + const newClient = await factory.createClient('test2', { url: 'mongodb://localhost:27017/mooc-backend-test' }); expect(newClient).not.toBe(client); @@ -27,7 +27,7 @@ describe('Create a client', () => { }); it('returns a client if it already exists', async () => { - const newClient = await factory.createClient('test'); + const newClient = await factory.createClient('test', { url: 'mongodb://localhost:27017/mooc-backend-test' }); expect(newClient).toBe(client);