Skip to content

Commit

Permalink
Merge pull request #49 from andela/ft-get-comments-164537634
Browse files Browse the repository at this point in the history
#164537634 get comments
  • Loading branch information
Chrismarcel committed Mar 12, 2019
2 parents cc90391 + ff3a572 commit 37fdacc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
26 changes: 23 additions & 3 deletions server/controllers/articleComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,32 @@ const { ArticleComment } = models;
* @exports ArticleComment
*/
class Comment {
/**
* @method getComments
* @description - Gets all comments for a single article
* @param {object} req - The request object
* @param {object} res - The response object
* @returns {object} - The comment object
*/
static async getComments(req, res) {
const articleId = req.article.id;
const comments = await ArticleComment.findAll({
where: {
articleId
}
});
return response(res).success({
message: 'Comments successfully retrieved',
comments
});
}

/**
* @method postComment
* @description - Posts comment to the database
* @param {object} req - The request object
* @param {object} res - The response object
* @returns {object} - The article object
* @returns {object} - The comment object
*/
static async postComment(req, res) {
const userId = req.user.id;
Expand All @@ -32,7 +52,7 @@ class Comment {
* @description - Updates comment
* @param {object} req - The request object
* @param {object} res - The response object
* @returns {object} - The updated article object
* @returns {object} - The updated comment object
*/
static async updateComment(req, res) {
const { commentRow } = req;
Expand Down Expand Up @@ -61,7 +81,7 @@ class Comment {
* @description - Deletes comment
* @param {object} req - The request object
* @param {object} res - The response object
* @returns {object} - The updated article object
* @returns {object} - response message
*/
static async deleteComment(req, res) {
const { commentRow } = req;
Expand Down
4 changes: 4 additions & 0 deletions server/routes/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ router.post('/articles/:slug/comment',
ValidateComment.validateComment,
Comment.postComment);

router.get('/articles/:slug/comments',
AuthenticateArticle.verifyArticle,
Comment.getComments);

router.get('/articles/tags',
controller.getTags.bind(controller));
router.get('/articles', controller.getAll.bind(controller));
Expand Down
2 changes: 1 addition & 1 deletion server/test/articles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ describe('DELETE article /api/articles/:slug', () => {
})
.end((err, res) => {
secondArticleSlug = res.body.article.slug;
done();
done(err);
});
});

Expand Down
12 changes: 12 additions & 0 deletions server/test/comment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ describe('PATCH update comment', () => {
});
});

describe('GET comments of a single article', () => {
it('should return 200 and get comments for an article', (done) => {
chai.request(app)
.get('/api/articles/this-is-an-article-1/comments')
.end((err, res) => {
expect(res.status).to.be.equal(200);
expect(res.body.message).to.be.equal('Comments successfully retrieved');
done(err);
});
});
});

describe('DELETE user comment', () => {
before((done) => {
chai
Expand Down

0 comments on commit 37fdacc

Please sign in to comment.