Skip to content

Commit

Permalink
ft(comment): Complete endpoint for user to delete comment
Browse files Browse the repository at this point in the history
  • Loading branch information
richienabuk committed Sep 10, 2019
1 parent f42e6ba commit 5f8eb2a
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 153 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
],
"rules": {
"one-var": 0,
"arrow-parens": 0,
"one-var-declaration-per-line": 0,
"new-cap": 0,
"consistent-return": 0,
Expand Down
33 changes: 33 additions & 0 deletions public/docs/swaggerDoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,39 @@
}
}
}
},
"api/v1/comment/trips/:uuid": {
"delete": {
"tags": [
"Comment"
],
"summary": "Delete a user's comment",
"description": "Deletes a user's comment",
"consumes": "application/json",
"security": [
{
"bearerAuth": []
}
],
"parameters": [
{
"name": "uuid",
"in": "path",
"description": "uuid of comment to delete",
"required": true,
"type": "string"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Comment deleted successful",
"name": "type"
}
}
}
}
},
"definitions": {
Expand Down
28 changes: 26 additions & 2 deletions src/controllers/CommentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class CommentController {
const { uuid: userUuid } = req.userData;
const { message } = req.body;
const { commentUuid } = req.params;
const comment = await CommentRepository.findById({ uuid: commentUuid });
const comment = await CommentRepository.getOne({ uuid: commentUuid });
if (!comment) return sendErrorResponse(res, 404, 'This comment does not exist');
const { dataValues } = comment;
if (userUuid !== dataValues.user_uuid) return sendErrorResponse(res, 403, 'You can\'t edit a comment you didn\'t post');
Expand All @@ -76,7 +76,31 @@ class CommentController {
} catch (err) {
return next(err);
}
}
}

/**
* @description deletes the users comment from display
*
* @param {object} req contains the comment details and requester
*
* @param {object} res is the response object
*
* @param {function} next forwards request to the next middleware in the call stack
*
* @returns {object} it returns a response that is an object
*/
static async delete(req, res, next) {
try {
const { uuid } = req.params;
const comment = await CommentRepository.getOne({ uuid });
if (!comment) return sendErrorResponse(res, 404, 'This comment does not exist');
if (comment.dataValues.user_uuid !== req.userData.uuid) return sendErrorResponse(res, 401, 'You are not allowed to perform this action');
await CommentRepository.delete({ uuid });
return sendSuccessResponse(res, 200, { message: 'Comment deleted successful' });
} catch (err) {
return next(err);
}
}
}

export default CommentController;
8 changes: 6 additions & 2 deletions src/database/migrations/20190903210241-create-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ export default {
allowNull: false,
type: Sequelize.STRING
},
deletedAt: {
type: Sequelize.DATE,
field: 'deleted_at'
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
field: 'created_at'
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
field: 'updated_at'
}
}),
// eslint-disable-next-line arrow-parens
Expand Down
20 changes: 0 additions & 20 deletions src/database/seeders/20190829092037-create-trip-request.js

This file was deleted.

17 changes: 14 additions & 3 deletions src/database/seeders/20190831152452-create-trip-request.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import uuid from 'uuid/v4';

