Skip to content

Commit a43eef8

Browse files
committed
Set as async the interfaces that will be async in the future implementations. Add Nullable type
1 parent d3d1c39 commit a43eef8

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Course from './Course';
2+
import { Nullable } from '../../../Shared/domain/Nullable';
23

34
export default interface CourseRepository {
4-
save(course: Course): Promise<void> | void;
5+
save(course: Course): Promise<void>;
56

6-
search(id: string): Promise<Course> | Course;
7+
search(id: string): Promise<Nullable<Course>>;
78
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import CourseRepository from '../domain/CourseRepository';
22
import Course from '../domain/Course';
3-
import * as fs from 'fs';
3+
import fs from 'fs';
44
import BSON from 'bson';
5+
import { Nullable } from '../../../Shared/domain/Nullable';
56

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

9-
save(course: Course): void | Promise<void> {
10-
fs.writeFileSync(this.filePath(course.id), BSON.serialize(course));
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);
1115
}
1216

13-
search(id: string): Course {
14-
return fs.existsSync(this.filePath(id)) ? BSON.deserialize(fs.readFileSync(this.filePath(id))) : null;
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;
1522
}
1623

1724
private filePath(id: string): string {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Nullable<T> = T | null;

0 commit comments

Comments
 (0)