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.9 kB · 109 lines
JavaScript
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110const asyncHandler = require('../middleware/async')const ErrorResponse = require('../utils/errorResponse')const Comment = require('../models/Comment')const Video = require('../models/Video')
// @desc Get comments// @route GET /api/v1/comments// @access Privateexports.getComments = asyncHandler(async (req, res, next) => { res.status(200).json(res.advancedResults)})
// @desc Get comments by video id// @route GET /api/v1/comments/:videoId/videos// @access Publicexports.getCommentByVideoId = asyncHandler(async (req, res, next) => { const comments = await Comment.find({ videoId: req.params.videoId }) .populate('userId') .populate('replies') .sort('-createdAt')
if (!comments) { return next( new ErrorResponse( `No comment with that video id of ${req.params.videoId}` ) ) }
res.status(200).json({ sucess: true, data: comments })})
// @desc Create comment// @route POST /api/v1/comments/// @access Privateexports.createComment = asyncHandler(async (req, res, next) => { console.log(req.body.videoId) let video = await Video.findOne({ _id: req.body.videoId, status: 'public' })
if (!video) { return next( new ErrorResponse(`No video with id of ${req.body.videoId}`, 404) ) } const comment = await Comment.create({ ...req.body, userId: req.user._id })
return res.status(200).json({ sucess: true, data: comment })})
// @desc Update comment// @route PUT /api/v1/comments/:id// @access Privateexports.updateComment = asyncHandler(async (req, res, next) => { let comment = await Comment.findById(req.params.id).populate('videoId')
if (!comment) { return next( new ErrorResponse(`No comment with id of ${req.params.id}`, 404) ) }
if ( comment.userId.toString() == req.user._id.toString() || comment.videoId.userId.toString() == req.user._id.toString() ) { comment = await Comment.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true })
res.status(200).json({ success: true, data: comment }) } else { return next( new ErrorResponse(`You are not authorized to update this comment`, 400) ) }})
// @desc Delete comment// @route DELETE /api/v1/comments/:id// @access Privateexports.deleteComment = asyncHandler(async (req, res, next) => { let comment = await Comment.findById(req.params.id).populate('videoId')
if (!comment) { return next( new ErrorResponse(`No comment with id of ${req.params.id}`, 404) ) }
if ( comment.userId.toString() == req.user._id.toString() || comment.videoId.userId.toString() == req.user._id.toString() ) { await comment.remove() } else { return next( new ErrorResponse(`You are not authorized to delete this comment`, 400) ) }
return res.status(200).json({ success: true, comment })})