-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from andela/ft/167190444-user-comment
#167190444 User can make comments to articles
- Loading branch information
Showing
15 changed files
with
320 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import models from '../db/models'; | ||
import helpers from '../helpers'; | ||
|
||
const { successStat, errorStat } = helpers; | ||
|
||
/** | ||
* @Module CommentController | ||
* @description Controlls comments made by users | ||
*/ | ||
export default class CommentController { | ||
/** | ||
* @static | ||
* @param {*} req Request object | ||
* @param {*} res Response object | ||
* @returns {Object} Object containing the user comment, author, and timestaps | ||
* @memberof CommentController | ||
*/ | ||
static async addAComment(req, res) { | ||
const { params: { postId }, user } = req; | ||
const { comment } = req.body.comment; | ||
const post = await models.Article.findByPk(postId); | ||
if (!post) return errorStat(res, 404, 'Post not found'); | ||
const newComment = { | ||
authorId: user.id, | ||
body: comment, | ||
articleId: postId | ||
}; | ||
const userComment = await models.Comment.create(newComment); | ||
await post.addComment(userComment); | ||
const commentResponse = await models.Comment.findOne({ | ||
where: { id: userComment.id }, | ||
include: [ | ||
{ | ||
as: 'author', | ||
model: models.User, | ||
attributes: ['id', 'firstname', 'lastname', 'image', 'socialId', 'username'] | ||
} | ||
], | ||
attributes: { | ||
exclude: [ | ||
'authorId' | ||
] | ||
} | ||
}); | ||
return successStat(res, 201, 'comment', commentResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Comments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
body: { | ||
type: Sequelize.TEXT, | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
authorId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Comments') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const Comment = sequelize.define('Comment', { | ||
body: DataTypes.TEXT, | ||
authorId: DataTypes.INTEGER, | ||
articleId: DataTypes.INTEGER | ||
}, {}); | ||
Comment.associate = (models) => { | ||
Comment.belongsTo(models.Article, { as: 'article' }); | ||
Comment.belongsTo(models.User, { as: 'author' }); | ||
}; | ||
return Comment; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import express from 'express'; | ||
import middlewares from '../middlewares'; | ||
import CommentController from '../controllers/commentController'; | ||
|
||
const { | ||
verifyToken, | ||
validateCommentMessage | ||
} = middlewares; | ||
|
||
const { addAComment } = CommentController; | ||
|
||
const commentRouter = express(); | ||
|
||
commentRouter.post('/:postId', verifyToken, validateCommentMessage, addAComment); | ||
|
||
export default commentRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../index'; | ||
import commentData from './testData/comment.data'; | ||
import userData from './testData/user.data'; | ||
|
||
|
||
const { expect } = chai; | ||
chai.use(chaiHttp); | ||
const baseUrl = '/api/v1'; | ||
|
||
describe('Handle Comment', () => { | ||
let userToken, postId; | ||
before('sign up a user', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseUrl}/users/`) | ||
.send(userData[20]) | ||
.end(() => { | ||
done(); | ||
}); | ||
}); | ||
describe('Add Comment', () => { | ||
before('sign in a user', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseUrl}/users/login`) | ||
.send(userData[21]) | ||
.end((err, res) => { | ||
userToken = res.body.user.token; | ||
done(); | ||
}); | ||
}); | ||
before('add a post', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseUrl}/articles`) | ||
.set('Authorization', `${userToken}`) | ||
.send({ | ||
article: { | ||
title: 'Hello', | ||
description: 'i say hello', | ||
articleBody: 'hhh', | ||
tagList: 'jssd', | ||
image: 'hs.jpg' | ||
} | ||
}) | ||
.end((err, res) => { | ||
postId = res.body.articles.id; | ||
done(); | ||
}); | ||
}); | ||
it('Should fail if user isn\'t logged in', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseUrl}/comment/${postId}`) | ||
.send(commentData[0]) | ||
.end((err, res) => { | ||
const { status, error } = res.body; | ||
expect(status).to.equal(401); | ||
expect(error).to.equal('Authorization error'); | ||
done(); | ||
}); | ||
}); | ||
it('Should fail if message is empty', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseUrl}/comment/${postId}`) | ||
.set('Authorization', `${userToken}`) | ||
.send(commentData[1]) | ||
.end((err, res) => { | ||
const { status, error } = res.body; | ||
expect(status).to.equal(400); | ||
expect(error[0]).to.equal('comment is not allowed to be empty'); | ||
done(); | ||
}); | ||
}); | ||
it('Should fail if post is not found', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseUrl}/comment/${23}`) | ||
.set('Authorization', `${userToken}`) | ||
.send(commentData[0]) | ||
.end((err, res) => { | ||
const { status, error } = res.body; | ||
expect(status).to.equal(404); | ||
expect(error).to.equal('Post not found'); | ||
done(); | ||
}); | ||
}); | ||
it('Should pass if user is a valid one and is logged in', (done) => { | ||
chai | ||
.request(app) | ||
.post(`${baseUrl}/comment/${postId}`) | ||
.set('Authorization', `${userToken}`) | ||
.send(commentData[0]) | ||
.end((err, res) => { | ||
const { status } = res.body; | ||
expect(status).to.equal(201); | ||
expect(res.body.comment).to.be.an('object'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const comment = [ | ||
// correct message detail 0 | ||
{ | ||
comment: 'I love this post, It is very intuitive' | ||
}, | ||
// incorrect message details 1 | ||
{ | ||
comment: '' | ||
} | ||
|
||
]; | ||
|
||
export default comment; |
Oops, something went wrong.