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.
1.9 kB · 66 lines
JavaScript
at master
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667const asyncHandler = require('../middleware/async')const ErrorResponse = require('../utils/errorResponse')const User = require('../models/User')
// @desc Get all users// @route GET /api/v1/auth/users// @access Private/Adminexports.getUsers = asyncHandler(async (req, res, next) => { res.status(200).json(res.advancedResults)})
// @desc Get single user// @route GET /api/v1/auth/users/:id// @access Private/Adminexports.getUser = asyncHandler(async (req, res, next) => { const user = await User.findById(req.params.id).populate({ path: 'subscribers' })
if (!user) return next(new ErrorResponse(`No user with that id of ${req.params.id}`))
res.status(200).json({ success: true, data: user })})
// @desc Create user// @route POST /api/v1/auth/users// @access Private/Adminexports.createUser = asyncHandler(async (req, res, next) => { const user = await User.create(req.body)
res.status(201).json({ success: true, data: user })})
// @desc Update user// @route PUT /api/v1/auth/users/:id// @access Private/Adminexports.updateUser = asyncHandler(async (req, res, next) => { req.body.password = '' delete req.body.password
const user = await User.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true, context: 'query' })
if (!user) return next(new ErrorResponse(`No user with that id of ${req.params.id}`))
res.status(200).json({ success: true, data: user })})
// @desc Delete user// @route DELETE /api/v1/auth/users/:id// @access Private/Adminexports.deleteUser = asyncHandler(async (req, res, next) => { const user = await User.findById(req.params.id)
if (!user) return next(new ErrorResponse(`No user with that id of ${req.params.id}`))
await User.findByIdAndDelete(req.params.id)
res.status(200).json({ success: true, data: {} })})