Skip to content

Commit 4800be9

Browse files
committed
Merge branch 'create_course' into part_one
2 parents 2b02fb5 + 7c74f5a commit 4800be9

File tree

18 files changed

+199
-37
lines changed

18 files changed

+199
-37
lines changed

cucumber.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let common = [
22
'tests/**/features/*.feature', // Specify our feature files
33
'--require-module ts-node/register', // Load TypeScript module
4-
'--require tests/**/features/*.steps.ts' // Load step definitions
4+
'--require tests/**/features/step_definitions/*.steps.ts' // Load step definitions
55
].join(' ');
66

77
module.exports = {

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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
import CourseRepository from '../domain/CourseRepository';
2+
import Course from '../domain/Course';
3+
14
export default class CreateCourse {
2-
async run(): Promise<void> {
3-
return null;
5+
private repository: CourseRepository;
6+
7+
constructor(repository: CourseRepository) {
8+
this.repository = repository;
9+
}
10+
11+
async run(id: string, name: string, duration: string): Promise<void> {
12+
const course = new Course(id, name, duration);
13+
14+
this.repository.save(course);
415
}
516
}

src/Mooc/Courses/domain/Course.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default class Course {
2+
private _id: string;
3+
private _name: string;
4+
private _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+
12+
get id(): string {
13+
return this._id;
14+
}
15+
16+
get name(): string {
17+
return this._name;
18+
}
19+
20+
get duration(): string {
21+
return this._duration;
22+
}
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Course from './Course';
2+
3+
export default interface CourseRepository {
4+
save(course: Course): Promise<void> | void;
5+
6+
search(id: string): Promise<Course> | Course;
7+
}
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+
}

src/apps/mooc_backend/config/dependency-injection/application.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
services:
2+
Mooc.courses.CourseRepository:
3+
class: ../../../../Mooc/Courses/infrastructure/FileCourseRepository
4+
arguments: []
5+
26
Mooc.courses.CreateCourse:
37
class: ../../../../Mooc/Courses/application/CreateCourse
4-
arguments: []
8+
arguments: ["@Mooc.courses.CourseRepository"]
59

610
Apps.mooc.controllers.CreateCourseController:
711
class: ../../controllers/CreateCourseController

src/apps/mooc_backend/controllers/CreateCourseController.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import CreateCourse from '../../../Mooc/Courses/application/CreateCourse';
33
import httpStatus from 'http-status';
44

55
export default class CreateCourseController {
6-
constructor(private createUser: CreateCourse) {}
6+
constructor(private createCourse: CreateCourse) {}
77

88
async create(req: Request, res: Response) {
9+
const id: string = req.params.id;
10+
const name: string = req.body.name;
11+
const duration: string = req.body.duration;
12+
913
try {
10-
await this.createUser.run();
14+
await this.createCourse.run(id, name, duration);
1115
} catch (e) {
1216
res.status(500).json(e);
1317
}

src/apps/mooc_backend/routes/create-course.route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import CreateCourseController from '../controllers/CreateCourseController';
44

55
export const createUserRoute = (app: Express) => {
66
const controller: CreateCourseController = container.get('Apps.mooc.controllers.CreateCourseController');
7-
app.post('/courses', (req, res) => controller.create(req, res));
7+
app.put('/courses/:id', (req, res) => controller.create(req, res));
88
};

0 commit comments

Comments
 (0)