|
| 1 | +import models from '../models'; |
| 2 | +import { successResponse, errorResponse, status } from '../utils'; |
| 3 | + |
| 4 | +const { Comments, Requests, Users } = models; |
| 5 | + |
| 6 | +const association = [ |
| 7 | + { |
| 8 | + model: Users, |
| 9 | + as: 'theUser', |
| 10 | + attributes: ['id', 'firstName', 'lastName', 'email'] |
| 11 | + } |
| 12 | +]; |
| 13 | + |
| 14 | +/** |
| 15 | + * @class CommentsController |
| 16 | + * @description Controllers for handling travel requests comments |
| 17 | + * @exports CommentsController |
| 18 | + */ |
| 19 | +class CommentsController { |
| 20 | + /** |
| 21 | + * @method addComment |
| 22 | + * @description Method to add comment to requests |
| 23 | + * @param {object} req - The Request Object |
| 24 | + * @param {object} res - The Response Object |
| 25 | + * @returns {object} Newly added request comment |
| 26 | + */ |
| 27 | + static async addComment(req, res) { |
| 28 | + const { userId } = req.user; |
| 29 | + const { requestId } = req.params; |
| 30 | + const { comment } = req.body; |
| 31 | + try { |
| 32 | + const existingRequest = await Requests.findOne({ |
| 33 | + where: { id: requestId } |
| 34 | + }); |
| 35 | + if (!existingRequest) { |
| 36 | + return errorResponse(res, status.notfound, 'Request does not exist'); |
| 37 | + } |
| 38 | + const commentAdded = await Comments.create({ comment, userId, requestId }); |
| 39 | + const response = commentAdded.toJSON(); |
| 40 | + return successResponse(res, status.created, 'Comment added successfully', response); |
| 41 | + } catch (error) { |
| 42 | + return errorResponse(res, status.error, 'Error adding comment'); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @method getSingleComment |
| 48 | + * @description Method to get a comment |
| 49 | + * @param {object} req - The Request Object |
| 50 | + * @param {object} res - The Response Object |
| 51 | + * @returns {object} retrieved comment details |
| 52 | + */ |
| 53 | + static async getCommentById(req, res) { |
| 54 | + const { commentId } = req.params; |
| 55 | + try { |
| 56 | + const getComment = await Comments.findOne({ where: { id: commentId }, include: association }); |
| 57 | + if (!getComment) { |
| 58 | + return errorResponse(res, status.notfound, 'Comment not found'); |
| 59 | + } |
| 60 | + const response = getComment.toJSON(); |
| 61 | + return successResponse(res, status.success, 'Comment retrieved successfully', response); |
| 62 | + } catch (error) { |
| 63 | + return errorResponse(res, status.error, 'Error retrieving comment'); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @method updateComment |
| 69 | + * @description Method to update comment |
| 70 | + * @param {object} req - The Request Object |
| 71 | + * @param {object} res - The Response Object |
| 72 | + * @returns {object} updated comment details |
| 73 | + */ |
| 74 | + static async updateCommentById(req, res) { |
| 75 | + const { commentId } = req.params; |
| 76 | + const { comment } = req.body; |
| 77 | + try { |
| 78 | + const getComment = await Comments.findOne({ where: { id: commentId } }); |
| 79 | + if (!getComment) { |
| 80 | + return errorResponse(res, status.notfound, 'Comment not found'); |
| 81 | + } |
| 82 | + await Comments.update({ comment }, { where: { id: commentId } }); |
| 83 | + return successResponse(res, status.success, 'Comment updated successfully'); |
| 84 | + } catch (error) { |
| 85 | + return errorResponse(res, status.error, 'Error updating comment'); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @method deleteComment |
| 91 | + * @description Method to delete comment |
| 92 | + * @param {object} req - The Request Object |
| 93 | + * @param {object} res - The Response Object |
| 94 | + * @returns {object} deleted comment details |
| 95 | + */ |
| 96 | + static async deleteCommentById(req, res) { |
| 97 | + const { commentId } = req.params; |
| 98 | + try { |
| 99 | + const getComment = await Comments.findByPk(commentId); |
| 100 | + if (!getComment) { |
| 101 | + return errorResponse(res, status.notfound, 'Comment not found'); |
| 102 | + } |
| 103 | + await Comments.destroy({ where: { id: commentId } }); |
| 104 | + return successResponse(res, status.success, 'Comment deleted successfully'); |
| 105 | + } catch (error) { |
| 106 | + return errorResponse(res, status.error, 'Error deleting comment'); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @method getAllCommentsOnRequest |
| 112 | + * @description Method to get all comments on requests |
| 113 | + * @param {object} req - The Request Object |
| 114 | + * @param {object} res - The Response Object |
| 115 | + * @returns {object} retrieved comments details |
| 116 | + */ |
| 117 | + static async getAllCommentsOnRequest(req, res) { |
| 118 | + const { requestId } = req.params; |
| 119 | + try { |
| 120 | + const existingRequest = await Requests.findOne({ where: { id: requestId } }); |
| 121 | + if (!existingRequest) { |
| 122 | + return errorResponse(res, status.notfound, 'Request not found'); |
| 123 | + } |
| 124 | + const response = await Comments.findAll({ where: { requestId }, include: association }); |
| 125 | + return successResponse(res, status.success, 'Comments retrieved successfully', response); |
| 126 | + } catch (error) { |
| 127 | + return errorResponse(res, status.error, 'Error retrieving comments'); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export default CommentsController; |
0 commit comments