Skip to content

Commit e214b63

Browse files
committed
added Joi validaion for lesson
1 parent 4c5ee10 commit e214b63

File tree

6 files changed

+111
-40
lines changed

6 files changed

+111
-40
lines changed

server/controllers/CoursesCtrl.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11

22
import { Router, Request, Response, NextFunction } from "express";
33
import CourseRepo from "./../repositories/CoursesRepo";
4-
54
import { apiErrorHandler } from "./../handlers/errorHandler";
65

76
export default class CoursesRoutes {
87

9-
constructor(router: Router) {
10-
router.get("/", this.getAllCourses.bind(this));
11-
router.get("/:id", this.getCourseDetails.bind(this));
8+
constructor() {
9+
1210
}
1311

14-
private getAllCourses(req: Request, res: Response, next: NextFunction) {
12+
getAllCourses(req: Request, res: Response, next: NextFunction) {
1513
CourseRepo.getAllCourses({ order: ["seqNo"] })
1614
.then((result) => res.json(result))
1715
.catch((err) => { apiErrorHandler(err, req, res, "Fetch All Courses failed."); });
1816
}
1917

20-
private getCourseDetails(req: Request, res: Response, next: NextFunction) {
18+
getCourseDetails(req: Request, res: Response, next: NextFunction) {
2119

2220
CourseRepo.getById(req.params.id)
2321
.then((result) => res.json(result))
24-
.catch((err) => { apiErrorHandler(err, req, res, "Course not found."); });
22+
.catch((err) => { apiErrorHandler(err, req, res, `Course ${req.params.id} not found.`); });
2523
}
26-
}
24+
}

server/controllers/LessonsCtrl.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
import { NextFunction, Request, Response, Router } from 'express';
22

3+
import { apiErrorHandler } from '../handlers/errorHandler';
34
import LessonRepo from '../repositories/LessonsRepo';
45

56
export default class LessonRoutes {
67

7-
constructor(router: Router) {
8-
router.post("/", this.createLesson.bind(this));
9-
router.put("/:id", this.updateLesson.bind(this));
10-
router.delete("/:id", this.deleteLesson.bind(this));
8+
constructor() { }
9+
10+
getAllLessons(req: Request, res: Response, next: NextFunction) {
11+
LessonRepo.getAllLessons({ order: ["seqNo"] })
12+
.then((result) => res.json(result))
13+
.catch((err) => { apiErrorHandler(err, req, res, "Fetch All Lessons failed."); });
14+
}
15+
16+
getLessonByCourse(req: Request, res: Response, next: NextFunction) {
17+
18+
LessonRepo.getLessonByCourse(req.params.id)
19+
.then((result) => res.json(result))
20+
.catch((err) => { apiErrorHandler(err, req, res, "Fetch All Lessons failed."); });
21+
}
22+
23+
getLessonById(req: Request, res: Response, next: NextFunction) {
24+
LessonRepo.getLessonById(req.params.id)
25+
.then((result) => res.json(result))
26+
.catch((err) => { apiErrorHandler(err, req, res, `Lesson ${req.params.id} not found.`); });
1127
}
1228

13-
private createLesson(req: Request, res: Response, next: NextFunction) {
14-
LessonRepo.createLesson(req.body)
29+
createLesson(req: Request, res: Response, next: NextFunction) {
30+
LessonRepo.createLesson(req['value']['body'])
1531
.then((result) => { res.json(result); })
16-
.catch((err) => { res.json({ error: err }); });
32+
.catch((err) => { apiErrorHandler(err, req, res, "Creation of Lesson failed."); });
1733
}
1834

19-
private updateLesson(req: Request, res: Response, next: NextFunction) {
20-
LessonRepo.updateLesson(req.params.id, req.body)
35+
updateLesson(req: Request, res: Response, next: NextFunction) {
36+
LessonRepo.updateLesson(req.params.id, req['value']['body'])
2137
.then((result) => { res.json(result); })
22-
.catch((err) => { res.json({ error: err }); });
38+
.catch((err) => { apiErrorHandler(err, req, res, `updation of Lesson ${req.params.id} failed.`); });
2339
}
2440

25-
private deleteLesson(req: Request, res: Response, next: NextFunction) {
41+
deleteLesson(req: Request, res: Response, next: NextFunction) {
2642
LessonRepo.deleteLesson(req.params.id)
2743
.then((result) => { res.json(result); })
28-
.catch((err) => { res.json({ error: err }); });
44+
.catch((err) => { apiErrorHandler(err, req, res, `deletion of Lesson ${req.params.id} failed.`); });
2945
}
30-
}
46+
}

server/repositories/CoursesRepo.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
2-
import { Lesson } from "./../models/Lesson";
3-
import { Course } from "./../models/Course";
1+
import { Course } from '../models/Course';
2+
import { Lesson } from '../models/Lesson';
43

54
class CourseRepo {
65

7-
constructor() {
8-
console.log("Courses Repo...");
9-
}
6+
constructor() { }
107

118
getAllCourses(options) {
129
return Course.findAll(options);

server/repositories/LessonsRepo.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
2-
import { Lesson } from "./../models/Lesson";
3-
import { Course } from "./../models/Course";
1+
import { Course } from '../models/Course';
2+
import { Lesson } from '../models/Lesson';
43

54
class LessonRepo {
65

7-
constructor() {
8-
console.log("Lessons Repo...");
6+
constructor() { }
7+
8+
getAllLessons(options) {
9+
return Lesson.findAll(options);
10+
}
11+
12+
getLessonById(id) {
13+
return Lesson.findById(id);
14+
}
15+
16+
getLessonByCourse(id) {
17+
return Lesson.findAll({ where: { 'courseId': id } });
918
}
1019

1120
createLesson(props: any) {
1221
return Lesson.create(props);
1322
}
23+
1424
updateLesson(id: Number, props: any) {
1525
return Lesson.update(props, { where: { id } });
1626
}
27+
1728
deleteLesson(id: Number) {
1829
return Lesson.destroy({ where: { id } });
1930
}

server/routes.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11

22
import { Application, Request, Response, NextFunction, Router } from "express";
3-
import Courses from "./controllers/CoursesCtrl";
4-
import Lessons from "./controllers/LessonsCtrl";
3+
import CoursesCtrl from "./controllers/CoursesCtrl";
4+
import LessonsCtrl from "./controllers/LessonsCtrl";
5+
import { LessonValidator, lessonSchema } from './validators/lessonValidator';
56

67
export default class Routes {
78

8-
public router: Router;
9+
lessonsCtrl = new LessonsCtrl();
10+
coursesCtrl = new CoursesCtrl();
11+
12+
lessonValidator = new LessonValidator();
913

1014
constructor(app: Application) {
1115

12-
this.router = Router();
13-
var coursesRoutes: Courses = new Courses(this.router);
14-
var lessonRoutes: Lessons = new Lessons(this.router);
16+
//course reoutes
17+
app.route("/api/courses/").get(this.coursesCtrl.getAllCourses);
18+
app.route("/api/courses/:id").get(this.coursesCtrl.getCourseDetails);
1519

16-
app.use("/api/lesson", this.router);
17-
app.use("/api/courses", this.router);
20+
// lesson routes
21+
app.route("/api/lessons").get(this.lessonsCtrl.getAllLessons);
22+
app.route("/api/lessons/course/:id").get(this.lessonsCtrl.getLessonByCourse);
23+
app.route("/api/lessons/:id").get(this.lessonsCtrl.getLessonById);
24+
app.route("/api/lessons").post(this.lessonValidator.validateBody(lessonSchema), this.lessonsCtrl.createLesson);
25+
app.route("/api/lessons/:id").put(this.lessonValidator.validateBody(lessonSchema), this.lessonsCtrl.updateLesson);
26+
app.route("/api/lessons/:id").delete(this.lessonsCtrl.deleteLesson);
1827
}
1928
}

server/validators/lessonValidator.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as Joi from 'joi';
2+
import { JoiObject } from 'joi';
3+
import { Router, Request, Response, NextFunction } from "express";
4+
5+
export class LessonValidator {
6+
7+
constructor() { }
8+
9+
validateBody(schema) {
10+
return (req: Request, res: Response, next: NextFunction) => {
11+
const result = Joi.validate(req.body, schema);
12+
13+
if (result.error) {
14+
return res.status(400).json(result.error);
15+
} else {
16+
if (!req['value']) {
17+
req['value'] = {};
18+
}
19+
if (!req['value']['body']) {
20+
req['value']['body'] = {};
21+
}
22+
req['value']['body'] = result.value;
23+
next();
24+
}
25+
}
26+
}
27+
}
28+
29+
export const lessonSchema = Joi.object().keys({
30+
courseId: Joi.number().integer().required(),
31+
url: Joi.string().trim().required(),
32+
gitHubUrl: Joi.string().trim().required(),
33+
tags: Joi.string().trim().required(),
34+
description: Joi.string().trim(),
35+
duration: Joi.string(),
36+
pro: Joi.boolean(),
37+
seqNo: Joi.number(),
38+
createdAt: Joi.date(),
39+
updatedAt: Joi.date()
40+
});

0 commit comments

Comments
 (0)