Skip to content

Commit 331b8dc

Browse files
committed
Use command and query bus in backoffice frontend app
1 parent fced599 commit 331b8dc

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

src/apps/backoffice/frontend/config/dependency-injection/application.yaml

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ services:
88

99
Mooc.coursesCounter.CoursesCounterFinder:
1010
class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder
11-
arguments: ["@Mooc.coursesCounter.CoursesCounterRepository"]
11+
arguments: ['@Mooc.coursesCounter.CoursesCounterRepository']
1212

1313
Mooc.coursesCounter.CoursesCounterRepository:
1414
class: ../../../../../Contexts/Mooc/CoursesCounter/infrastructure/persistence/mongo/MongoCoursesCounterRepository
15-
arguments: ["@Mooc.shared.ConnectionManager"]
15+
arguments: ['@Mooc.shared.ConnectionManager']
1616

1717
Mooc.shared.ConnectionManager:
1818
factory:
@@ -34,8 +34,36 @@ services:
3434

3535
Apps.Backoffice.Frontend.controllers.CoursesGetController:
3636
class: ../../controllers/CoursesGetController
37-
arguments: ['@Mooc.coursesCounter.CoursesCounterFinder']
37+
arguments: ['@Mooc.shared.QueryBus']
3838

3939
Apps.Backoffice.Frontend.controllers.CoursesPostController:
4040
class: ../../controllers/CoursesPostController
41+
arguments: ['@Mooc.shared.CommandBus']
42+
43+
Mooc.coursesCounter.FindCoursesCounterQueryHandler:
44+
class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQueryHandler
45+
arguments: ['@Mooc.coursesCounter.CoursesCounterFinder']
46+
tags:
47+
- { name: 'queryHandler' }
48+
49+
Mooc.courses.CreateCourseCommandHandler:
50+
class: ../../../../../Contexts/Mooc/Courses/application/CreateCourseCommandHandler
4151
arguments: ['@Mooc.courses.CourseCreator']
52+
tags:
53+
- { name: 'commandHandler' }
54+
55+
Mooc.shared.QueryBus:
56+
class: ../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus
57+
arguments: ['@Mooc.shared.QueryHandlersInformation']
58+
59+
Mooc.shared.CommandBus:
60+
class: ../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus
61+
arguments: ['@Mooc.shared.CommandHandlersInformation']
62+
63+
Mooc.shared.QueryHandlersInformation:
64+
class: ../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation
65+
arguments: ['!tagged queryHandler']
66+
67+
Mooc.shared.CommandHandlersInformation:
68+
class: ../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation
69+
arguments: ['!tagged commandHandler']

src/apps/backoffice/frontend/controllers/CoursesGetController.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Request, Response } from 'express';
2-
import { CoursesCounterFinder } from '../../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder';
32
import { Uuid } from '../../../../Contexts/Shared/domain/value-object/Uuid';
3+
import { QueryBus } from '../../../../Contexts/Shared/domain/QueryBus';
4+
import { FindCoursesCounterResponse } from '../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterResponse';
5+
import { FindCoursesCounterQuery } from '../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQuery';
46

57
export class CoursesGetController {
6-
constructor(private coursesCounterFinder: CoursesCounterFinder) {}
8+
constructor(private queryBus: QueryBus) {}
79

810
async run(req: Request, res: Response) {
9-
const courses = await this.coursesCounterFinder.run();
11+
const courses = await this.queryBus.ask<FindCoursesCounterResponse>(new FindCoursesCounterQuery());
1012

1113
res.render('pages/courses/courses', {
1214
title: 'Welcome',

src/apps/backoffice/frontend/controllers/CoursesPostController.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Request, Response } from 'express';
2-
import { CreateCourseRequest } from '../../../../Contexts/Mooc/Courses/application/CreateCourseRequest';
3-
import { CourseCreator } from '../../../../Contexts/Mooc/Courses/application/CourseCreator';
2+
import { CommandBus } from '../../../../Contexts/Shared/domain/CommandBus';
3+
import { CreateCourseCommand } from '../../../../Contexts/Mooc/Courses/application/CreateCourseCommand';
44

55
export class CoursesPostController {
6-
constructor(private courseCreator: CourseCreator) {}
6+
constructor(private commandBus: CommandBus) {}
77

88
async run(req: Request, res: Response) {
99
// TODO: validation
@@ -13,11 +13,13 @@ export class CoursesPostController {
1313
}
1414

1515
private async createCourse(req: Request, res: Response) {
16-
await this.courseCreator.run({
17-
courseId: req.body.id,
18-
courseName: req.body.name,
19-
courseDuration: req.body.duration
16+
const createCourseCommand = new CreateCourseCommand({
17+
id: req.body.id,
18+
name: req.body.name,
19+
duration: req.body.duration
2020
});
21+
await this.commandBus.dispatch(createCourseCommand);
22+
2123
req.flash('message', `Felicidades, el curso ${req.body.name} ha sido creado!`);
2224
res.redirect('/courses');
2325
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ services:
2020
class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder
2121
arguments: ["@Mooc.coursesCounter.CoursesCounterRepository"]
2222

23-
2423
Mooc.coursesCounter.FindCoursesCounterQueryHandler:
2524
class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQueryHandler
2625
arguments: ["@Mooc.coursesCounter.CoursesCounterFinder"]
2726
tags:
2827
- { name: 'queryHandler' }
29-

0 commit comments

Comments
 (0)