Something went wrong. Try again.
VueTube is a YouTube clone built with nodejs, expressjs & mongodb. This is the RESTful API repository.
Something went wrong. Try again.
2.0 kB · 72 lines
JavaScript
at master
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273const asyncHandler = require('../middleware/async')const ErrorResponse = require('../utils/errorResponse')const Category = require('../models/Category')
// @desc Get categories// @route GET /api/v1/categories// @access Private/Adminexports.getCategories = asyncHandler(async (req, res, next) => { res.status(200).json(res.advancedResults)})
// @desc Get single category// @route GET /api/v1/categories/:id// @access Private/Adminexports.getCategory = asyncHandler(async (req, res, next) => { const category = await Category.findById(req.params.id)
if (!category) { return next( new ErrorResponse(`No category with that id of ${req.params.id}`) ) }
res.status(200).json({ sucess: true, data: category })})
// @desc Create Category// @route POST /api/v1/categories/// @access Private/Adminexports.createCategory = asyncHandler(async (req, res, next) => { const category = await Category.create({ ...req.body, userId: req.user.id })
return res.status(200).json({ sucess: true, data: category })})
// @desc Update category// @route PUT /api/v1/categories// @access Private/Adminexports.updateCategory = asyncHandler(async (req, res, next) => { const category = await Category.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true, context: 'query' })
if (!category) return next( new ErrorResponse(`No category with that id of ${req.params.id}`) )
res.status(200).json({ success: true, data: category })})
// @desc Delete Category// @route DELETE /api/v1/categories/:id// @access Private/Adminexports.deleteCategory = asyncHandler(async (req, res, next) => { let category = await Category.findById(req.params.id)
if (!category) { return next( new ErrorResponse(`No category with id of ${req.params.id}`, 404) ) }
await category.remove()
return res.status(200).json({ success: true, category })})