-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(comment): user can comment on article
- create comment model and migration - fix routes, middleware and controllers to allow user comment [Delivers #159206056]
- Loading branch information
Showing
25 changed files
with
689 additions
and
35 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
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,110 @@ | ||
import db from '../models'; | ||
import utils from '../helpers/utilities'; | ||
|
||
const { Comment, User, Reply } = db; | ||
|
||
/** * Class representing comments */ | ||
export default class CommentsController { | ||
/** | ||
* @function createComment | ||
* @summary Allows a user create comment | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
* @param {function} next - to handle errors | ||
* @returns {object} status code 201 with comment created if successful | ||
*/ | ||
static createComment(req, res, next) { | ||
const { body } = req.body.comment; | ||
Comment.create({ | ||
userId: req.userId, | ||
articleId: req.articleObject.id, | ||
body | ||
}) | ||
.then(comment => res.status(201).json({ | ||
success: true, | ||
comment, | ||
user: utils.userToJson(req.userObject), | ||
article: req.articleObject, | ||
})) | ||
.catch(next); | ||
} | ||
|
||
/** | ||
* @function createReply | ||
* @summary Allows a user create comment | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
* @param {function} next - to handle errors | ||
* @returns {object} status code 201 with comment created if successful | ||
*/ | ||
static createReply(req, res, next) { | ||
const { body } = req.body.comment; | ||
Reply.create({ | ||
userId: req.userId, | ||
commentId: req.params.id, | ||
body, | ||
}) | ||
.then(reply => res.status(201).json({ | ||
success: true, | ||
reply, | ||
user: utils.userToJson(req.userObject), | ||
comment: req.commentObject, | ||
})) | ||
.catch(next); | ||
} | ||
|
||
/** | ||
* @function getComments | ||
* @summary Get a single comment, the user and article related to it | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
* @param {function} next - to handle errors | ||
* @returns {object} status code 200 with comment if successful | ||
*/ | ||
static getComments(req, res, next) { | ||
Comment.findAll({ | ||
include: [{ | ||
model: User, | ||
attributes: { exclude: ['id', 'email', 'hashedPassword', 'createdAt', 'updatedAt'] }, | ||
}, { | ||
model: Reply, | ||
include: [{ | ||
model: User, | ||
attributes: { exclude: ['id', 'email', 'hashedPassword', 'createdAt', 'updatedAt', 'lastname', 'firstname', 'bio'] }, | ||
}] | ||
}], | ||
where: { | ||
articleId: req.articleObject.id, | ||
} | ||
}) | ||
.then(comments => res.status(200).json({ | ||
success: true, | ||
article: req.articleObject, | ||
comments, | ||
})) | ||
.catch(next); | ||
} | ||
|
||
/** | ||
* @function deleteComment | ||
* @summary Delete a single comment | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
* @param {function} next - to handle errors | ||
* @returns {object} status code 200 if successful | ||
*/ | ||
static deleteComment(req, res, next) { | ||
Comment.destroy({ | ||
where: { | ||
id: req.params.id, | ||
} | ||
}) | ||
.then(() => res.status(200).json({ | ||
success: true, | ||
message: { | ||
body: ['Comment deleted successfully'] | ||
} | ||
})) | ||
.catch(next); | ||
} | ||
} |
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,21 @@ | ||
import db from '../models'; | ||
|
||
const { Article } = db; | ||
const getArticle = (req, res, next) => { | ||
Article.findOne({ where: { slug: req.params.slug } }) | ||
.then((article) => { | ||
if (!article) { | ||
return res.status(404).json({ | ||
success: false, | ||
errors: { | ||
body: ['The article does not exist'] | ||
}, | ||
}); | ||
} | ||
req.articleObject = article; | ||
next(); | ||
}) | ||
.catch(next); | ||
}; | ||
|
||
export default getArticle; |
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,21 @@ | ||
import db from '../models'; | ||
|
||
const { Comment } = db; | ||
const getComment = (req, res, next) => { | ||
Comment.findById(req.params.id) | ||
.then((comment) => { | ||
if (!comment) { | ||
return res.status(404).json({ | ||
success: false, | ||
errors: { | ||
body: ['The comment does not exist'] | ||
}, | ||
}); | ||
} | ||
req.commentObject = comment; | ||
next(); | ||
}) | ||
.catch(next); | ||
}; | ||
|
||
export default getComment; |
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,26 @@ | ||
const validateComment = (req, res, next) => { | ||
const { body, commentId } = req.body.comment; | ||
const errors = []; | ||
if (!body) { | ||
errors.push('Comment can not be empty'); | ||
} | ||
if (typeof body !== 'string') { | ||
errors.push('Comment must be a string'); | ||
} | ||
if (commentId) { | ||
if (typeof commentId !== 'number') { | ||
errors.push('parentId must be a number'); | ||
} | ||
} | ||
if (errors.length !== 0) { | ||
return res.status(403).json({ | ||
success: false, | ||
errors: { | ||
body: [...errors] | ||
} | ||
}); | ||
} | ||
next(); | ||
}; | ||
|
||
export default validateComment; |
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
File renamed without changes.
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,45 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Comments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
as: 'userId', | ||
}, | ||
}, | ||
articleId: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
as: 'articleId', | ||
}, | ||
}, | ||
body: { | ||
allowNull: false, | ||
type: Sequelize.TEXT | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface/* , Sequelize */) => { | ||
queryInterface.dropTable('Comments'); | ||
} | ||
}; |
Oops, something went wrong.