Skip to content

Commit

Permalink
Merge 14fff35 into d91b908
Browse files Browse the repository at this point in the history
  • Loading branch information
habinezadalvan committed Nov 3, 2019
2 parents d91b908 + 14fff35 commit 13d302b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Articles {
image
};
const rating = await RateService.getArticleRatingStatistic(article.slug);
let claps = await likeService.getAllAClaps(req.params.Article);
let claps = await likeService.getAllAClaps(article.slug);
claps = Object.values(claps)[0];
const readTime = Helper.calculateReadTime(article.body);
const timeAgo = moment(article.createdAt).fromNow();
Expand Down
123 changes: 120 additions & 3 deletions src/controllers/comments.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import moment from 'moment';
import _ from 'lodash';
import commentsService from '../services/comments.service';
import UserService from '../services/user.service';
import models from '../models';
Expand All @@ -6,7 +8,6 @@ import NotificationServices from '../services/notification.service';
import Util from '../helpers/util';
import StatsService from '../services/db.service';


const util = new Util();

const { notifyUsersWhoFavorited } = NotificationServices;
Expand Down Expand Up @@ -41,7 +42,7 @@ class Comments {
articleSlug: getArticle.slug,
userId: req.auth.id,
body: req.body.body,
parentCommentId: req.body.parentCommentId,
parentCommentId: req.body.parentCommentId
};
const createdComment = await commentsService.addComment(comment);
await notifyUsersWhoFavorited(req, res, getArticle.id, req.params.slug);
Expand Down Expand Up @@ -117,6 +118,119 @@ class Comments {
return util.send(res);
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} data
* @memberof Comments
*/
static async getAllCommentsOfArticle(req, res) {
const { offset, limit } = req.query;
const { articleSlug } = req.params;
const comments = await CommentsDb.findAll({
where: { articleSlug },
order: [['createdAt', 'DESC']],
offset,
limit
});
if (comments.length === 0) {
await util.setError(404, 'No comments found');
return util.send(res);
}
const allComments = _.map(
comments,
_.partialRight(_.pick, [
'id',
'userId',
'articleSlug',
'body',
'parentCommentId',
'commentRevisions',
'likesCount',
'likeInfo',
'createdAt'
])
);
await Promise.all(
allComments.map(async (comment) => {
try {
const userDetails = await UserService.getOneUser(comment.userId);
const { username, image } = userDetails;
const timeAgo = moment(comment.createdAt).fromNow();
comment.username = username;
comment.userImage = image;
comment.timeCreated = timeAgo;
return true;
} catch (error) {
throw error;
}
})
);
await util.setSuccess(200, 'All comments successfully retrieved', allComments);
return util.send(res);
}

// delete a comment of a particular article.
/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} data
* @memberof Comments
*/
static async deleteCommentOfAnArticle(req, res) {
const userId = req.auth.id;
const getComment = await CommentsDb.findOne({ where: { id: req.params.id } });
if (!getComment) {
await util.setError(404, 'That article comment does not exit');
return util.send(res);
}
const commentedUser = getComment.dataValues.userId;
if (userId !== commentedUser) {
await util.setError(403, 'Sorry, you are not allowed to delete this comment');
return util.send(res);
}
await commentsService.deleteComment(req.params.id);
await util.setSuccess(200, 'Comment was successfully deleted');
return util.send(res);
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} return comment updation message
* @memberof UserController
*/
static async updateParticularComment(req, res) {
const userId = req.auth.id;
const getComment = await CommentsDb.findOne({ where: { id: req.params.id } });
if (!getComment) {
await util.setError(404, 'That article comment does not exit');
return util.send(res);
}
const commentedUser = getComment.dataValues.userId;
if (userId !== commentedUser) {
await util.setError(403, 'Sorry, you are not allowed to update this comment');
return util.send(res);
}
const { body } = req.body;
const commentRevisions = getComment.dataValues.body;
const updateComment = await commentsService.updateComment(req.params.id, {
body,
commentRevisions
});
await util.setSuccess(200, 'Update is successfully', updateComment);
return util.send(res);
}

/**
*
*
Expand All @@ -142,7 +256,10 @@ class Comments {

const { body } = req.body;
const commentRevisions = getComment.dataValues.body;
const updateComment = await commentsService.updateComment(req.params.id, { body, commentRevisions });
const updateComment = await commentsService.updateComment(req.params.id, {
body,
commentRevisions
});
await util.setSuccess(200, 'Update is successfully', updateComment);
return util.send(res);
}
Expand Down
8 changes: 5 additions & 3 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ class UserController {
email: theUser.email,
username: theUser.username,
role: theUser.role,
verified: theUser.verified
verified: theUser.verified,
image: theUser.image
};
const token = await Helper.generateToken(payload);
return res.status(200).send({
status: 200,
message: `welcome back ${theUser.firstname}`,
token
token,
});
} catch (error) {
return res.status(404).send({
Expand Down Expand Up @@ -120,7 +121,8 @@ class UserController {
id: createdUser.id,
email: createdUser.email,
role: createdUser.role,
verified: createdUser.verified
verified: createdUser.verified,
image: createdUser.image
};
const token = await Helper.generateToken(payload);
const verifyUrl = `${process.env.FRONT_END_URL}/verify?token=${token}`;
Expand Down
3 changes: 3 additions & 0 deletions src/routes/api/comment/comments.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import confirmEmailAuth from '../../../middlewares/emailVarification.middleware'

const router = express.Router();
router.post('/:slug', [auth, CommentsValidation], Comments.createComment);
router.get('/:articleSlug', Comments.getAllCommentsOfArticle);
router.get('/', [auth], Comments.getComments);
router.delete('/:id', [auth], Comments.deleteComment);
router.delete('/particularArticle/:id', [auth], Comments.deleteCommentOfAnArticle);
router.put('/particularArticle/:id', [auth, CommentsValidation], Comments.updateParticularComment);
router.put('/:id', [auth, CommentsValidation], Comments.updateComment);
router.get('/like/:id', [auth, confirmEmailAuth], Comments.getLikesComments);
router.post('/like/:id', [auth, confirmEmailAuth], Comments.likeComment);
Expand Down
9 changes: 0 additions & 9 deletions test/unit.test.js

This file was deleted.

0 comments on commit 13d302b

Please sign in to comment.