-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
385 additions
and
71 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,151 @@ | ||
import commentsService from '../services/comments.service'; | ||
import UserService from '../services/user.service'; | ||
import models from '../models'; | ||
|
||
const CommentsDb = models.Comment; | ||
const database = models.Article; | ||
/** | ||
* | ||
* | ||
* @class Comments | ||
*/ | ||
class Comments { | ||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} data | ||
* @memberof Comments | ||
*/ | ||
static async createComment(req, res) { | ||
const userId = req.auth.id; | ||
const getUser = await UserService.getOneUser(userId); | ||
const getArticle = await database.findOne({ | ||
where: { slug: req.params.slug } | ||
}); | ||
if (!getUser) return res.status(404).send({ message: `User with id ${req.auth.id} not found` }); | ||
if (!getArticle) return res.status(404).send({ message: `Article with slug ${req.params.slug} not found` }); | ||
try { | ||
const comment = { | ||
articleSlug: getArticle.slug, | ||
userId: req.auth.id, | ||
body: req.body.body | ||
}; | ||
const createdComment = await commentsService.addComment(comment); | ||
return res.status(201).send({ | ||
status: 201, | ||
message: 'Comment successfully created', | ||
comment: createdComment | ||
}); | ||
} catch (error) { | ||
return res.send({ | ||
message: error.message | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} data | ||
* @memberof Comments | ||
*/ | ||
static async deleteComment(req, res) { | ||
const userId = req.auth.id; | ||
const getUser = await CommentsDb.findOne({ where: { userId } }); | ||
const commentAuthor = getUser && getUser.get().userId; | ||
const { id } = req.params; | ||
|
||
if (!Number(id)) { | ||
return res.status(400).send({ | ||
status: 400, | ||
message: 'Please provide numeric value' | ||
}); | ||
} | ||
if (!(req.auth.id === commentAuthor)) { | ||
return res.status(403).send({ | ||
status: 403, | ||
message: 'comment is not yours' | ||
}); | ||
} | ||
try { | ||
const CommentTODelete = await commentsService.deleteComment(id); | ||
if (CommentTODelete) { | ||
return res.status(200).send({ | ||
status: 200, | ||
message: `Comment with id ${id} is successfully deleted` | ||
}); | ||
} | ||
|
||
return res.status(404).send({ | ||
status: 404, | ||
message: `Comment with id ${id} is not found` | ||
}); | ||
} catch (error) { | ||
return res.send({ | ||
message: error.message | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} data | ||
* @memberof Comments | ||
*/ | ||
static async getComments(req, res) { | ||
const comments2 = await CommentsDb.findAll(); | ||
if (!comments2) { | ||
return res.status(200).send({ | ||
message: 'No comments found' | ||
}); | ||
} | ||
return res.status(200).send({ | ||
status: 200, | ||
message: 'All comments successfully retrieved', | ||
comments2 | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {Object} return comment updation message | ||
* @memberof UserController | ||
*/ | ||
static async updateComment(req, res) { | ||
const getComment = await CommentsDb.findOne({ where: { id: req.params.id } }); | ||
const gottenComent = getComment && getComment.get().id; | ||
const { id } = req.params; | ||
if (!id) { | ||
return res.status(400).send({ | ||
status: 400, | ||
message: 'Please provide valid numeric value' | ||
}); | ||
} | ||
if (!gottenComent) { | ||
return res.status(200).json({ status: 200, message: 'That comment does not exist' }); | ||
} | ||
|
||
const { body } = req.body; | ||
const updateComment = await commentsService.updateComment(req.params.id, { body }); | ||
return res.status(200).send({ | ||
status: 200, | ||
message: 'Updation is successfully', | ||
updateComment | ||
}); | ||
} | ||
} | ||
export default 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import validator from 'validator'; | ||
|
||
const CommentValidation = (req, res, next) => { | ||
if (validator.isEmpty(req.body.body)) { | ||
return res.status(400).send({ | ||
message: 'Comment body is required' | ||
}); | ||
} | ||
next(); | ||
}; | ||
export default CommentValidation; |
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,33 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Comments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
articleSlug: { | ||
type: Sequelize.STRING | ||
}, | ||
body: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Comment = sequelize.define('Comment', { | ||
userId: DataTypes.INTEGER, | ||
articleSlug: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
references: { | ||
model: 'Articles', | ||
key: 'slug' | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE' | ||
}, | ||
body: DataTypes.STRING | ||
}, {}); | ||
Comment.associate = function(models) { | ||
// associations can be defined here | ||
Comment.belongsTo(models.user,{ | ||
foreignKey:'id',as:'commentAuthor' | ||
}); | ||
}; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import express from 'express'; | ||
import Comments from '../../../controllers/comments.controller'; | ||
import auth from '../../../middlewares/auth'; | ||
import CommentsValidation from '../../../middlewares/validators/comments.body'; | ||
|
||
const router = express.Router(); | ||
router.post('/:slug', [auth, CommentsValidation], Comments.createComment); | ||
router.get('/', [auth], Comments.getComments); | ||
router.delete('/:id', [auth], Comments.deleteComment); | ||
router.put('/:id', [auth, CommentsValidation], Comments.updateComment); | ||
export default router; |
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,67 @@ | ||
import models from '../models'; | ||
|
||
const database = models.Comment; | ||
/** | ||
* | ||
* | ||
* @class commentsService | ||
*/ | ||
class commentsService { | ||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} comment | ||
* @returns {object} data | ||
* @memberof commentsService | ||
*/ | ||
static async addComment(comment) { | ||
try { | ||
return await database.create(comment); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} id | ||
* @returns {object} data | ||
* @memberof commentsService | ||
*/ | ||
static async deleteComment(id) { | ||
try { | ||
const CommentToDelete = await database.findOne({ where: { id: Number(id) } }); | ||
if (CommentToDelete) { | ||
const deletedComment = await database.destroy({ | ||
where: { id: Number(id) } | ||
}); | ||
return deletedComment; | ||
} | ||
return null; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} id | ||
* @param {*} updateComments | ||
* @returns {object} data | ||
* @memberof commentsService | ||
*/ | ||
static async updateComment(id, updateComments) { | ||
try { | ||
const results = await database.update(updateComments, { where: { id }, returning: true }); | ||
return results; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
} | ||
export default commentsService; |