Skip to content

Commit 3e9368a

Browse files
authored
Merge pull request #42 from CodelyTV/part-two
Part two
2 parents 1de4cc1 + 8b30b45 commit 3e9368a

File tree

58 files changed

+993
-114
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+993
-114
lines changed

.github/workflows/nodejs.yml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ on: [push]
44

55
jobs:
66
build:
7-
87
runs-on: ubuntu-latest
98

109
strategy:
1110
matrix:
1211
node-version: [8.x, 10.x, 12.x]
12+
mongodb-version: [4.0, 4.2]
1313

1414
steps:
15-
- uses: actions/checkout@v1
16-
- name: Use Node.js ${{ matrix.node-version }}
17-
uses: actions/setup-node@v1
18-
with:
19-
node-version: ${{ matrix.node-version }}
20-
- name: npm install, build, and test
21-
run: |
22-
npm install
23-
npm run build --if-present
24-
npm test
25-
env:
26-
CI: true
15+
- uses: actions/checkout@v1
16+
- name: Use Node.js ${{ matrix.node-version }}
17+
uses: actions/setup-node@v1
18+
with:
19+
node-version: ${{ matrix.node-version }}
20+
- name: Launch MongoDB
21+
uses: wbari/[email protected]
22+
with:
23+
mongoDBVersion: ${{ matrix.mongodb-version }}
24+
- name: npm install, build, and test
25+
run: |
26+
npm install
27+
npm run build --if-present
28+
npm test
29+
env:
30+
CI: true

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ node_modules/
22
dist/
33
.tmp
44
logs/
5-
src/Contexts/Mooc/Courses/infrastructure/courses.*
5+
src/Contexts/Mooc/Courses/infrastructure/persistence/courses.*

package-lock.json

Lines changed: 97 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
"@types/express": "^4.17.2",
2626
"@types/glob": "^7.1.1",
2727
"@types/helmet": "0.0.44",
28+
"@types/mongodb": "^3.3.15",
2829
"@types/node": "~13.1.1",
2930
"@types/uuid": "^3.4.6",
31+
"@types/uuid-validate": "0.0.1",
3032
"body-parser": "^1.19.0",
3133
"bson": "^4.0.2",
3234
"compression": "^1.7.4",
@@ -38,10 +40,12 @@
3840
"helmet": "^3.21.2",
3941
"http-status": "^1.4.2",
4042
"mandrill-api": "^1.0.45",
43+
"mongodb": "^3.5.2",
4144
"node-dependency-injection": "^2.4.2",
4245
"ts-node": "^8.3.0",
43-
"typescript": "^3.4.5",
46+
"typescript": "^3.7.2",
4447
"uuid": "^3.3.3",
48+
"uuid-validate": "0.0.3",
4549
"winston": "^3.2.1"
4650
},
4751
"devDependencies": {
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
import CourseRepository from '../domain/CourseRepository';
2-
import Course from '../domain/Course';
1+
import { CourseRepository } from '../domain/CourseRepository';
2+
import { Course } from '../domain/Course';
3+
import { CreateCourseRequest } from './CreateCourseRequest';
4+
import { CourseId } from '../../Shared/domain/Courses/CourseId';
5+
import { CourseName } from '../domain/CourseName';
6+
import { CourseDuration } from '../domain/CourseDuration';
7+
import { EventBus } from '../../../Shared/domain/EventBus';
38

4-
export default class CourseCreator {
9+
export class CourseCreator {
510
private repository: CourseRepository;
11+
private eventBus: EventBus;
612

7-
constructor(repository: CourseRepository) {
13+
constructor(repository: CourseRepository, eventBus: EventBus) {
814
this.repository = repository;
15+
this.eventBus = eventBus;
916
}
1017

11-
async run(id: string, name: string, duration: string): Promise<void> {
12-
const course = new Course(id, name, duration);
18+
async run(request: CreateCourseRequest): Promise<void> {
19+
const course = new Course(
20+
new CourseId(request.id),
21+
new CourseName(request.name),
22+
new CourseDuration(request.duration)
23+
);
1324

14-
return this.repository.save(course);
25+
await this.repository.save(course);
26+
27+
this.eventBus.publish(course.pullDomainEvents());
1528
}
1629
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type CreateCourseRequest = {
2+
id: string;
3+
name: string;
4+
duration: string;
5+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { DomainEvent } from '../../../Shared/domain/DomainEvent';
2+
3+
export abstract class AggregateRoot {
4+
private domainEvents: Array<DomainEvent>;
5+
6+
constructor() {
7+
this.domainEvents = [];
8+
}
9+
10+
pullDomainEvents(): Array<DomainEvent> {
11+
return this.domainEvents;
12+
}
13+
14+
record(event: DomainEvent): void {
15+
this.domainEvents.push(event);
16+
}
17+
18+
abstract toPrimitives(): any;
19+
}
Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1-
export default class Course {
2-
readonly id: string;
3-
readonly name: string;
4-
readonly duration: string;
1+
import { AggregateRoot } from './AggregateRoot';
2+
import { CourseCreatedDomainEvent } from './CourseCreatedDomainEvent';
3+
import { CourseName } from './CourseName';
4+
import { CourseDuration } from './CourseDuration';
5+
import { CourseId } from '../../Shared/domain/Courses/CourseId';
56

6-
constructor(id: string, name: string, duration: string) {
7+
export class Course extends AggregateRoot {
8+
readonly id: CourseId;
9+
readonly name: CourseName;
10+
readonly duration: CourseDuration;
11+
12+
constructor(id: CourseId, name: CourseName, duration: CourseDuration) {
13+
super();
714
this.id = id;
815
this.name = name;
916
this.duration = duration;
1017
}
18+
19+
static create(id: CourseId, name: CourseName, duration: CourseDuration): Course {
20+
const course = new Course(id, name, duration);
21+
22+
course.record(
23+
new CourseCreatedDomainEvent({
24+
id: course.id.value,
25+
duration: course.duration.value,
26+
name: course.name.value
27+
})
28+
);
29+
30+
return course;
31+
}
32+
33+
static fromPrimitives(plainData: any): Course {
34+
return new Course(
35+
new CourseId(plainData.id),
36+
new CourseName(plainData.name),
37+
new CourseDuration(plainData.duration)
38+
);
39+
}
40+
41+
toPrimitives(): any {
42+
return {
43+
id: this.id.value,
44+
name: this.name.value,
45+
duration: this.duration.value
46+
};
47+
}
1148
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export default class CourseAlreadyExists extends Error {
2-
constructor(courseId: string) {
3-
super(`Course ${courseId} already exists`);
1+
export class CourseAlreadyExists extends Error {
2+
constructor(id: string) {
3+
super(`Course ${id} already exists`);
44
}
55
}

0 commit comments

Comments
 (0)