From 331b8dc35d126f5125833418370d4a25e5477794 Mon Sep 17 00:00:00 2001 From: Fran Ortiz Date: Sat, 11 Jul 2020 12:33:37 +0200 Subject: [PATCH] Use command and query bus in backoffice frontend app --- .../dependency-injection/application.yaml | 34 +++++++++++++++++-- .../controllers/CoursesGetController.ts | 8 +++-- .../controllers/CoursesPostController.ts | 16 +++++---- .../CoursesCounter/application.yaml | 2 -- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/apps/backoffice/frontend/config/dependency-injection/application.yaml b/src/apps/backoffice/frontend/config/dependency-injection/application.yaml index 510476e..16fcec8 100644 --- a/src/apps/backoffice/frontend/config/dependency-injection/application.yaml +++ b/src/apps/backoffice/frontend/config/dependency-injection/application.yaml @@ -8,11 +8,11 @@ services: Mooc.coursesCounter.CoursesCounterFinder: class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder - arguments: ["@Mooc.coursesCounter.CoursesCounterRepository"] + arguments: ['@Mooc.coursesCounter.CoursesCounterRepository'] Mooc.coursesCounter.CoursesCounterRepository: class: ../../../../../Contexts/Mooc/CoursesCounter/infrastructure/persistence/mongo/MongoCoursesCounterRepository - arguments: ["@Mooc.shared.ConnectionManager"] + arguments: ['@Mooc.shared.ConnectionManager'] Mooc.shared.ConnectionManager: factory: @@ -34,8 +34,36 @@ services: Apps.Backoffice.Frontend.controllers.CoursesGetController: class: ../../controllers/CoursesGetController - arguments: ['@Mooc.coursesCounter.CoursesCounterFinder'] + arguments: ['@Mooc.shared.QueryBus'] Apps.Backoffice.Frontend.controllers.CoursesPostController: class: ../../controllers/CoursesPostController + arguments: ['@Mooc.shared.CommandBus'] + + Mooc.coursesCounter.FindCoursesCounterQueryHandler: + class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQueryHandler + arguments: ['@Mooc.coursesCounter.CoursesCounterFinder'] + tags: + - { name: 'queryHandler' } + + Mooc.courses.CreateCourseCommandHandler: + class: ../../../../../Contexts/Mooc/Courses/application/CreateCourseCommandHandler arguments: ['@Mooc.courses.CourseCreator'] + tags: + - { name: 'commandHandler' } + + Mooc.shared.QueryBus: + class: ../../../../../Contexts/Shared/infrastructure/QueryBus/InMemoryQueryBus + arguments: ['@Mooc.shared.QueryHandlersInformation'] + + Mooc.shared.CommandBus: + class: ../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus + arguments: ['@Mooc.shared.CommandHandlersInformation'] + + Mooc.shared.QueryHandlersInformation: + class: ../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation + arguments: ['!tagged queryHandler'] + + Mooc.shared.CommandHandlersInformation: + class: ../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation + arguments: ['!tagged commandHandler'] diff --git a/src/apps/backoffice/frontend/controllers/CoursesGetController.ts b/src/apps/backoffice/frontend/controllers/CoursesGetController.ts index 8cd931d..0900f3d 100644 --- a/src/apps/backoffice/frontend/controllers/CoursesGetController.ts +++ b/src/apps/backoffice/frontend/controllers/CoursesGetController.ts @@ -1,12 +1,14 @@ import { Request, Response } from 'express'; -import { CoursesCounterFinder } from '../../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder'; import { Uuid } from '../../../../Contexts/Shared/domain/value-object/Uuid'; +import { QueryBus } from '../../../../Contexts/Shared/domain/QueryBus'; +import { FindCoursesCounterResponse } from '../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterResponse'; +import { FindCoursesCounterQuery } from '../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQuery'; export class CoursesGetController { - constructor(private coursesCounterFinder: CoursesCounterFinder) {} + constructor(private queryBus: QueryBus) {} async run(req: Request, res: Response) { - const courses = await this.coursesCounterFinder.run(); + const courses = await this.queryBus.ask(new FindCoursesCounterQuery()); res.render('pages/courses/courses', { title: 'Welcome', diff --git a/src/apps/backoffice/frontend/controllers/CoursesPostController.ts b/src/apps/backoffice/frontend/controllers/CoursesPostController.ts index f3274ff..f99a1bb 100644 --- a/src/apps/backoffice/frontend/controllers/CoursesPostController.ts +++ b/src/apps/backoffice/frontend/controllers/CoursesPostController.ts @@ -1,9 +1,9 @@ import { Request, Response } from 'express'; -import { CreateCourseRequest } from '../../../../Contexts/Mooc/Courses/application/CreateCourseRequest'; -import { CourseCreator } from '../../../../Contexts/Mooc/Courses/application/CourseCreator'; +import { CommandBus } from '../../../../Contexts/Shared/domain/CommandBus'; +import { CreateCourseCommand } from '../../../../Contexts/Mooc/Courses/application/CreateCourseCommand'; export class CoursesPostController { - constructor(private courseCreator: CourseCreator) {} + constructor(private commandBus: CommandBus) {} async run(req: Request, res: Response) { // TODO: validation @@ -13,11 +13,13 @@ export class CoursesPostController { } private async createCourse(req: Request, res: Response) { - await this.courseCreator.run({ - courseId: req.body.id, - courseName: req.body.name, - courseDuration: req.body.duration + const createCourseCommand = new CreateCourseCommand({ + id: req.body.id, + name: req.body.name, + duration: req.body.duration }); + await this.commandBus.dispatch(createCourseCommand); + req.flash('message', `Felicidades, el curso ${req.body.name} ha sido creado!`); res.redirect('/courses'); } diff --git a/src/apps/mooc_backend/config/dependency-injection/CoursesCounter/application.yaml b/src/apps/mooc_backend/config/dependency-injection/CoursesCounter/application.yaml index 493864c..d4457d9 100644 --- a/src/apps/mooc_backend/config/dependency-injection/CoursesCounter/application.yaml +++ b/src/apps/mooc_backend/config/dependency-injection/CoursesCounter/application.yaml @@ -20,10 +20,8 @@ services: class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder arguments: ["@Mooc.coursesCounter.CoursesCounterRepository"] - Mooc.coursesCounter.FindCoursesCounterQueryHandler: class: ../../../../../Contexts/Mooc/CoursesCounter/application/Find/FindCoursesCounterQueryHandler arguments: ["@Mooc.coursesCounter.CoursesCounterFinder"] tags: - { name: 'queryHandler' } -