Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Contexts/Mooc/Courses/application/CourseCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ export class CourseCreator {
}

async run({ courseId, courseName, courseDuration }: Params): Promise<void> {
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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
};
}
}
Original file line number Diff line number Diff line change
@@ -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<MongoClient> {
static async createClient(contextName: string, config: MongoConfig): Promise<MongoClient> {
let client = MongoClientFactory.getClient(contextName);

if (!client) {
client = await MongoClientFactory.createAndConnectClient();
client = await MongoClientFactory.createAndConnectClient(config);

MongoClientFactory.registerClient(client, contextName);
}
Expand All @@ -21,8 +21,8 @@ export class MongoClientFactory {
return MongoClientFactory.clients[contextName];
}

private static async createAndConnectClient(): Promise<MongoClient> {
const client = new MongoClient(config.get('mongo.url'), { useUnifiedTopology: true, ignoreUndefined: true });
private static async createAndConnectClient(config: MongoConfig): Promise<MongoClient> {
const client = new MongoClient(config.url, { useUnifiedTopology: true, ignoreUndefined: true });

await client.connect();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type MongoConfig = { url: string };

export default MongoConfig;
Original file line number Diff line number Diff line change
@@ -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']
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -19,15 +19,15 @@ 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);

await newClient.close();
});

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);

Expand Down