-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
takuyadev
committed
Nov 3, 2022
1 parent
73b2cb1
commit 9f38b1c
Showing
12 changed files
with
397 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const Courses = require('../model/Courses') | ||
const asyncHandler = require('../middleware/async'); | ||
const ErrorResponse = require('../utils/errorResponse'); | ||
const Bootcamp = require('../model/Bootcamp'); | ||
|
||
// @desc Get all bootcamps | ||
// @route GET /api/v1/courses | ||
// @route GET /api/v1/bootcamps/:bootcampId/courses | ||
// @access Public | ||
exports.getCourses = asyncHandler(async (req, res, next) => { | ||
|
||
// If parameter is filled out | ||
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) | ||
} | ||
|
||
const courses = await query; | ||
|
||
res.status(200).json({ | ||
succes: true, | ||
count: courses.length, | ||
data: courses | ||
}) | ||
}) | ||
|
||
|
||
// @desc Get single course | ||
// @route GET /api/v1/courses | ||
// @route GET /api/v1/courses/:id | ||
// @access Public | ||
exports.getCourse = asyncHandler(async (req, res, next) => { | ||
const course = await Courses.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({ | ||
succes: true, | ||
data: course | ||
}) | ||
}) | ||
|
||
// @desc Add course | ||
// @route GET /api/v1/bootcamps/:bootcampId/courses | ||
// @access Private | ||
exports.addCourse = asyncHandler(async (req, res, next) => { | ||
req.body.bootcamp = req.params.bootcampId; | ||
|
||
const bootcamp = await Bootcamp.findById(req.params.bootcampId) | ||
|
||
if (!bootcamp) { | ||
return next(new ErrorResponse(`No bootcamp with the id of ${req.params.bootcampId}`), 404) | ||
} | ||
|
||
const course = await Courses.create(req.body) | ||
|
||
res.status(200).json({ | ||
succes: true, | ||
data: course | ||
}) | ||
}) | ||
|
||
// @desc Update course | ||
// @route PUT /api/v1/courses/:id | ||
// @access Private | ||
exports.updateCourse = asyncHandler(async (req, res, next) => { | ||
let course = await Courses.findById(req.params.id) | ||
|
||
if (!course) { | ||
return next(new ErrorResponse(`No course with the id of ${req.params.id}`), 404) | ||
} | ||
|
||
course = await Courses.findByIdAndUpdate(req.params.id, req.body, { | ||
new: true, | ||
runValidators: true | ||
}) | ||
|
||
res.status(200).json({ | ||
succes: true, | ||
data: course | ||
}) | ||
}) | ||
|
||
// @desc Delete course | ||
// @route DELETE /api/v1/courses/:id | ||
// @access Private | ||
exports.deleteCourse = asyncHandler(async (req, res, next) => { | ||
const course = await Courses.findById(req.params.id) | ||
|
||
if (!course) { | ||
return next(new ErrorResponse(`No course with the id of ${req.params.id}`), 404) | ||
} | ||
|
||
await course.remove() | ||
|
||
res.status(200).json({ | ||
succes: true, | ||
data: {} | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const advancedResults = (model, populate) => async (req, res, next) => { | ||
let query; | ||
|
||
// Copy req.query | ||
const reqQuery = { ...req.query }; | ||
|
||
// Fields to exclude | ||
const removeFields = ['select', 'sort', 'page', 'limit']; | ||
|
||
// Loop over removeFields and delete them from reqQuery | ||
removeFields.forEach(param => delete reqQuery[param]); | ||
|
||
// Create query string | ||
let queryStr = JSON.stringify(reqQuery); | ||
|
||
// Create operators ($gt, $gte, etc) | ||
queryStr = queryStr.replace(/\b(gt|gte|lt|lte|in)\b/g, match => `$${match}`); | ||
|
||
// Finding resource | ||
query = model.find(JSON.parse(queryStr)); | ||
|
||
// Select Fields | ||
if (req.query.select) { | ||
const fields = req.query.select.split(',').join(' '); | ||
query = query.select(fields); | ||
} | ||
|
||
// Sort | ||
if (req.query.sort) { | ||
const sortBy = req.query.sort.split(',').join(' '); | ||
query = query.sort(sortBy); | ||
} else { | ||
query = query.sort('-createdAt'); | ||
} | ||
|
||
// Pagination | ||
const page = parseInt(req.query.page, 10) || 1; | ||
const limit = parseInt(req.query.limit, 10) || 25; | ||
const startIndex = (page - 1) * limit; | ||
const endIndex = page * limit; | ||
const total = await model.countDocuments(JSON.parse(queryStr)); | ||
|
||
query = query.skip(startIndex).limit(limit); | ||
|
||
if (populate) { | ||
query = query.populate(populate); | ||
} | ||
|
||
// Executing query | ||
const results = await query; | ||
|
||
// Pagination result | ||
const pagination = {}; | ||
|
||
if (endIndex < total) { | ||
pagination.next = { | ||
page: page + 1, | ||
limit | ||
}; | ||
} | ||
|
||
if (startIndex > 0) { | ||
pagination.prev = { | ||
page: page - 1, | ||
limit | ||
}; | ||
} | ||
|
||
res.advancedResults = { | ||
success: true, | ||
count: results.length, | ||
pagination, | ||
data: results | ||
}; | ||
|
||
next(); | ||
}; | ||
|
||
module.exports = advancedResults; |
Oops, something went wrong.