|
1 | | -const { Lesson } = require('../models/index'); |
| 1 | +const { Lesson } = require('../models'); |
2 | 2 | const ErrorWithHttpStatus = require('../utils/error.httpStatus.utils'); |
3 | 3 |
|
4 | | -exports.addLesson = (req, res) => { |
5 | | - Lesson |
6 | | - .create(req.body) |
| 4 | +// ================================================================= |
| 5 | +// ================================================================= |
| 6 | +// GET requests |
| 7 | + |
| 8 | +exports.getLessons = (request, response, next) => { |
| 9 | + Lesson.findAll({ where: request.query }) |
7 | 10 | .then(data => { |
8 | | - res.status(200).send(data); |
| 11 | + if (!data || data.length === 0) { |
| 12 | + throw new ErrorWithHttpStatus('No lessons found matching query', 404); |
| 13 | + } |
| 14 | + response.status(200).send(data); |
9 | 15 | }) |
10 | | - .catch(err => { |
11 | | - res.status(400).send(err); |
12 | | - }); |
13 | | -} |
| 16 | + .catch(next); |
| 17 | +}; |
14 | 18 |
|
15 | | -exports.getLessons = (req, res) => { |
16 | | - Lesson |
17 | | - .findAll({ |
18 | | - where: req.query, |
19 | | - }) |
| 19 | +exports.getLessonById = (request, response, next) => { |
| 20 | + Lesson.findAll({ where: { uuid: request.params.id } }) |
20 | 21 | .then(data => { |
21 | | - res.status(200).send(data); |
| 22 | + if (!data || data.length === 0) { |
| 23 | + throw new ErrorWithHttpStatus('No lesson found matching id', 404); |
| 24 | + } |
| 25 | + response.status(200).send(data[0]); |
22 | 26 | }) |
23 | | - .catch(err => { |
24 | | - res.status(400).send(err); |
25 | | - }) |
26 | | -} |
| 27 | + .catch(next); |
| 28 | +}; |
27 | 29 |
|
28 | | -exports.getLessonById = (req, res) => { |
29 | | - Lesson |
30 | | - .findAll({ |
31 | | - where: { |
32 | | - uuid: req.params.id, |
33 | | - }, |
34 | | - }) |
| 30 | +exports.getLessonByCourseId = (request, response, next) => { |
| 31 | + // TODO: maybe check if it's even a valid course id first? |
| 32 | + Lesson.findAll({ where: { courseUuid: request.params.id } }) |
35 | 33 | .then(data => { |
36 | | - res.status(200).send(data); |
| 34 | + if (!data || data.length === 0) { |
| 35 | + throw new ErrorWithHttpStatus('No lessons found related to given course', 404); |
| 36 | + } |
| 37 | + response.status(200).send(data); |
37 | 38 | }) |
38 | | - .catch(err => { |
39 | | - res.status(400).send(err); |
40 | | - }) |
41 | | -} |
| 39 | + .catch(next); |
| 40 | +}; |
42 | 41 |
|
43 | | -exports.deleteLesson = (req, res) => { |
44 | | - Lesson |
45 | | - .destroy({ |
46 | | - where: { |
47 | | - uuid: req.params.id, |
48 | | - }, |
49 | | - }) |
50 | | - .then(() => { |
51 | | - res.send('Lesson deleted') |
52 | | - }) |
53 | | - .catch(err => { |
54 | | - res.status(400).send(err); |
| 42 | +// ================================================================= |
| 43 | +// ================================================================= |
| 44 | +// POST requests |
| 45 | + |
| 46 | +exports.createLesson = (request, response, next) => { |
| 47 | + Lesson.create(request.body) |
| 48 | + .then(data => { |
| 49 | + response.status(201).send(data); |
55 | 50 | }) |
56 | | -} |
| 51 | + .catch(next); |
| 52 | +}; |
57 | 53 |
|
58 | | -exports.updateLesson = (req, res) => { |
59 | | - if (!Object.keys(req.body).length) throw new ErrorWithHttpStatus('No body provided', 404); |
| 54 | +// ================================================================= |
| 55 | +// ================================================================= |
| 56 | +// PATCH requests |
60 | 57 |
|
61 | | - Lesson |
62 | | - .update(req.body, { |
63 | | - where: { |
64 | | - uuid: req.params.id, |
65 | | - }, |
66 | | - }) |
| 58 | +exports.updateLesson = (request, response, next) => { |
| 59 | + if (!Object.keys(request.body).length) { |
| 60 | + throw new ErrorWithHttpStatus('No body provided', 400); |
| 61 | + } |
| 62 | + if (request.body.uuid !== undefined) { |
| 63 | + throw new ErrorWithHttpStatus('Cannot change lesson uuid', 403); |
| 64 | + } |
| 65 | + Lesson.update(request.body, { returning: true, where: { uuid: request.params.id } }) |
67 | 66 | .then(data => { |
68 | | - const msg = data && data[0] ? 'Lesson updated' : 'No lessons updated'; |
69 | | - res.status(200).send(msg); |
| 67 | + if (!data || data[0] === 0 || !data[1] || data[1].length === 0) { |
| 68 | + throw new ErrorWithHttpStatus('No lessons updated', 400); |
| 69 | + } |
| 70 | + // returns updated object |
| 71 | + response.status(200).send(data[1][0]); |
70 | 72 | }) |
71 | | - .catch(err => { |
72 | | - res.status(400).send(err); |
| 73 | + .catch(next); |
| 74 | +}; |
| 75 | + |
| 76 | +// ================================================================= |
| 77 | +// ================================================================= |
| 78 | +// DELETE requests |
| 79 | + |
| 80 | +exports.deleteLesson = (request, response, next) => { |
| 81 | + Lesson.destroy({ where: { uuid: request.params.id } }) |
| 82 | + .then(() => { |
| 83 | + response.status(200).send(`Lesson ${request.params.id} deleted.`); |
73 | 84 | }) |
74 | | -} |
| 85 | + .catch(next); |
| 86 | +}; |
0 commit comments