|
1 | | -import { NextFunction, Request, Response, Router } from 'express'; |
| 1 | +import { Request, Response, NextFunction } from 'express'; |
2 | 2 | import { apiErrorHandler } from '../handlers/errorHandler'; |
3 | 3 | import LessonRepo from '../repositories/LessonsRepo'; |
4 | 4 |
|
5 | | -export default class LessonRoutes { |
6 | | - constructor() {} |
| 5 | +export default class LessonsCtrl { |
| 6 | + constructor() { } |
7 | 7 |
|
8 | | - getAllLessons(req: Request, res: Response, next: NextFunction) { |
9 | | - LessonRepo.getAllLessons({ order: ['id'] }) |
10 | | - .then(result => res.json(result)) |
11 | | - .catch(err => { |
12 | | - apiErrorHandler(err, req, res, 'Fetch All Lessons failed.'); |
13 | | - }); |
| 8 | + async getAllLessons(req: Request, res: Response, next: NextFunction) { |
| 9 | + try { |
| 10 | + const lessons = await LessonRepo.getAllLessons({ order: ['id'] }); |
| 11 | + res.json(lessons); |
| 12 | + } catch (error) { |
| 13 | + apiErrorHandler(error, req, res, 'Fetch All Lessons failed.'); |
| 14 | + } |
14 | 15 | } |
15 | 16 |
|
16 | | - getLessonByCourse(req: Request, res: Response, next: NextFunction) { |
17 | | - LessonRepo.getLessonByCourse(req.params.id) |
18 | | - .then(result => res.json(result)) |
19 | | - .catch(err => { |
20 | | - apiErrorHandler( |
21 | | - err, |
22 | | - req, |
23 | | - res, |
24 | | - `Lessons in course ${req.params.id} failed.`, |
25 | | - ); |
26 | | - }); |
| 17 | + async getLessonByCourse(req: Request, res: Response, next: NextFunction) { |
| 18 | + try { |
| 19 | + const lesson = await LessonRepo.getLessonByCourse(req.params.id); |
| 20 | + res.json(lesson); |
| 21 | + } catch (error) { |
| 22 | + apiErrorHandler( |
| 23 | + error, |
| 24 | + req, |
| 25 | + res, |
| 26 | + `Lessons in course ${req.params.id} failed.`, |
| 27 | + ); |
| 28 | + } |
27 | 29 | } |
28 | 30 |
|
29 | | - getLessonById(req: Request, res: any, next: NextFunction) { |
30 | | - LessonRepo.getLessonById(req.params.id) |
31 | | - .then(result => { |
32 | | - if (result) { |
33 | | - return res.json(result); |
34 | | - } else { |
35 | | - res.status(404).send(`Lesson ${req.params.id} not found.`); |
36 | | - } |
37 | | - }) |
38 | | - .catch(err => { |
39 | | - apiErrorHandler(err, req, res, `Lesson ${req.params.id} failed.`); |
40 | | - }); |
| 31 | + async getLessonById(req: Request, res: any, next: NextFunction) { |
| 32 | + try { |
| 33 | + const result = await LessonRepo.getLessonById(req.params.id); |
| 34 | + if (result) { |
| 35 | + return res.json(result); |
| 36 | + } else { |
| 37 | + res.status(404).send(`Lesson ${req.params.id} not found.`); |
| 38 | + } |
| 39 | + } catch (error) { |
| 40 | + apiErrorHandler(error, req, res, `Lesson ${req.params.id} failed.`); |
| 41 | + } |
41 | 42 | } |
42 | 43 |
|
43 | | - createLesson(req: Request, res: Response, next: NextFunction) { |
44 | | - LessonRepo.createLesson(req['value']['body']) |
45 | | - .then(result => res.json(result)) |
46 | | - .catch(err => { |
47 | | - apiErrorHandler(err, req, res, 'Creation of Lesson failed.'); |
48 | | - }); |
| 44 | + async createLesson(req: Request, res: Response, next: NextFunction) { |
| 45 | + try { |
| 46 | + const result = await LessonRepo.createLesson(req['value']['body']); |
| 47 | + res.json(result); |
| 48 | + } catch (error) { |
| 49 | + apiErrorHandler(error, req, res, 'Creation of Lesson failed.'); |
| 50 | + } |
49 | 51 | } |
50 | 52 |
|
51 | | - updateLesson(req: Request, res: Response, next: NextFunction) { |
| 53 | + async updateLesson(req: Request, res: Response, next: NextFunction) { |
52 | 54 | const id = parseInt(req.params.id); |
53 | | - LessonRepo.updateLesson(id, req['value']['body']) |
54 | | - .then(result => res.json(result)) |
55 | | - .catch(err => { |
56 | | - apiErrorHandler( |
57 | | - err, |
58 | | - req, |
59 | | - res, |
60 | | - `updation of Lesson ${req.params.id} is failed.`, |
61 | | - ); |
62 | | - }); |
| 55 | + try { |
| 56 | + const result = await LessonRepo.updateLesson(id, req['value']['body']); |
| 57 | + res.json(result); |
| 58 | + } catch (error) { |
| 59 | + apiErrorHandler( |
| 60 | + error, |
| 61 | + req, |
| 62 | + res, |
| 63 | + `updation of Lesson ${req.params.id} is failed.`, |
| 64 | + ); |
| 65 | + } |
63 | 66 | } |
64 | 67 |
|
65 | | - deleteLesson(req: Request, res: Response, next: NextFunction) { |
| 68 | + async deleteLesson(req: Request, res: Response, next: NextFunction) { |
66 | 69 | const id = parseInt(req.params.id); |
67 | | - LessonRepo.deleteLesson(id) |
68 | | - .then(result => res.json(result)) |
69 | | - .catch(err => { |
70 | | - apiErrorHandler( |
71 | | - err, |
72 | | - req, |
73 | | - res, |
74 | | - `deletion of Lesson ${req.params.id} is failed.`, |
75 | | - ); |
76 | | - }); |
| 70 | + try { |
| 71 | + const result = await LessonRepo.deleteLesson(id); |
| 72 | + res.json(result); |
| 73 | + } catch (error) { |
| 74 | + apiErrorHandler( |
| 75 | + error, |
| 76 | + req, |
| 77 | + res, |
| 78 | + `deletion of Lesson ${req.params.id} is failed.`, |
| 79 | + ); |
| 80 | + } |
77 | 81 | } |
78 | 82 | } |
0 commit comments