This repository was archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourses.js
143 lines (122 loc) · 3.42 KB
/
courses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const ErrorResponse = require("../utils/errorResponse");
const asyncHandler = require("../middleware/async");
const Course = require("../models/Course");
const Bootcamp = require("../models/Bootcamp");
// @desc Get courses
// @route GET /api/v1/courses
// @route GET /api/v1/bootcamps/:bootcampId/courses
// @access Public
exports.getCourses = asyncHandler(async (req, res, next) => {
if (req.params.bootcampId) {
const courses = await Course.find({ bootcamp: req.params.bootcampId });
return res.status(200).json({
success: true,
count: courses.length,
data: courses,
});
} else {
res.status(200).json(res.advancedResults);
}
});
// @desc Get single course
// @route GET /api/v1/courses/:id
// @access Public
exports.getCourse = asyncHandler(async (req, res, next) => {
const course = await Course.findById(req.params.id).populate({
path: "bootcamp",
select: "name description",
});
if (!course) {
return next(
new ErrorResponse(`No course with the id of ${req.params.id}`),
404
);
}
res.status(200).json({
success: true,
data: course,
});
});
// @desc Add course
// @route POST /api/v1/bootcamps/:bootcampId/courses
// @access Private
exports.addCourse = asyncHandler(async (req, res, next) => {
req.body.bootcamp = req.params.bootcampId;
req.body.user = req.user.id;
const bootcamp = await Bootcamp.findById(req.params.bootcampId);
if (!bootcamp) {
return next(
new ErrorResponse(`No bootcamp with the id of ${req.params.bootcampId}`),
404
);
}
// Make sure user is bootcamp owner
if (bootcamp.user.toString() !== req.user.id && req.user.role !== "admin") {
return next(
new ErrorResponse(
`User ${req.user.id} is not authorized to add a course to bootcamp ${bootcamp._id}`,
401
)
);
}
const course = await Course.create(req.body);
res.status(200).json({
success: true,
data: course,
});
});
// @desc Update course
// @route PUT /api/v1/courses/:id
// @access Private
exports.updateCourse = asyncHandler(async (req, res, next) => {
let course = await Course.findById(req.params.id);
if (!course) {
return next(
new ErrorResponse(`No course with the id of ${req.params.id}`),
404
);
}
// Make sure user is course owner
if (course.user.toString() !== req.user.id && req.user.role !== "admin") {
return next(
new ErrorResponse(
`User ${req.user.id} is not authorized to update course ${course._id}`,
401
)
);
}
course = await Course.findByIdAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true,
});
res.status(200).json({
success: true,
data: course,
});
});
// @desc Delete course
// @route DELETE /api/v1/courses/:id
// @access Private
exports.deleteCourse = asyncHandler(async (req, res, next) => {
const course = await Course.findById(req.params.id);
if (!course) {
return next(
new ErrorResponse(`No course with the id of ${req.params.id}`),
404
);
}
// Make sure user is course owner
if (course.user.toString() !== req.user.id && req.user.role !== "admin") {
return next(
new ErrorResponse(
`User ${req.user.id} is not authorized to delete course ${course._id}`,
401
)
);
}
await course.remove();
res.status(200).json({
success: true,
data: {},
});
});