Skip to content

Commit 784ab21

Browse files
aliondevrsaladocidivanportillo
committed
Command handler to create course
Co-authored-by: Rubén Salado <rsaladocid@users.noreply.github.com> Co-authored-by: Iván Portillo <ivanportillo@me.com>
1 parent 3c9c97f commit 784ab21

File tree

10 files changed

+77
-13
lines changed

10 files changed

+77
-13
lines changed

src/Contexts/Mooc/Courses/application/CourseCreator.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { CourseName } from '../domain/CourseName';
66
import { CourseDuration } from '../domain/CourseDuration';
77
import { EventBus } from '../../../Shared/domain/EventBus';
88

9+
type Params = {
10+
courseId: CourseId;
11+
courseName: CourseName;
12+
courseDuration: CourseDuration;
13+
};
14+
915
export class CourseCreator {
1016
private repository: CourseRepository;
1117
private eventBus: EventBus;
@@ -15,11 +21,11 @@ export class CourseCreator {
1521
this.eventBus = eventBus;
1622
}
1723

18-
async run(request: CreateCourseRequest): Promise<void> {
24+
async run({ courseId, courseName, courseDuration }: Params): Promise<void> {
1925
const course = Course.create(
20-
new CourseId(request.id),
21-
new CourseName(request.name),
22-
new CourseDuration(request.duration)
26+
courseId,
27+
courseName,
28+
courseDuration
2329
);
2430

2531
await this.repository.save(course);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Command } from '../../../Shared/domain/Command';
2+
3+
type Params = {
4+
id: string;
5+
name: string;
6+
duration: string;
7+
};
8+
9+
export class CreateCourseCommand extends Command {
10+
id: string;
11+
name: string;
12+
duration: string;
13+
14+
constructor({ id, name, duration }: Params) {
15+
super();
16+
this.id = id;
17+
this.name = name;
18+
this.duration = duration;
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { CreateCourseCommand } from './CreateCourseCommand';
2+
import { CommandHandler } from '../../../Shared/domain/CommandHandler';
3+
import { CourseCreator } from './CourseCreator';
4+
import { Command } from '../../../Shared/domain/Command';
5+
import { CourseId } from '../../Shared/domain/Courses/CourseId';
6+
import { CourseName } from '../domain/CourseName';
7+
import { CourseDuration } from '../domain/CourseDuration';
8+
9+
export class CreateCourseCommandHandler implements CommandHandler<CreateCourseCommand> {
10+
constructor(private courseCreator: CourseCreator) {}
11+
12+
subscribedTo(): Command {
13+
return CreateCourseCommand;
14+
}
15+
16+
async handle(command: CreateCourseCommand): Promise<void> {
17+
const courseId = new CourseId(command.id);
18+
const courseName = new CourseName(command.name);
19+
const courseDuration = new CourseDuration(command.duration);
20+
await this.courseCreator.run({ courseId, courseName, courseDuration });
21+
}
22+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command } from './Command';
22

33
export interface CommandBus {
4-
dispatch(command: Command): void;
4+
dispatch(command: Command): Promise<void>;
55
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Command } from './Command';
22

33
export interface CommandHandler<T extends Command> {
4-
subscribedTo(): T;
5-
handle(command: T): void;
4+
subscribedTo(): Command;
5+
handle(command: T): Promise<void>;
66
}

src/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { CommandHandlersInformation } from './CommandHandlersInformation';
55
export class InMemoryCommandBus implements CommandBus {
66
constructor(private commandHandlersInformation: CommandHandlersInformation) {}
77

8-
dispatch(command: Command): void {
8+
async dispatch(command: Command): Promise<void> {
99
const handler = this.commandHandlersInformation.search(command);
1010

11-
handler.handle(command);
11+
await handler.handle(command);
1212
}
1313
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ services:
77
Mooc.courses.CourseCreator:
88
class: ../../../../../Contexts/Mooc/Courses/application/CourseCreator
99
arguments: ['@Mooc.courses.CourseRepository', '@Mooc.shared.EventBus']
10+
11+
Mooc.courses.CreateCourseCommandHandler:
12+
class: ../../../../../Contexts/Mooc/Courses/application/CreateCourseCommandHandler
13+
arguments: ['@Mooc.courses.CourseCreator']
14+
tags:
15+
- { name: 'commandHandler' }

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ services:
1313
class: ../../../../../Contexts/Shared/infrastructure/EventBus/InMemoryAsyncEventBus
1414
arguments: []
1515

16+
Mooc.shared.CommandHandlersInformation:
17+
class: ../../../../../Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation
18+
arguments: ['!tagged commandHandler']
19+
20+
Mooc.shared.CommandBus:
21+
class: ../../../../../Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus
22+
arguments: ['@Mooc.shared.CommandHandlersInformation']
23+
1624
Mooc.shared.EventBus.DomainEventMapping:
1725
class: ../../../../../Contexts/Shared/infrastructure/EventBus/DomainEventMapping
1826
arguments: ['!tagged domainEventSubscriber']

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ services:
22

33
Apps.mooc.controllers.CoursePutController:
44
class: ../../../controllers/CoursePutController
5-
arguments: ["@Mooc.courses.CourseCreator"]
5+
arguments: ["@Mooc.shared.CommandBus"]
66

77
Apps.mooc.controllers.StatusGetController:
88
class: ../../../controllers/StatusGetController

src/apps/mooc_backend/controllers/CoursePutController.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { Request, Response } from 'express';
2-
import { CourseCreator } from '../../../Contexts/Mooc/Courses/application/CourseCreator';
32
import httpStatus from 'http-status';
43
import { Controller } from './Controller';
54
import { CourseAlreadyExists } from '../../../Contexts/Mooc/Courses/domain/CourseAlreadyExists';
5+
import { CommandBus } from '../../../Contexts/Shared/domain/CommandBus';
6+
import { CreateCourseCommand } from '../../../Contexts/Mooc/Courses/application/CreateCourseCommand';
67

78
export class CoursePutController implements Controller {
8-
constructor(private courseCreator: CourseCreator) {}
9+
constructor(private commandBus: CommandBus) {}
910

1011
async run(req: Request, res: Response) {
1112
const id: string = req.params.id;
1213
const name: string = req.body.name;
1314
const duration: string = req.body.duration;
15+
const createCourseCommand = new CreateCourseCommand({ id, name, duration });
1416

1517
try {
16-
await this.courseCreator.run({ id, name, duration });
18+
await this.commandBus.dispatch(createCourseCommand);
1719
} catch (error) {
1820
if (error instanceof CourseAlreadyExists) {
1921
res.status(httpStatus.BAD_REQUEST).send(error.message);

0 commit comments

Comments
 (0)