Skip to content

Commit b14761c

Browse files
authored
Merge pull request #38 from CodelyTV/step-60661909
Refactoring to UUIDs
2 parents 37ee44b + 9ce2ce4 commit b14761c

File tree

11 files changed

+72
-41
lines changed

11 files changed

+72
-41
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@types/helmet": "0.0.44",
2828
"@types/node": "~13.1.1",
2929
"@types/uuid": "^3.4.6",
30+
"@types/uuid-validate": "0.0.1",
3031
"body-parser": "^1.19.0",
3132
"bson": "^4.0.2",
3233
"compression": "^1.7.4",
@@ -42,6 +43,7 @@
4243
"ts-node": "^8.3.0",
4344
"typescript": "^3.4.5",
4445
"uuid": "^3.3.3",
46+
"uuid-validate": "0.0.3",
4547
"winston": "^3.2.1"
4648
},
4749
"devDependencies": {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CourseRepository } from '../domain/CourseRepository';
22
import { Course } from '../domain/Course';
33
import { CreateCourseRequest } from './CreateCourseRequest';
4+
import { CourseId } from '../../Shared/domain/Courses/CourseId';
45

56
export class CourseCreator {
67
private repository: CourseRepository;
@@ -10,7 +11,7 @@ export class CourseCreator {
1011
}
1112

1213
async run(request: CreateCourseRequest): Promise<void> {
13-
const course = new Course(request.id, request.name, request.duration);
14+
const course = new Course(new CourseId(request.id), request.name, request.duration);
1415

1516
return this.repository.save(course);
1617
}

src/Contexts/Mooc/Courses/domain/Course.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { CourseId } from '../../Shared/domain/Courses/CourseId';
2+
13
export class Course {
2-
readonly id: string;
4+
readonly id: CourseId;
35
readonly name: string;
46
readonly duration: string;
57

6-
constructor(id: string, name: string, duration: string) {
8+
constructor(id: CourseId, name: string, duration: string) {
79
this.id = id;
810
this.name = name;
911
this.duration = duration;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Nullable } from '../../../Shared/domain/Nullable';
22
import { Course } from './Course';
3+
import { CourseId } from '../../Shared/domain/Courses/CourseId';
34

45
export interface CourseRepository {
56
save(course: Course): Promise<void>;
67

7-
search(id: string): Promise<Nullable<Course>>;
8+
search(id: CourseId): Promise<Nullable<Course>>;
89
}

src/Contexts/Mooc/Courses/infrastructure/FileCourseRepository.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ import { Course } from '../domain/Course';
33
import fs from 'fs';
44
import BSON from 'bson';
55
import { Nullable } from '../../../Shared/domain/Nullable';
6+
import { CourseId } from '../../Shared/domain/Courses/CourseId';
67

78
export class FileCourseRepository implements CourseRepository {
89
private FILE_PATH = `${__dirname}/courses`;
910

1011
async save(course: Course): Promise<void> {
11-
const filePath = this.filePath(course.id);
12+
const filePath = this.filePath(course.id.value);
1213
const data = BSON.serialize(course);
1314

1415
return fs.writeFileSync(filePath, data);
1516
}
1617

17-
async search(id: string): Promise<Nullable<Course>> {
18-
const filePath = this.filePath(id);
18+
async search(id: CourseId): Promise<Nullable<Course>> {
19+
const filePath = this.filePath(id.value);
1920
const exists = fs.existsSync(filePath);
2021

21-
return exists ? BSON.deserialize(fs.readFileSync(this.filePath(id))) : null;
22+
return exists ? BSON.deserialize(fs.readFileSync(this.filePath(id.value))) : null;
2223
}
2324

2425
private filePath(id: string): string {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Uuid } from '../../../../Shared/domain/value-object/Uuid';
2+
3+
export class CourseId extends Uuid {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class InvalidArgumentError extends Error {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import uuid from 'uuid/v4';
2+
import validate from 'uuid-validate';
3+
import { InvalidArgumentError } from './InvalidArgumentError';
4+
5+
export class Uuid {
6+
readonly value: string;
7+
8+
constructor(value: string) {
9+
this.ensureIsValidUuid(value);
10+
11+
this.value = value;
12+
}
13+
14+
static random(): Uuid {
15+
return new Uuid(uuid());
16+
}
17+
18+
private ensureIsValidUuid(id: string): void {
19+
if (!validate(id)) {
20+
throw new InvalidArgumentError(`<${this.constructor.name}> does not allow the value <${id}>`);
21+
}
22+
}
23+
24+
toString(): string {
25+
return this.value;
26+
}
27+
}

tests/Contexts/Mooc/Courses/application/CourseCreator.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course';
22
import { CourseCreator } from '../../../../../src/Contexts/Mooc/Courses/application/CourseCreator';
33
import { CourseRepository } from '../../../../../src/Contexts/Mooc/Courses/domain/CourseRepository';
4+
import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';
45

56
describe('Course Creator', () => {
67
it('should create a valid course', async () => {
@@ -12,11 +13,11 @@ describe('Course Creator', () => {
1213

1314
const createCourse = new CourseCreator(repository);
1415

15-
const id = 'some-id';
16+
const id = '0766c602-d4d4-48b6-9d50-d3253123275e';
1617
const name = 'some-name';
1718
const duration = 'some-duration';
1819

19-
const course = new Course(id, name, duration);
20+
const course = new Course(new CourseId(id), name, duration);
2021

2122
await createCourse.run({ id, name, duration });
2223

0 commit comments

Comments
 (0)