Skip to content

Commit 5246eda

Browse files
author
Roberto Sanchez
authored
Merge pull request #37 from talent-path-pipeline/s30-store-seed-data
S30 store seed data
2 parents 13eed03 + c9dea0e commit 5246eda

File tree

23 files changed

+1110
-215
lines changed

23 files changed

+1110
-215
lines changed

config/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const sequelize = new Sequelize(process.env.DATABASE_URL, {
3131
logging: false,
3232

3333
dialectOptions: {
34-
ssl: true,
34+
ssl: process.env.DB_SSL !== 'false',
3535
},
3636

3737
pool: {

controllers/courseController.js

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,86 @@
1-
const { Course } = require('../models/index');
1+
const { Course } = require('../models');
22
const ErrorWithHttpStatus = require('../utils/error.httpStatus.utils');
33

4-
exports.addCourse = (req, res) => {
5-
Course
6-
.create(req.body)
4+
// =================================================================
5+
// =================================================================
6+
// GET requests
7+
8+
exports.getCourses = (request, response, next) => {
9+
Course.findAll({ where: request.query })
710
.then(data => {
8-
res.status(200).send(data);
11+
if (!data || data.length === 0) {
12+
throw new ErrorWithHttpStatus('No courses found matching query', 404);
13+
}
14+
response.status(200).send(data);
915
})
10-
.catch(err => {
11-
res.status(400).send(err);
12-
});
13-
}
16+
.catch(next);
17+
};
1418

15-
exports.getCourses = (req, res) => {
16-
Course
17-
.findAll({
18-
where: req.query,
19-
})
19+
exports.getCourseById = (request, response, next) => {
20+
Course.findAll({ where: { uuid: request.params.id } })
2021
.then(data => {
21-
res.status(200).send(data);
22+
if (!data || data.length === 0) {
23+
throw new ErrorWithHttpStatus('No course found matching id', 404);
24+
}
25+
response.status(200).send(data[0]);
2226
})
23-
.catch(err => {
24-
res.status(400).send(err);
25-
})
26-
}
27+
.catch(next);
28+
};
2729

28-
exports.getCourseById = (req, res) => {
29-
Course
30-
.findAll({
31-
where: {
32-
uuid: req.params.id,
33-
},
34-
})
30+
exports.getCourseByPathId = (request, response, next) => {
31+
// TODO: maybe check if it's even a valid path id first?
32+
Course.findAll({ where: { pathUuid: request.params.id } })
3533
.then(data => {
36-
res.status(200).send(data);
34+
if (!data || data.length === 0) {
35+
throw new ErrorWithHttpStatus('No courses found related to given path', 404);
36+
}
37+
response.status(200).send(data);
3738
})
38-
.catch(err => {
39-
res.status(400).send(err);
40-
})
41-
}
39+
.catch(next);
40+
};
4241

43-
exports.deleteCourse = (req, res) => {
44-
Course
45-
.destroy({
46-
where: {
47-
uuid: req.params.id,
48-
},
49-
})
50-
.then(() => {
51-
res.send('Course deleted')
52-
})
53-
.catch(err => {
54-
res.status(400).send(err);
42+
// =================================================================
43+
// =================================================================
44+
// POST requests
45+
46+
exports.createCourse = (request, response, next) => {
47+
Course.create(request.body)
48+
.then(data => {
49+
response.status(201).send(data);
5550
})
56-
}
51+
.catch(next);
52+
};
5753

58-
exports.updateCourse = (req, res) => {
59-
if (!Object.keys(req.body).length) throw new ErrorWithHttpStatus('No body provided', 404);
54+
// =================================================================
55+
// =================================================================
56+
// PATCH requests
6057

61-
Course
62-
.update(req.body, {
63-
where: {
64-
uuid: req.params.id,
65-
},
66-
})
58+
exports.updateCourse = (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 course uuid', 403);
64+
}
65+
Course.update(request.body, { returning: true, where: { uuid: request.params.id } })
6766
.then(data => {
68-
const msg = data && data[0] ? 'Course updated' : 'No courses updated';
69-
res.status(200).send(msg);
67+
if (!data || data[0] === 0 || !data[1] || data[1].length === 0) {
68+
throw new ErrorWithHttpStatus('No courses updated', 400);
69+
}
70+
// returns updated object
71+
response.status(200).send(data[1][0]);
7072
})
71-
.catch(err => {
72-
res.status(400).send(err);
73+
.catch(next);
74+
};
75+
76+
// =================================================================
77+
// =================================================================
78+
// DELETE requests
79+
80+
exports.deleteCourse = (request, response, next) => {
81+
Course.destroy({ where: { uuid: request.params.id } })
82+
.then(() => {
83+
response.status(200).send(`Course ${request.params.id} deleted.`);
7384
})
74-
}
85+
.catch(next);
86+
};

controllers/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Controllers
3+
*/
4+
module.exports.lesson = require('./lessonController');
5+
module.exports.course = require('./courseController');
6+
module.exports.path = require('./pathController');
7+
module.exports.user = require('./userController');
8+
module.exports.nested_data = require('./nestedDataController');

controllers/lessonController.js

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,86 @@
1-
const { Lesson } = require('../models/index');
1+
const { Lesson } = require('../models');
22
const ErrorWithHttpStatus = require('../utils/error.httpStatus.utils');
33

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 })
710
.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);
915
})
10-
.catch(err => {
11-
res.status(400).send(err);
12-
});
13-
}
16+
.catch(next);
17+
};
1418

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 } })
2021
.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]);
2226
})
23-
.catch(err => {
24-
res.status(400).send(err);
25-
})
26-
}
27+
.catch(next);
28+
};
2729

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 } })
3533
.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);
3738
})
38-
.catch(err => {
39-
res.status(400).send(err);
40-
})
41-
}
39+
.catch(next);
40+
};
4241

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);
5550
})
56-
}
51+
.catch(next);
52+
};
5753

58-
exports.updateLesson = (req, res) => {
59-
if (!Object.keys(req.body).length) throw new ErrorWithHttpStatus('No body provided', 404);
54+
// =================================================================
55+
// =================================================================
56+
// PATCH requests
6057

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 } })
6766
.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]);
7072
})
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.`);
7384
})
74-
}
85+
.catch(next);
86+
};

0 commit comments

Comments
 (0)