Skip to content

Commit 42e6b42

Browse files
authored
Merge pull request #6 from CodelyTV/part-one-refactor
Part one refactor
2 parents f32a6da + 7cd747e commit 42e6b42

File tree

25 files changed

+159
-115
lines changed

25 files changed

+159
-115
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
dist/
33
.tmp
4+
src/Contexts/Mooc/Courses/infrastructure/courses.*

package-lock.json

Lines changed: 23 additions & 3 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
@@ -23,6 +23,7 @@
2323
"@types/convict": "^4.2.1",
2424
"@types/errorhandler": "0.0.32",
2525
"@types/express": "^4.17.1",
26+
"@types/glob": "^7.1.1",
2627
"@types/helmet": "0.0.44",
2728
"@types/node": "~11.13.0",
2829
"@types/uuid": "^3.4.5",
@@ -33,6 +34,7 @@
3334
"copy": "^0.3.2",
3435
"errorhandler": "^1.5.1",
3536
"express": "^4.17.1",
37+
"glob": "^7.1.6",
3638
"helmet": "^3.21.1",
3739
"http-status": "^1.3.2",
3840
"mandrill-api": "^1.0.45",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import CourseRepository from '../domain/CourseRepository';
22
import Course from '../domain/Course';
33

4-
export default class CreateCourse {
4+
export default class CourseCreator {
55
private repository: CourseRepository;
66

77
constructor(repository: CourseRepository) {
@@ -11,6 +11,6 @@ export default class CreateCourse {
1111
async run(id: string, name: string, duration: string): Promise<void> {
1212
const course = new Course(id, name, duration);
1313

14-
this.repository.save(course);
14+
return this.repository.save(course);
1515
}
1616
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default class Course {
2+
readonly id: string;
3+
readonly name: string;
4+
readonly duration: string;
5+
6+
constructor(id: string, name: string, duration: string) {
7+
this.id = id;
8+
this.name = name;
9+
this.duration = duration;
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default class CourseAlreadyExists extends Error {
2+
constructor(courseId: string) {
3+
super(`Course ${courseId} already exists`);
4+
}
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Course from './Course';
2+
import { Nullable } from '../../../Shared/domain/Nullable';
3+
4+
export default interface CourseRepository {
5+
save(course: Course): Promise<void>;
6+
7+
search(id: string): Promise<Nullable<Course>>;
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import CourseRepository from '../domain/CourseRepository';
2+
import Course from '../domain/Course';
3+
import fs from 'fs';
4+
import BSON from 'bson';
5+
import { Nullable } from '../../../Shared/domain/Nullable';
6+
7+
export default class FileCourseRepository implements CourseRepository {
8+
private FILE_PATH = `${__dirname}/courses`;
9+
10+
async save(course: Course): Promise<void> {
11+
const filePath = this.filePath(course.id);
12+
const data = BSON.serialize(course);
13+
14+
return fs.writeFileSync(filePath, data);
15+
}
16+
17+
async search(id: string): Promise<Nullable<Course>> {
18+
const filePath = this.filePath(id);
19+
const exists = fs.existsSync(filePath);
20+
21+
return exists ? BSON.deserialize(fs.readFileSync(this.filePath(id))) : null;
22+
}
23+
24+
private filePath(id: string): string {
25+
return `${this.FILE_PATH}.${id}.repo`;
26+
}
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Nullable<T> = T | null;

src/Mooc/Courses/domain/Course.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)