From 6f0698b950d1cbfabb8fa137bfc124fcbac57d8f Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 11:27:05 -0800 Subject: [PATCH] More endpoints --- models/comment.js | 23 +++-- routes/api/comments/index.js | 10 ++- tests/routes/api/comments/index.js | 135 ++++++++++++++++++++++++++--- 3 files changed, 146 insertions(+), 22 deletions(-) diff --git a/models/comment.js b/models/comment.js index 5cf9f2aa0..644ffb6d0 100644 --- a/models/comment.js +++ b/models/comment.js @@ -32,10 +32,10 @@ const CommentSchema = new Schema({ /** * Finds a comment by the id. - * @param {String} asset_id identifier of comment (uuid) + * @param {String} id identifier of comment (uuid) */ CommentSchema.statics.findById = function(id) { - return Comment.findOne({id}); + return Comment.findOne({'id': id}); }; /** @@ -48,13 +48,19 @@ CommentSchema.statics.findByAssetId = function(asset_id) { /** * Change the status of a comment. - * @param {String} comment_id identifier of the comment (uuid) + * @param {String} id identifier of the comment (uuid) * @param {String} status the new status of the comment */ CommentSchema.statics.changeStatus = function(id, status) { - var comment = Comment.findOne({id}); - comment.status = status; - return comment.save(); + return Comment.update({'id': id}, {$set: {'status': status}}, {upsert: false}).then(() => { + Comment.findById(id).then((comment) => { + return comment; + }).catch((err) => { + console.log('Error updating status for the comment.', err); + }); + }).catch((err) => { + console.log('Error updating status for the comment.', err); + }); }; /** @@ -64,10 +70,9 @@ CommentSchema.statics.changeStatus = function(id, status) { */ CommentSchema.statics.addAction = function(id, user_id, action_type) { // check that the comment exist - var item_type = 'comment'; - let action = new Action({ + var action = new Action({ action_type: action_type, - item_type: item_type, + item_type: 'comment', item_id: id, user_id: user_id }); diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 80107bad1..2c9daa73f 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -66,15 +66,19 @@ router.delete('/:comment_id', (req, res, next) => { }); router.post('/:comment_id/status', (req, res, next) => { - Comment.changeStatus(req.params.comment_id, req.query.status).then((comment) => { + Comment.changeStatus(req.params.comment_id, req.body.status).then((comment) => { res.status(200).send(comment); }).catch(error => { next(error); }); }); -router.post('/:comment_id/actions', (req, res) => { - res.send('Add a comment action'); +router.post('/:comment_id/actions', (req, res, next) => { + Comment.addAction(req.params.comment_id, req.body.user_id, req.body.action_type).then((action) => { + res.status(200).send(action); + }).catch(error => { + next(error); + }); }); module.exports = router; diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 7cce20d85..ac8cc5327 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -33,10 +33,10 @@ describe('Get /:comment_id', () => { const users = [{ id: '123', - display_name: 'John', + display_name: 'Ana', },{ id: '456', - display_name: 'Paul', + display_name: 'Maria', }] const actions = [{ @@ -71,10 +71,10 @@ describe('Get /:comment_id', () => { describe('Post /comments', () => { const users = [{ id: '123', - display_name: 'John', + display_name: 'Ana', },{ id: '456', - display_name: 'Paul', + display_name: 'Maria', }] const actions = [{ @@ -121,10 +121,10 @@ describe('Get /:comment_id', () => { const users = [{ id: '123', - display_name: 'John', + display_name: 'Ana', },{ id: '456', - display_name: 'Paul', + display_name: 'Maria', }] const actions = [{ @@ -176,10 +176,10 @@ describe('Put /:comment_id', () => { const users = [{ id: '123', - display_name: 'John', + display_name: 'Ana', },{ id: '456', - display_name: 'Paul', + display_name: 'Maria', }] const actions = [{ @@ -229,10 +229,10 @@ describe('Delete /:comment_id', () => { const users = [{ id: '123', - display_name: 'John', + display_name: 'Ana', },{ id: '456', - display_name: 'Paul', + display_name: 'Maria', }] const actions = [{ @@ -260,3 +260,118 @@ describe('Delete /:comment_id', () => { }) }) }) + + +describe('Post /:comment_id/status', () => { + + const comments = [{ + id: 'abc', + body: 'comment 10', + asset_id: 'asset', + author_id: '123', + status: '' + },{ + id: 'def', + body: 'comment 20', + asset_id: 'asset', + author_id: '456', + status: 'rejected' + },{ + id: 'hij', + body: 'comment 30', + asset_id: '456', + status: 'accepted' + }] + + const users = [{ + id: '123', + display_name: 'Ana', + },{ + id: '456', + display_name: 'Maria', + }] + + const actions = [{ + action_type: 'flag', + item_id: 'abc' + },{ + action_type: 'like', + item_id: 'hij' + }] + + beforeEach(() => { + return Comment.create(comments).then(() => { + return User.create(users) + }).then(() => { + return Action.create(actions) + }) + }) + + it('it should update status', function(done) { + chai.request(app) + .post('/api/v1/comments/abc/status') + .send({'status': 'accepted'}) + .end(function(res){ + expect(res).to.have.status(200) + done() + }) + }) +}) + + + +describe('Post /:comment_id/actions', () => { + + const comments = [{ + id: 'abc', + body: 'comment 10', + asset_id: 'asset', + author_id: '123', + status: '' + },{ + id: 'def', + body: 'comment 20', + asset_id: 'asset', + author_id: '456', + status: 'rejected' + },{ + id: 'hij', + body: 'comment 30', + asset_id: '456', + status: 'accepted' + }] + + const users = [{ + id: '123', + display_name: 'Ana', + },{ + id: '456', + display_name: 'Maria', + }] + + const actions = [{ + action_type: 'flag', + item_id: 'abc' + },{ + action_type: 'like', + item_id: 'hij' + }] + + beforeEach(() => { + return Comment.create(comments).then(() => { + return User.create(users) + }).then(() => { + return Action.create(actions) + }) + }) + + it('it should update status', function(done) { + chai.request(app) + .post('/api/v1/comments/abc/actions') + .send({'user_id': '456', 'action_type': 'flag'}) + .end(function(res){ + expect(res).to.have.status(200) + done() + }) + }) +})