Skip to content

Commit

Permalink
Merge c8c1ea8 into 6b2ec24
Browse files Browse the repository at this point in the history
  • Loading branch information
vic3king committed Apr 10, 2019
2 parents 6b2ec24 + c8c1ea8 commit be8d8d5
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 16 deletions.
94 changes: 90 additions & 4 deletions server/controllers/comment.controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
import search from '../helpers/search-database';
import model from '../models';
import likeHelper from '../helpers/like-comment-helpers';
import validations from '../helpers/validations';
import likeHelper from '../helpers/like-comment-helpers';
import serverError from '../helpers/server-error';

const { User, Comment } = model;

const { User, Comment, Comment_history: CommentHistory } = model;
const { databaseError, findArticle } = search;

const getCommentAndHistories = async (req, res) => {
if (!validations.verifyUUID(req.params.commentid)) {
return res.status(400).json({
errors: {
body: ['id not valid'],
},
});
}

const comment = await Comment.findOne({
where: {
id: req.params.commentid,
},
});

if (!comment) {
return res.status(404).json({
errors: {
body: ['This comment does not exist'],
},
});
}
const editHistory = await Comment.findAll({
where: {
id: req.params.commentid,
},
include: [
{
model: CommentHistory,
as: 'updatedComments',
attributes: ['id', 'comment_id', 'body', 'updatedAt', 'createdAt'],
},
],
});

return res.status(200).json({
comment: editHistory,
});
};

