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 12, 2019
1 parent 3b53b80 commit a5f86fe
Show file tree
Hide file tree
Showing 12 changed files with 623 additions and 22 deletions.
124 changes: 124 additions & 0 deletions src/controllers/commentsController.js
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;
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, {})
};

1 change: 1 addition & 0 deletions src/models/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default (sequelize, DataTypes) => {
}, {});
Comment.associate = (models) => {
Comment.belongsTo(models.Users, { as: 'theComment', foreignKey: 'userId' });
Comment.belongsTo(models.Requests, { as: 'theRequest', foreignKey: 'requestId' });
};
return Comment;
};
2 changes: 1 addition & 1 deletion src/models/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default (sequelize, DataTypes) => {
},
}, {});
Request.associate = (models) => {
Request.hasMany(models.Comments, { as: 'requestComments', foreignKey: 'reqId' });
Request.hasMany(models.Comments, { as: 'requestComments', foreignKey: 'requestId' });
Request.hasMany(models.Trips, { as: 'requestTrips', foreignKey: 'reqId' });
};
return Request;
Expand Down
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.patch('/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 @@ -4,6 +4,8 @@ import authRoutes from './api/auth';
import resetPasswordRoute from './api/resetPassword';
import accommodationRoute from './api/accommodation';
import roomRoute from './api/room';
import commentsRoute from './api/comments';


const router = express.Router();

Expand All @@ -12,5 +14,7 @@ router.use('/users', userRoute);
router.use('/', resetPasswordRoute);
router.use('/', accommodationRoute);
router.use('/', roomRoute);
router.use('/', commentsRoute);


export default router;
Loading

0 comments on commit a5f86fe

Please sign in to comment.