Skip to content

Commit

Permalink
Implement calculate total views
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyendacthienngan committed Dec 31, 2021
1 parent cc7d751 commit c86acb1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
44 changes: 26 additions & 18 deletions controllers/video-info.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,21 @@ exports.search = function (req, res) {
})
};

exports.updateVideoInfo = function (req, res) {
exports.updateVideoInfo = async function (req, res) {
const body = req.body
const id = body.id;
Video.findById(id)
.exec(function(err, video) {
if (err) {
console.error('error, no entry found');
res.send(err)
}
else {
video.title = body.title ? body.title : video.title;
video.description = body.description ? body.description : video.description;
video.url = body.url ? body.url : video.url;
video.size = body.size ? body.size : video.size;
video.visibility = body.visibility ? body.visibility : video.visibility;
video.save();
res.send(video)
}
})
try {
const video = await Video.updateOne({ _id: body.id }, {
total_views: body.totalViews,
title : body.title,
description : body.description,
url : body.url,
size : body.size,
visibility : body.visibility
});
res.send(video);
} catch (error) {
res.send(error);
}
}

exports.deleteVideoInfo = async function (req, res) {
Expand All @@ -112,4 +108,16 @@ exports.deleteVideoInfo = async function (req, res) {
} catch (error) {
res.send(error);
}
}

exports.getTotalViewsByVideoId = async function(req, res) {
const id = req.params.id
try {
const video = await Video.findById(id);
if (video) {
res.status(200).send(JSON.stringify(video.total_views));
}
} catch (error) {
res.status(400).send(error);
}
}
6 changes: 4 additions & 2 deletions routes/videos.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ router.get('/:id', videoInfos.getVideoInfoById);

router.get('/:id/likes', likes.getAllLikesByVideoId)

router.get('/:id/total-likes', likes.getTotalLikesByVideoId)

router.get('/:id/comments', comments.getAllCommentsByVideoId)

router.get('/:id/total-likes', likes.getTotalLikesByVideoId)

router.get('/:id/total-comments', comments.getTotalCommentsByVideoId)

router.get('/:id/total-views', videoInfos.getTotalViewsByVideoId)

router.post('/like', likes.likeVideo)

router.post('/comment', comments.commentVideo)
Expand Down

0 comments on commit c86acb1

Please sign in to comment.