const post = async (req, res) => {
const userId = req.user.userObj.id;
/**
Expand Down Expand Up @@ -98,6 +137,53 @@ const toggleLike = async (req, res) => {
}
};

const comments = { post, toggleLike };
const updateComment = async (req, res) => {
if (!validations.verifyUUID(req.params.commentid)) {
return res.status(400).json({
errors: {
body: ['id not valid'],
},
});
}

const comment = await Comment.findOne({
where: {
id: req.params.commentid,
},
});

if (!comment) {
return res.status(404).json({
errors: {
body: ['This comment does not exist'],
},
});
}

const userId = req.user.userObj.id;
if (userId !== comment.user_id) {
return res.status(403).json({
errors: {
body: ['You are not authorized to edit this comment'],
},
});
}

const editComment = await Comment.update(
{ body: req.body.body },
{ where: { id: req.params.commentid }, returning: true }
);

await CommentHistory.create({
comment_id: comment.id,
body: comment.body,
});

res.status(200).json({
editedComment: editComment,
});
};

const comments = { post, toggleLike, getCommentAndHistories, updateComment };

export default comments;
13 changes: 12 additions & 1 deletion server/joiSchema/commentSchema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Joi from 'joi';

const commentSchema = () => {
const postCommentSchema = () => {
const schema = Joi.object().keys({
article_id: Joi.string()
.guid({
Expand All @@ -15,4 +15,15 @@ const commentSchema = () => {
return schema;
};

const editCommentSchema = () => {
const schema = Joi.object().keys({
body: Joi.string()
.min(1)
.max(250)
.required(),
});
return schema;
};

const commentSchema = { postCommentSchema, editCommentSchema };
export default commentSchema;
21 changes: 18 additions & 3 deletions server/middlewares/comment.middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Joi from 'joi';
import joiFormater from '../helpers/joi-formater';
import commentSchema from '../joiSchema/commentSchema';

const validateComment = (req, res, next) => {
const { error } = Joi.validate(req.body, commentSchema());
const validatePostComment = (req, res, next) => {
const { error } = Joi.validate(req.body, commentSchema.postCommentSchema());

if (error) {
const { message } = error.details[0];
Expand All @@ -17,4 +17,19 @@ const validateComment = (req, res, next) => {
return next();
};

export default { validateComment };
const validateEditComment = (req, res, next) => {
const { error } = Joi.validate(req.body, commentSchema.editCommentSchema());

if (error) {
const { message } = error.details[0];
const formatedMessage = joiFormater(message);
return res.status(400).json({
errors: {
body: [formatedMessage],
},
});
}
return next();
};

export default { validatePostComment, validateEditComment };
32 changes: 32 additions & 0 deletions server/migrations/20190409194718-create-comment-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Comment_histories', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
unique: true,
},
comment_id: {
type: Sequelize.UUID,
allowNull: false,
},
body: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now'),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now'),
},
});
},
down: queryInterface => queryInterface.dropTable('Comment_histories'),
};
11 changes: 9 additions & 2 deletions server/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ module.exports = (sequelize, DataTypes) => {
},
});
Comment.associate = models => {
Comment.belongsTo(models.Article, {
const { Article, User, Comment_history } = models;

Comment.belongsTo(Article, {
foreignKey: 'user_id',
});
Comment.belongsTo(models.User, {
Comment.belongsTo(User, {
foreignKey: 'id',
});

Comment.hasMany(Comment_history, {
foreignKey: 'comment_id',
as: 'updatedComments',
});
};
return Comment;
};
26 changes: 26 additions & 0 deletions server/models/comment_history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = (sequelize, DataTypes) => {
const Comment_history = sequelize.define('Comment_history', {
id: {
allowNull: false,
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
unique: true,
},
comment_id: {
type: DataTypes.UUID,
allowNull: false,
},
body: {
type: DataTypes.STRING,
allowNull: false,
},
});
// Comment_history.associate = models => {
// Comment_history.belongsTo(models.Comment, {
// foreignKey: 'id',
// as: 'updatedComments',
// });
// };
return Comment_history;
};
16 changes: 15 additions & 1 deletion server/routes/comment.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const router = express.Router();
router.post(
'/comment',
tokenValidator.verifyToken,
commentMiddleware.validateComment,
commentMiddleware.validatePostComment,
commentController.post
);

Expand All @@ -49,4 +49,18 @@ router.post(
tokenValidator.verifyToken,
commentController.toggleLike
);

router.get(
'/comment/:commentid/history',
tokenValidator.verifyToken,
commentController.getCommentAndHistories
);

router.patch(
'/comment/:commentid/edit',
tokenValidator.verifyToken,
commentMiddleware.validateEditComment,
commentController.updateComment
);

export default router;
6 changes: 4 additions & 2 deletions server/seeders/20190331212702-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ module.exports = {
up: queryInterface =>
queryInterface.bulkInsert('Comments', [
{
id: 'a83dc585-9cc8-48ad-b1c0-b2df0dd28117',
id: '15a2628f-ecf7-4098-8db5-95ecaf24847e',
user_id: '6517a6ea-662b-4eef-ab9f-20f89bd7099c',
article_id: '7139d3af-b8b4-44f6-a49f-9305791700f4',
likes_count: 0,
body: 'Nice write up',
},
{
id: 'aaeed0f6-b074-4b75-a1e9-6d1a73bfa084',
id: '0b29d287-0ad0-42ca-8f74-3159bbe304af',
user_id: '57c515a1-890d-412f-8ca1-0a5395123dca',
article_id: 'fa3def47-153a-40bd-8181-a1c787e083d6',
likes_count: 0,
body: 'Nice',
},
]),
Expand Down
25 changes: 25 additions & 0 deletions server/seeders/20190409195758-comment-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
up: queryInterface =>
queryInterface.bulkInsert('Comment_histories', [
{
id: 'c125a9a9-f6d0-4e8f-ac39-6964f7b6a2c5',
comment_id: '15a2628f-ecf7-4098-8db5-95ecaf24847e',
body: 'deep write up',
},
{
id: '10ed2582-0e52-4645-a135-cd22b030ef41',
comment_id: '15a2628f-ecf7-4098-8db5-95ecaf24847e',
body: 'updated comment',
},
{
id: 'a1adb042-5111-4d16-bb5c-ef2f4b57bf65',
comment_id: '0b29d287-0ad0-42ca-8f74-3159bbe304af',
body: 'Nice',
},
{
id: '4432824c-dd6e-43e7-a154-f4a95c3734a2',
comment_id: '0b29d287-0ad0-42ca-8f74-3159bbe304af',
body: 'Hmmm',
},
]),
};
Loading

0 comments on commit be8d8d5

Please sign in to comment.