Skip to content

Commit c0ed341

Browse files
committed
Implement file repository
1 parent 288e6fc commit c0ed341

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"test": "NODE_ENV=test jest",
1111
"test:unit": "NODE_ENV=test jest --watch",
1212
"test:features": "./node_modules/.bin/cucumber-js -p default",
13-
"start": "NODE_ENV=production node dist/src/apps/mooc/server",
13+
"start": "NODE_ENV=production node dist/src/apps/mooc_backend/server",
1414
"build": "npm run build:clean && npm run build:tsc && npm run build:di",
1515
"build:tsc": "tsc -p tsconfig.prod.json",
1616
"build:di": "copy 'src/**/*.yaml' dist/src",
1717
"build:clean": "rm -r dist; exit 0"
1818
},
1919
"dependencies": {
20+
"@types/bson": "^4.0.0",
2021
"@types/compression": "^1.0.1",
2122
"@types/convict": "^4.2.1",
2223
"@types/errorhandler": "0.0.32",
@@ -25,6 +26,7 @@
2526
"@types/node": "~11.13.0",
2627
"@types/uuid": "^3.4.5",
2728
"body-parser": "^1.19.0",
29+
"bson": "^4.0.2",
2830
"compression": "^1.7.4",
2931
"convict": "^5.1.0",
3032
"copy": "^0.3.2",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import CourseRepository from '../domain/CourseRepository';
2+
import Course from '../domain/Course';
3+
import * as fs from 'fs';
4+
import BSON from 'bson';
5+
6+
export default class FileCourseRepository implements CourseRepository {
7+
private FILE_PATH = `${__dirname}/courses`;
8+
9+
save(course: Course): void | Promise<void> {
10+
fs.writeFileSync(this.filePath(course.id), BSON.serialize(course));
11+
}
12+
13+
search(id: string): Course {
14+
return fs.existsSync(this.filePath(id)) ? BSON.deserialize(fs.readFileSync(this.filePath(id))) : null;
15+
}
16+
17+
private filePath(id: string): string {
18+
return `${this.FILE_PATH}.${id}.repo`;
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Course from '../../../../src/Mooc/Courses/domain/Course';
2+
import FileCourseRepository from '../../../../src/Mooc/Courses/infrastructure/FileCourseRepository';
3+
4+
describe('Save Course', () => {
5+
it('should have a course', () => {
6+
const repository = new FileCourseRepository();
7+
const course = new Course('id', 'name', 'duration');
8+
9+
repository.save(course);
10+
});
11+
});

0 commit comments

Comments
 (0)