Skip to content

Commit

Permalink
Merge e8e691a into 33dfd3e
Browse files Browse the repository at this point in the history
  • Loading branch information
fantastic-genius committed Jul 18, 2019
2 parents 33dfd3e + e8e691a commit 7805a86
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 8 deletions.
87 changes: 87 additions & 0 deletions controllers/comments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,92 @@ export default {
message: 'something went wrong'
});
}
},

editComment: async (req, res) => {
const { params: { slug, commentId }, body: { content } } = req;
try {
const foundArticle = await db.Article.findOne({
where: { slug }
});
if (!foundArticle) {
return res.status(404).send({
error: 'Article does not exist'
});
}

const foundComment = await db.Comment.findOne({
where: { id: commentId }
});
if (!foundComment) {
return res.status(404).send({
error: 'Comment does not exist'
});
}
await db.CommentHistory.create({
commentId,
content: foundComment.content
});

const editedComment = await foundComment.update({ content });

return res.status(200).send({
comment: editedComment
});
} catch (e) {
/* istanbul ignore next */
return res.status(500).send({
error: 'Something went wrong',
});
}
},

getCommentHistory: async (req, res) => {
const { params: { slug, commentId }, query } = req;
const limit = query.limit || 20;
const offset = query.offset ? (query.offset * limit) : 0;

try {
const article = await db.Article.findOne({
where: { slug }
});

if (!article) {
return res.status(404).send({
error: 'Article does not exist'
});
}

const comment = await db.Comment.findOne({
where: { id: commentId },
attributes: ['id', 'articleId', 'content', 'updatedAt']
});

if (!comment) {
return res.status(404).send({
error: 'Comment does not exist'
});
}

const commentHistory = await db.CommentHistory.findAndCountAll({
offset,
limit,
where: {
commentId
},
attributes: ['content', 'createdAt']
});

return res.status(200).send({
comment,
commentHistory: commentHistory.rows,
commentHistorycount: commentHistory.count
});
} catch (e) {
/* istanbul ignore next */
return res.status(500).send({
error: e.message
});
}
}
};
32 changes: 32 additions & 0 deletions db/migrations/20190716112846-create-comment-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('CommentHistories', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
commentId: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'Comments',
key: 'id',
as: 'comment'
}
},
content: {
allowNull: false,
type: Sequelize.TEXT
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('CommentHistories')
};
6 changes: 6 additions & 0 deletions db/models/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ module.exports = (sequelize, DataTypes) => {
as: 'bookmarks',
cascade: true,
});

Article.hasMany(models.Comment, {
foreignKey: 'articleId',
as: 'comment',
cascade: true
});
};
return Article;
};
11 changes: 7 additions & 4 deletions db/models/Comment.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@


module.exports = (sequelize, DataTypes) => {
const Comment = sequelize.define('Comment', {
userId: DataTypes.INTEGER,
articleId: DataTypes.INTEGER,
content: DataTypes.TEXT
}, {});
Comment.associate = (models) => {
Comment.belongsTo(models.User, {
foreignKey: 'userId',
as: 'user'
});

Comment.belongsTo(models.Article, {
foreignKey: 'articleId',
as: 'article',
cascade: true
});

Comment.hasMany(models.CommentHistory, {
foreignKey: 'commentId',
as: 'commentHistory',
cascade: true
});
};
return Comment;
};
12 changes: 12 additions & 0 deletions db/models/CommentHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = (sequelize, DataTypes) => {
const CommentHistory = sequelize.define('CommentHistory', {
content: DataTypes.TEXT
}, {});
CommentHistory.associate = (models) => {
CommentHistory.belongsTo(models.Comment, {
foreignKey: 'commentId',
as: 'comment'
});
};
return CommentHistory;
};
20 changes: 20 additions & 0 deletions routes/v1/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,24 @@ router.get(
Validation.articleSlug,
Article.bookmarkArticle,
);

router.patch(
'/:slug/comments/:commentId',
Middleware.authenticate,
Middleware.isblackListedToken,
Validation.articleSlug,
Validation.addComment,
Validation.commentId,
Comment.editComment
);

router.get(
'/:slug/comments/:commentId/history',
Middleware.authenticate,
Middleware.isblackListedToken,
Validation.articleSlug,
Validation.commentId,
Comment.getCommentHistory
);

export default router;
63 changes: 62 additions & 1 deletion swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,66 @@ paths:
responses:
'200':
description: an object containg search result
/articles/{slug}/comments/{commnetId}:
patch:
tags:
- Edit Article Comment
summary: edit article comment
operationId: EditComment
consumes:
- application/json
- application/x-www-form-urlencoded
produces:
- application/json
parameters:
- name: "slug"
in: "path"
description: "Slug of article with its comment being edited"
required: true
type: string
- name: "commentId"
in: "path"
description: "Id of article comment to be edited"
required: true
type: "integer"
format: "int64"
responses:
'200':
description: comment successfully edited
'404':
description: Article not existing or comment not existing
'400':
description: Article slug must be string or comment Id must be integer
/articles/{slug}/comments/{commnetId}/history:
get:
tags:
- Get Article Comment Edit History
summary: get an article comment's edit history
operationId: GetCommentHistory
consumes:
- application/json
- application/x-www-form-urlencoded
produces:
- application/json
parameters:
- name: "slug"
in: "path"
description: "Islug of article whose comment historynis being gotten"
required: true
type: string
- name: "commentId"
in: "path"
description: "Id of article comment that its edit history is being gotten"
required: true
type: "integer"
format: "int64"
responses:
'200':
description: comment edit history successfully gotten
'404':
description: Article not existing or comment not existing
'400':
description: Article slug must be string or comment Id must be integer
definitions:
Notify:
type: object
Expand Down Expand Up @@ -623,7 +683,8 @@ definitions:
images:
type: string
tags:
type: string (should be comma separated strings)
type: string
description: should be comma separated strings
xml:
name: NewArticle
ratings:
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export const createArticle = async article => db.Article.create({
readTime: readingTime(article.body).text
});

export const createComment = async comment => db.Comment.create(comment);

export const createRate = async rating => db.Ratings.create(rating);

export const createArticleVote = async vote => db.ArticleVote.create(vote);
Expand All @@ -67,3 +69,7 @@ export const createTestFakeUsers = () => {
export const createTag = async tag => db.Tag.create(tag);

export const createArticleTag = async tag => db.ArticleTag.create(tag);

export const editComment = async editedComment => db.CommentHistory.create(editedComment);

export const createCommentHistory = async editedComment => db.CommentHistory.create(editedComment);
Loading

0 comments on commit 7805a86

Please sign in to comment.