Skip to content

Commit d6b7729

Browse files
ivanportillorsaladocidaliondev
committed
Refactor FindCourseCounter to use query bus
Co-authored-by: Rubén Salado <rsaladocid@users.noreply.github.com> Co-authored-by: aliondev <antonioleon@audiense.com>
1 parent 69f44d6 commit d6b7729

File tree

11 files changed

+64
-22
lines changed

11 files changed

+64
-22
lines changed

src/Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CoursesCounterRepository } from '../../domain/CoursesCounterRepository';
22
import { CoursesCounterNotExist } from '../../domain/CoursesCounterNotExist';
3-
import { CoursesCounterResponse } from './CoursesCounterResponse';
3+
import { FindCoursesCounterResponse } from './FindCoursesCounterResponse';
44

55
export class CoursesCounterFinder {
66
constructor(private repository: CoursesCounterRepository) {}
@@ -11,6 +11,6 @@ export class CoursesCounterFinder {
1111
throw new CoursesCounterNotExist();
1212
}
1313

14-
return new CoursesCounterResponse(counter.total.value);
14+
return new FindCoursesCounterResponse(counter.total.value);
1515
}
1616
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Query } from "../../../../Shared/domain/Query";
2+
3+
export class FindCoursesCounterQuery implements Query {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { QueryHandler } from "../../../../Shared/domain/QueryHandler";
2+
import { FindCoursesCounterQuery } from "./FindCoursesCounterQuery";
3+
import { FindCoursesCounterResponse } from "./FindCoursesCounterResponse";
4+
import { Query } from "../../../../Shared/domain/Query";
5+
import { CoursesCounterFinder } from "./CoursesCounterFinder";
6+
7+
export class FindCoursesCounterQueryHandler implements QueryHandler<FindCoursesCounterQuery, FindCoursesCounterResponse> {
8+
constructor(private finder: CoursesCounterFinder) {}
9+
10+
subscribedTo(): Query {
11+
return FindCoursesCounterQuery;
12+
}
13+
handle(_query: FindCoursesCounterQuery): Promise<FindCoursesCounterResponse> {
14+
return this.finder.run();
15+
}
16+
17+
}

src/Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterResponse.ts renamed to src/Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class CoursesCounterResponse {
1+
export class FindCoursesCounterResponse {
22
readonly total: number;
33

44
constructor(total: number) {

src/apps/mooc_backend/config/dependency-injection/CoursesCounter/application.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
services:
2-
32
Mooc.coursesCounter.CoursesCounterRepository:
43
class: ../../../../../Contexts/Mooc/CoursesCounter/infrastructure/persistence/mongo/MongoCoursesCounterRepository
54
arguments: ["@Mooc.shared.ConnectionManager"]
@@ -21,4 +20,10 @@ services:
2120
class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder
2221
arguments: ["@Mooc.coursesCounter.CoursesCounterRepository"]
2322

23+
24+
Mooc.coursesCounter.FindCoursesCounterQueryHandler:
25+
class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQueryHandler
26+
arguments: ["@Mooc.coursesCounter.CoursesCounterFinder"]
27+
tags:
28+
- { name: 'queryHandler' }
2429

src/apps/mooc_backend/config/dependency-injection/Shared/application.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ services:
2828
Mooc.shared.EventBus.DomainEventJsonDeserializer:
2929
class: ../../../../../Contexts/Shared/infrastructure/EventBus/DomainEventJsonDeserializer
3030
arguments: ['@Mooc.shared.EventBus.DomainEventMapping']
31+
32+
Mooc.shared.QueryHandlersInformation:
33+
class: ../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation
34+
arguments: ['!tagged queryHandler']
35+
36+
Mooc.shared.QueryBus:
37+
class: ../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus
38+
arguments: ['@Mooc.shared.QueryHandlersInformation']

src/apps/mooc_backend/config/dependency-injection/apps/application.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ services:
1010

1111
Apps.mooc.controllers.CoursesCounterGetController:
1212
class: ../../../controllers/CoursesCounterGetController
13-
arguments: ["@Mooc.coursesCounter.CoursesCounterFinder"]
13+
arguments: ["@Mooc.shared.QueryBus"]

src/apps/mooc_backend/controllers/CoursesCounterGetController.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { Controller } from './Controller';
22
import { Request, Response } from 'express';
3-
import { CoursesCounterFinder } from '../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder';
43
import httpStatus = require('http-status');
54
import { CoursesCounterNotExist } from '../../../Contexts/Mooc/CoursesCounter/domain/CoursesCounterNotExist';
5+
import { FindCoursesCounterQuery } from '../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQuery';
6+
import { QueryBus } from '../../../Contexts/Shared/domain/QueryBus';
7+
import { FindCoursesCounterResponse } from '../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterResponse';
68

79
export class CoursesCounterGetController implements Controller {
8-
constructor(private coursesCounterFinder: CoursesCounterFinder) {}
10+
constructor(private queryBus: QueryBus) {}
911
async run(req: Request, res: Response): Promise<void> {
1012
try {
11-
const query = new FindCourseCounterQuery();
12-
const count = await this.queryBus.query(query);
13+
const query = new FindCoursesCounterQuery();
14+
const count = await this.queryBus.ask<FindCoursesCounterResponse>(query);
1315

14-
const counter = await this.coursesCounterFinder.run();
15-
res.status(httpStatus.OK).send(counter);
16+
res.status(httpStatus.OK).send(count);
1617
} catch (e) {
1718
if (e instanceof CoursesCounterNotExist) {
1819
res.status(httpStatus.NOT_FOUND).send();

tests/Contexts/Mooc/CoursesCounter/__mocks__/CoursesCounterRepositoryMock.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { CoursesCounterRepository } from '../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounterRepository';
22
import { CoursesCounter } from '../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounter';
33
import { Nullable } from '../../../../../src/Contexts/Shared/domain/Nullable';
4-
import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';
54

65
export class CoursesCounterRepositoryMock implements CoursesCounterRepository {
76
private mockSave = jest.fn();

tests/Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder.test.ts renamed to tests/Contexts/Mooc/CoursesCounter/application/Find/FindCourseCounterQueryHandler.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,37 @@ import { CoursesCounterMother } from '../../domain/CoursesCounterMother';
33
import { CoursesCounterRepositoryMock } from '../../__mocks__/CoursesCounterRepositoryMock';
44
import { CoursesCounterResponseMother } from '../../domain/CoursesCounterResponseMother';
55
import { CoursesCounterNotExist } from '../../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounterNotExist';
6+
import { FindCoursesCounterQueryHandler } from '../../../../../../src/Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQueryHandler';
7+
import { FindCoursesCounterQuery } from '../../../../../../src/Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQuery';
68

7-
describe('CoursesCounter Finder', () => {
8-
let finder: CoursesCounterFinder;
9+
describe('FindCourseCounter QueryHandler', () => {
910
let repository: CoursesCounterRepositoryMock;
1011

1112
beforeEach(() => {
1213
repository = new CoursesCounterRepositoryMock();
13-
finder = new CoursesCounterFinder(repository);
1414
});
1515

16+
1617
it('should find an existing courses counter', async () => {
1718
const counter = CoursesCounterMother.random();
18-
const expected = CoursesCounterResponseMother.create(counter.total);
1919
repository.returnOnSearch(counter);
2020

21-
const actual = await finder.run();
22-
21+
const handler = new FindCoursesCounterQueryHandler(new CoursesCounterFinder(repository));
22+
23+
const query = new FindCoursesCounterQuery();
24+
const response = await handler.handle(query);
25+
2326
repository.assertSearch();
24-
expect(expected).toEqual(actual);
27+
28+
const expected = CoursesCounterResponseMother.create(counter.total);
29+
expect(expected).toEqual(response);
2530
});
2631

2732
it('should throw an exception when courses counter does not exists', async () => {
28-
await expect(finder.run()).rejects.toBeInstanceOf(CoursesCounterNotExist);
33+
const handler = new FindCoursesCounterQueryHandler(new CoursesCounterFinder(repository));
34+
35+
const query = new FindCoursesCounterQuery();
36+
37+
await expect(handler.handle(query)).rejects.toBeInstanceOf(CoursesCounterNotExist);
2938
});
3039
});

0 commit comments

Comments
 (0)