Skip to content

Commit

Permalink
feat(request comments): request comments
Browse files Browse the repository at this point in the history
- 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
oyewoas committed Sep 15, 2019
1 parent d580d86 commit 022760d
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/controllers/accommodationController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-expressions */
import models from '../models';
import { successResponse, errorResponse } from '../utils';

Expand Down
132 changes: 132 additions & 0 deletions src/controllers/commentsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import models from '../models';
import { successResponse, errorResponse, status } from '../utils';

const { Comments, Requests, Users } = models;

const association = [
{
model: Users,
as: 'theUser',
attributes: ['id', 'firstName', 'lastName', 'email']
}
];

/**
* @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.findOne({ where: { id: commentId }, include: association });
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 }, include: association });
return successResponse(res, status.success, 'Comments retrieved successfully', response);
} catch (error) {
return errorResponse(res, status.error, 'Error retrieving comments');
}
}
}

export default CommentsController;
3 changes: 2 additions & 1 deletion src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import UsersController from './users';
import ResetPasswordController from './resetPassword';
import AccommodationController from './accommodationController';
import RoomController from './roomController';
import CommentsController from './commentsController';

export {
UsersController, ResetPasswordController,
AccommodationController, RoomController
AccommodationController, RoomController, CommentsController
};
29 changes: 29 additions & 0 deletions src/database/seeders/create-6-request.js
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, {})
};

29 changes: 29 additions & 0 deletions src/database/seeders/create-7-comments.js
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, {})
};

3 changes: 2 additions & 1 deletion src/models/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default (sequelize, DataTypes) => {
},
}, {});
Comment.associate = (models) => {
Comment.belongsTo(models.Users, { as: 'theComment', foreignKey: 'userId' });
Comment.belongsTo(models.Users, { as: 'theUser', foreignKey: 'userId' });
Comment.belongsTo(models.Requests, { as: 'theRequest', foreignKey: 'requestId' });
};
return Comment;
};
21 changes: 21 additions & 0 deletions src/routes/api/comments.js
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.put('/comments/:commentId', verifyToken, validate('addComment'), updateCommentById);
router.delete('/comments/:commentId', verifyToken, deleteCommentById);
router.get('/requests/:requestId/comments', verifyToken, getAllCommentsOnRequest);


export default router;
4 changes: 4 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import profileRoutes from './api/profile';
import resetPasswordRoute from './api/resetPassword';
import accommodationRoute from './api/accommodation';
import roomRoute from './api/room';
import commentsRoute from './api/comments';


const router = new Router();

Expand All @@ -14,5 +16,7 @@ router.use('/', roomRoute);
router.use('/auth', authRoutes);
router.use('/users', userRoute);
router.use('/profiles', profileRoutes);
router.use('/', commentsRoute);


export default router;
1 change: 1 addition & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/prefer-default-export */
import sendEmail from './autoMailer';

export { sendEmail };
Loading

0 comments on commit 022760d

Please sign in to comment.