-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(edit/delete comment):user can edit/delet his comment
add editcomment function add deleteComment function [finishes #168781696]
- Loading branch information
Showing
12 changed files
with
292 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import strings from '../utils/stringsUtil'; | ||
import commentsHelper from '../helpers/commentsHelper'; | ||
|
||
const { COMMENT_UPDATED, COMMENT_DELETED } = strings.comments.success; | ||
|
||
export default class CommentsController { | ||
static editComment(req, res) { | ||
const { id } = req.params; | ||
const { comment } = req.body; | ||
|
||
commentsHelper.findComment(req).then(async comments => { | ||
await commentsHelper.checkComment( | ||
req, comments, res, { comment }, id, | ||
COMMENT_UPDATED, comments | ||
); | ||
}); | ||
} | ||
|
||
static deleteComment(req, res) { | ||
const { id } = req.params; | ||
|
||
commentsHelper.findComment(req).then(async comments => { | ||
await commentsHelper.checkComment( | ||
req, comments, res, { deleted: true }, id, | ||
COMMENT_DELETED, 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,36 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('comments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
comment: { | ||
type: Sequelize.STRING | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
requestId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
deleted: { | ||
type: Sequelize.BOOLEAN | ||
}, | ||
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
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 |
---|---|---|
@@ -1,20 +1,18 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => Promise.all([ | ||
queryInterface.bulkInsert('comments', [ | ||
{ | ||
comment: 'i want to go to andela kigali', | ||
userId: 2, | ||
requestId: 3, | ||
deleted: false, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
} | ||
]) | ||
]), | ||
|
||
down: (queryInterface, Sequelize) => Promise.all([ | ||
queryInterface.bulkDelete('comments', null, {}) | ||
]) | ||
up: (queryInterface, Sequelize) => Promise.all([ | ||
queryInterface.bulkInsert('comments', [ | ||
{ | ||
comment: 'i want to go to andela kigali', | ||
userId: 3, | ||
requestId: 3, | ||
deleted: false, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
} | ||
]) | ||
]), | ||
down: (queryInterface, Sequelize) => Promise.all([ | ||
queryInterface.bulkDelete('comments', null, {}) | ||
]) | ||
}; |
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,34 @@ | ||
import sequelize from 'sequelize'; | ||
import models from '../database/models'; | ||
import strings from '../utils/stringsUtil'; | ||
import responseUtil from '../utils/responseUtil'; | ||
|
||
const findComment = req => { | ||
const { Op } = sequelize; | ||
const { id } = req.params; | ||
const comment = models.comments.findOne({ | ||
where: { | ||
[Op.and]: [{ id }, | ||
{ deleted: false }] | ||
}, | ||
}); | ||
return comment; | ||
}; | ||
|
||
const checkComment = async (req, comments, res, data, id, message) => { | ||
const ownerId = req.user.payload.id; | ||
|
||
if (!comments) { | ||
return responseUtil(res, 404, strings.comments.error.COMMENT_NOT_FOUND); | ||
} | ||
if (comments.userId !== ownerId) { | ||
return responseUtil(res, 403, strings.comments.error.NOT_OWNER); | ||
} | ||
await models.comments.update(data, { where: { id, }, }); | ||
const commentData = await models.comments.findOne({ where: { id, }, attributes: { exclude: ['userId', 'requestId', 'deleted', 'createdAt', 'updatedAt'] }, }); | ||
return responseUtil(res, 200, message, commentData); | ||
}; | ||
|
||
export default { | ||
findComment, checkComment | ||
}; |
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