Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 21 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/helmet": "0.0.44",
"@types/node": "~13.1.1",
"@types/uuid": "^3.4.6",
"@types/uuid-validate": "0.0.1",
"body-parser": "^1.19.0",
"bson": "^4.0.2",
"compression": "^1.7.4",
Expand All @@ -42,6 +43,7 @@
"ts-node": "^8.3.0",
"typescript": "^3.4.5",
"uuid": "^3.3.3",
"uuid-validate": "0.0.3",
"winston": "^3.2.1"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/Contexts/Mooc/Courses/application/CourseCreator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CourseRepository } from '../domain/CourseRepository';
import { Course } from '../domain/Course';
import { CreateCourseRequest } from './CreateCourseRequest';
import { CourseId } from '../../Shared/domain/Courses/CourseId';

export class CourseCreator {
private repository: CourseRepository;
Expand All @@ -10,7 +11,7 @@ export class CourseCreator {
}

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

return this.repository.save(course);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Contexts/Mooc/Courses/domain/Course.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CourseId } from '../../Shared/domain/Courses/CourseId';

export class Course {
readonly id: string;
readonly id: CourseId;
readonly name: string;
readonly duration: string;

constructor(id: string, name: string, duration: string) {
constructor(id: CourseId, name: string, duration: string) {
this.id = id;
this.name = name;
this.duration = duration;
Expand Down
3 changes: 2 additions & 1 deletion src/Contexts/Mooc/Courses/domain/CourseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Nullable } from '../../../Shared/domain/Nullable';
import { Course } from './Course';
import { CourseId } from '../../Shared/domain/Courses/CourseId';

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

search(id: string): Promise<Nullable<Course>>;
search(id: CourseId): Promise<Nullable<Course>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ import { Course } from '../domain/Course';
import fs from 'fs';
import BSON from 'bson';
import { Nullable } from '../../../Shared/domain/Nullable';
import { CourseId } from '../../Shared/domain/Courses/CourseId';

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

async save(course: Course): Promise<void> {
const filePath = this.filePath(course.id);
const filePath = this.filePath(course.id.value);
const data = BSON.serialize(course);

return fs.writeFileSync(filePath, data);
}

async search(id: string): Promise<Nullable<Course>> {
const filePath = this.filePath(id);
async search(id: CourseId): Promise<Nullable<Course>> {
const filePath = this.filePath(id.value);
const exists = fs.existsSync(filePath);

return exists ? BSON.deserialize(fs.readFileSync(this.filePath(id))) : null;
return exists ? BSON.deserialize(fs.readFileSync(this.filePath(id.value))) : null;
}

private filePath(id: string): string {
Expand Down
3 changes: 3 additions & 0 deletions src/Contexts/Mooc/Shared/domain/Courses/CourseId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Uuid } from '../../../../Shared/domain/value-object/Uuid';

export class CourseId extends Uuid {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class InvalidArgumentError extends Error {}
27 changes: 27 additions & 0 deletions src/Contexts/Shared/domain/value-object/Uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import uuid from 'uuid/v4';
import validate from 'uuid-validate';
import { InvalidArgumentError } from './InvalidArgumentError';

export class Uuid {
readonly value: string;

constructor(value: string) {
this.ensureIsValidUuid(value);

this.value = value;
}

static random(): Uuid {
return new Uuid(uuid());
}

private ensureIsValidUuid(id: string): void {
if (!validate(id)) {
throw new InvalidArgumentError(`<${this.constructor.name}> does not allow the value <${id}>`);
}
}

toString(): string {
return this.value;
}
}
5 changes: 3 additions & 2 deletions tests/Contexts/Mooc/Courses/application/CourseCreator.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course';
import { CourseCreator } from '../../../../../src/Contexts/Mooc/Courses/application/CourseCreator';
import { CourseRepository } from '../../../../../src/Contexts/Mooc/Courses/domain/CourseRepository';
import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';

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

const createCourse = new CourseCreator(repository);

const id = 'some-id';
const id = '0766c602-d4d4-48b6-9d50-d3253123275e';
const name = 'some-name';
const duration = 'some-duration';

const course = new Course(id, name, duration);
const course = new Course(new CourseId(id), name, duration);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { FileCourseRepository } from '../../../../../src/Contexts/Mooc/Courses/infrastructure/FileCourseRepository';
import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course';
import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';

describe('Save Course', () => {
it('should have a course', () => {
const repository = new FileCourseRepository();
const course = new Course('id', 'name', 'duration');
const course = new Course(new CourseId('0766c602-d4d4-48b6-9d50-d3253123275e'), 'name', 'duration');

repository.save(course);
});
Expand Down