-
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.
feat(request comments): request comments
- setup controller for user to comment on requests - setup route for user to comment on requests - setup unit test for user to comment on requests route [Delivers #167750066]
- Loading branch information
Showing
12 changed files
with
604 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import models from '../models'; | ||
import { successResponse, errorResponse, status } from '../utils'; | ||
|
||
const { Comments, Requests } = models; | ||
|
||
/** | ||
* @class CommentsController | ||
* @description Controllers for handling travel requests comments | ||
* @exports CommentsController | ||
*/ | ||
class CommentsController { | ||
/** | ||
* @method addComment | ||
* @description Method to add comment to requests | ||
* @param {object} req - The Request Object | ||
* @param {object} res - The Response Object | ||
* @returns {object} Newly added request comment | ||
*/ | ||
static async addComment(req, res) { | ||
const { userId } = req.user; | ||
const { requestId } = req.params; | ||
const { comment } = req.body; | ||
try { | ||
const existingRequest = await Requests.findOne({ | ||
where: { id: requestId } | ||
}); | ||
if (!existingRequest) { | ||
return errorResponse(res, status.notfound, 'Request does not exist'); | ||
} | ||
const commentAdded = await Comments.create({ comment, userId, requestId }); | ||
const response = commentAdded.toJSON(); | ||
return successResponse(res, status.created, 'Comment added successfully', response); | ||
} catch (error) { | ||
return errorResponse(res, status.error, 'Error adding comment'); | ||
} | ||
} | ||
|
||
/** | ||
* @method getSingleComment | ||
* @description Method to get a comment | ||
* @param {object} req - The Request Object | ||
* @param {object} res - The Response Object | ||
* @returns {object} retrieved comment details | ||
*/ | ||
static async getCommentById(req, res) { | ||
const { commentId } = req.params; | ||
try { | ||
const getComment = await Comments.findByPk(commentId); | ||
if (!getComment) { | ||
return errorResponse(res, status.notfound, 'Comment not found'); | ||
} | ||
const response = getComment.toJSON(); | ||
return successResponse(res, status.success, 'Comment retrieved successfully', response); | ||
} catch (error) { | ||
return errorResponse(res, status.error, 'Error retrieving comment'); | ||
} | ||
} | ||
|
||
/** | ||
* @method updateComment | ||
* @description Method to update comment | ||
* @param {object} req - The Request Object | ||
* @param {object} res - The Response Object | ||
* @returns {object} updated comment details | ||
*/ | ||
static async updateCommentById(req, res) { | ||
const { commentId } = req.params; | ||
const { comment } = req.body; | ||
try { | ||
const getComment = await Comments.findOne({ where: { id: commentId } }); | ||
if (!getComment) { | ||
return errorResponse(res, status.notfound, 'Comment not found'); | ||
} | ||
await Comments.update({ comment }, { where: { id: commentId } }); | ||
return successResponse(res, status.success, 'Comment updated successfully'); | ||
} catch (error) { | ||
return errorResponse(res, status.error, 'Error updating comment'); | ||
} | ||
} | ||
|
||
/** | ||
* @method deleteComment | ||
* @description Method to delete comment | ||
* @param {object} req - The Request Object | ||
* @param {object} res - The Response Object | ||
* @returns {object} deleted comment details | ||
*/ | ||
static async deleteCommentById(req, res) { | ||
const { commentId } = req.params; | ||
try { | ||
const getComment = await Comments.findByPk(commentId); | ||
if (!getComment) { | ||
return errorResponse(res, status.notfound, 'Comment not found'); | ||
} | ||
await Comments.destroy({ where: { id: commentId } }); | ||
return successResponse(res, status.success, 'Comment deleted successfully'); | ||
} catch (error) { | ||
return errorResponse(res, status.error, 'Error deleting comment'); | ||
} | ||
} | ||
|
||
/** | ||
* @method getAllCommentsOnRequest | ||
* @description Method to get all comments on requests | ||
* @param {object} req - The Request Object | ||
* @param {object} res - The Response Object | ||
* @returns {object} retrieved comments details | ||
*/ | ||
static async getAllCommentsOnRequest(req, res) { | ||
const { requestId } = req.params; | ||
try { | ||
const existingRequest = await Requests.findOne({ where: { id: requestId } }); | ||
if (!existingRequest) { | ||
return errorResponse(res, status.notfound, 'Request not found'); | ||
} | ||
const response = await Comments.findAll({ where: { requestId } }); | ||
return successResponse(res, status.success, 'Comments retrieved successfully', response); | ||
} catch (error) { | ||
return errorResponse(res, status.error, 'Error retrieving comments'); | ||
} | ||
} | ||
} | ||
|
||
export default CommentsController; |
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,29 @@ | ||
export default { | ||
up: queryInterface => queryInterface.bulkInsert( | ||
'Requests', | ||
[ | ||
{ | ||
id: '2b770fbc-76e6-4b5a-afab-882759fd1f06', | ||
status: 'pending', | ||
accommodationId: '2b770fbc-76e6-4b5a-afab-882759fd1f06', | ||
userId: 'e71c28fd-73d8-4d92-9125-ab3d022093b9' | ||
}, | ||
{ | ||
id: 'b356097c-c6d0-4a3d-85f6-33bc2595c974', | ||
status: 'rejected', | ||
accommodationId: '2b770fbc-76e6-4b5a-afab-882759fd1f06', | ||
userId: 'e71c28fd-73d8-4d92-9125-ab3d022093b0' | ||
}, | ||
{ | ||
id: '777f640e-a2ff-45ee-9ce1-bf37645c42d6', | ||
status: 'approved', | ||
accommodationId: '2b770fbc-76e6-4b5a-afab-882759fd1f06', | ||
userId: '7aa38d4e-7fbf-4067-8821-9c27d2fb6e3a' | ||
}, | ||
], | ||
{} | ||
), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('Requests', 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,29 @@ | ||
export default { | ||
up: queryInterface => queryInterface.bulkInsert( | ||
'Comments', | ||
[ | ||
{ | ||
id: '2b770fbc-76e6-4b5a-afab-882759fd1f06', | ||
comment: 'my added comment', | ||
userId: 'e71c28fd-73d8-4d92-9125-ab3d022093b9', | ||
requestId: '2b770fbc-76e6-4b5a-afab-882759fd1f06' | ||
}, | ||
{ | ||
id: 'b356097c-c6d0-4a3d-85f6-33bc2595c974', | ||
comment: 'my added comment', | ||
userId: 'e71c28fd-73d8-4d92-9125-ab3d022093b0', | ||
requestId: '2b770fbc-76e6-4b5a-afab-882759fd1f06' | ||
}, | ||
{ | ||
id: '777f640e-a2ff-45ee-9ce1-bf37645c42d6', | ||
comment: 'mu added comment', | ||
userId: '7aa38d4e-7fbf-4067-8821-9c27d2fb6e3a', | ||
requestId: '2b770fbc-76e6-4b5a-afab-882759fd1f06', | ||
}, | ||
], | ||
{} | ||
), | ||
|
||
down: queryInterface => 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
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 { Router } from 'express'; | ||
import { CommentsController } from '../../controllers'; | ||
import middlewares from '../../middlewares'; | ||
|
||
const router = new Router(); | ||
|
||
const { validate, Authenticate } = middlewares; | ||
const { verifyToken } = Authenticate; | ||
|
||
const { | ||
addComment, getCommentById, deleteCommentById, updateCommentById, getAllCommentsOnRequest | ||
} = CommentsController; | ||
|
||
router.post('/requests/:requestId/comments', verifyToken, validate('addComment'), addComment); | ||
router.get('/comments/:commentId', verifyToken, getCommentById); | ||
router.patch('/comments/:commentId', verifyToken, validate('addComment'), updateCommentById); | ||
router.delete('/comments/:commentId', verifyToken, deleteCommentById); | ||
router.get('/requests/:requestId/comments', verifyToken, getAllCommentsOnRequest); | ||
|
||
|
||
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
Oops, something went wrong.