export default {
up: (queryInterface, Sequelize) => {
const TripRequests = [
{
uuid: 'ce03b266-93f3-48d3-8ec6-a2b9e51cc058',
user_uuid: 'abef6009-48be-4b38-80d0-b38c1bc39922',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
leaving_from: 'dbf285c6-8d7c-4f71-8058-a82e22e27f6b',
travel_reasons: 'Govt contract',
travel_date: '10-10-2019',
return_date: '11-11-2019',
created_at: Sequelize.literal('NOW()'),
updated_at: Sequelize.literal('NOW()')
},
{
uuid: '95ccd25d-2524-4b95-a441-8e2643c4c075',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
leaving_from: uuid(),
travel_date: '10-10-2019',
return_date: '10-11-2019',
created_at: Sequelize.literal('NOW()'),
updated_at: Sequelize.literal('NOW()')
},
{
uuid: 'a1383300-5edd-41ca-9435-68bcd52b89bd',
user_uuid: 'abef6009-48be-4b38-80d0-b38c1bc39922',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
leaving_from: 'dbf285c6-8d7c-4f71-8058-a82e22e27f6b',
travel_reasons: 'Movie festival',
travel_date: '10-10-2019',
Expand All @@ -23,7 +34,7 @@ export default {
},
{
uuid: '215c5fd4-e1e9-476a-a38c-f459ffa3d1ac',
user_uuid: '407d0d03-be0d-477c-badd-5df63b04307e',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
leaving_from: '85b4a64e-331b-4051-a32c-6bc20eb0fc81',
travel_reasons: 'Family discourse',
travel_date: '10-10-2019',
Expand Down
57 changes: 57 additions & 0 deletions src/database/seeders/20190907183906-create-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export default {
up: (queryInterface, Sequelize) => {
const UsersComments = [
{
uuid: 'efbb7998-13dd-44fa-941f-671de1078ff5',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
trip_request_uuid: '215c5fd4-e1e9-476a-a38c-f459ffa3d1ac',
message: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
created_at: Sequelize.literal('NOW()'),
updated_at: Sequelize.literal('NOW()')
},
{
uuid: 'c93d817a-f655-4b32-b838-6d9b930f837f',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
trip_request_uuid: '215c5fd4-e1e9-476a-a38c-f459ffa3d1ac',
message: 'Cursus risus at ultrices mi tempus imperdiet nulla malesuada. Egestas dui id ornare arcu. Volutpat maecenas volutpat blandit aliquam etiam erat velit scelerisque in.',
created_at: Sequelize.literal('NOW()'),
updated_at: Sequelize.literal('NOW()')
},
{
uuid: '6eb297d2-9e7b-4166-b50d-09e3a24d669d',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
trip_request_uuid: '215c5fd4-e1e9-476a-a38c-f459ffa3d1ac',
message: 'Morbi enim nunc faucibus a pellentesque sit amet.',
created_at: Sequelize.literal('NOW()'),
updated_at: Sequelize.literal('NOW()')
},
{
uuid: '0abc4bd5-92f3-47a9-b033-d17bb7bb363c',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
trip_request_uuid: '215c5fd4-e1e9-476a-a38c-f459ffa3d1ac',
message: 'Pharetra sit amet aliquam id diam. Integer enim neque volutpat ac tincidunt vitae. Et tortor consequat id porta nibh venenatis cras sed.',
created_at: Sequelize.literal('NOW()'),
updated_at: Sequelize.literal('NOW()')
},
{
uuid: '76db1220-5216-4b67-a2ff-e040b18d838e',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
trip_request_uuid: '215c5fd4-e1e9-476a-a38c-f459ffa3d1ac',
message: 'Suscipit tellus mauris a diam maecenas sed. Non diam phasellus vestibulum lorem. Dui id ornare arcu odio.',
created_at: Sequelize.literal('NOW()'),
updated_at: Sequelize.literal('NOW()')
},
{
uuid: '4e8bd454-6a1a-4148-9dd5-7baf8c9a916e',
user_uuid: '95ccd25d-2524-4b95-a441-8e2643c4c077',
trip_request_uuid: '215c5fd4-e1e9-476a-a38c-f459ffa3d1ac',
message: 'Amet consectetur adipiscing elit pellentesque. Nibh tellus molestie nunc non blandit. Volutpat maecenas volutpat blandit aliquam etiam. ',
created_at: Sequelize.literal('NOW()'),
updated_at: Sequelize.literal('NOW()')
}
];
return queryInterface.bulkInsert('Comments', UsersComments, {});
},
// eslint-disable-next-line arrow-parens
down: queryInterface => queryInterface.bulkDelete('Comments', null, {})
};
19 changes: 13 additions & 6 deletions src/models/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ export default (sequelize, DataTypes) => {
allowNull: false,
type: DataTypes.STRING
},
}, {});
deletedAt: {
type: DataTypes.DATE,
field: 'deleted_at'
},
createdAt: {
type: DataTypes.DATE,
field: 'created_at'
},
updatedAt: {
type: DataTypes.DATE,
field: 'updated_at'
},
}, { paranoid: true });
Comment.associate = (models) => {
Comment.belongsTo(models.TripRequest, {
as: 'tripDetails',
foreignKey: 'trip_request_uuid',
onDelete: 'CASCADE'
});
Comment.belongsTo(models.User, {
as: 'user',
foreignKey: 'user_uuid',
Expand Down
5 changes: 5 additions & 0 deletions src/models/TripRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export default (sequelize, DataTypes) => {
foreignKey: 'leaving_from',
as: 'departure'
});
TripRequest.hasMany(models.Comment, {
as: 'comments',
foreignKey: 'trip_request_uuid',
onDelete: 'CASCADE'
});
};
return TripRequest;
};
22 changes: 18 additions & 4 deletions src/repositories/CommentRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ class CommentRepository {
*
* @returns {object} the details of the request that was created
*/
async findById(condition) {
async getOne(condition) {
try {
const commentDetails = await this.db.findOne({ where: condition });
return commentDetails;
return await this.db.findOne({ where: condition });
} catch (err) {
throw new Error(err);
}
Expand All @@ -50,7 +49,7 @@ class CommentRepository {
*
* @param {object} data refers to the an object that contains the column/data you want to update
*
* @param {object} condition refers to the an object that contains the
* @param {object} condition refers to the an object that contains the
*
* @returns {object} the details of the comment that was created
*/
Expand All @@ -64,6 +63,21 @@ class CommentRepository {
throw new Error(err);
}
}

/**
* @description deletes a users comment
*
* @param {object} condition contains the reference to the comment to delete
*
* @returns {boolean} returns true when delete successful
*/
async delete(condition) {
try {
return await this.db.destroy({ where: condition });
} catch (err) {
throw new Error(err);
}
}
}

export default new CommentRepository();
2 changes: 1 addition & 1 deletion src/repositories/TripRequestRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class RequestRepository {
},
include: [{
model: TripDestination, as: 'destinations', attributes: ['uuid'], include: ['office']
}, 'departure']
}, 'departure', 'comments']
});
} catch (e) {
throw new Error(e);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const router = Router();

router.post('/comment/trips', authenticateUser, requestValidation.comment, CommentController.createTripRequestComment);
router.put('/comment/trips/:commentUuid', authenticateUser, requestValidation.comment, CommentController.editTripRequestComment);

router.delete('/comment/trips/:uuid', authenticateUser, CommentController.delete);

export default router;
Loading

0 comments on commit 5f8eb2a

Please sign in to comment.