From 9916edc1277e3854d651a9ff2465ea1dfbfcd3e2 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 15:10:28 -0700 Subject: [PATCH 1/6] Adds implementation for get all comments --- routes/api/comments/index.js | 8 +++-- tests/routes/api/comments/index.js | 54 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index df9bbc456..c468d427f 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -8,8 +8,12 @@ const router = express.Router(); //============================================================================== -router.get('/', (req, res) => { - res.send('Read all of the comments ever'); +router.get('/', (req, res, next) => { + Comment.find({}).then((comments) => { + res.status(200).json(comments); + }).catch(error => { + next(error); + }); }); router.get('/:comment_id', (req, res, next) => { diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index ad21e54ec..db986db0c 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -13,6 +13,60 @@ const Comment = require('../../../../models/comment'); const Action = require('../../../../models/action'); const User = require('../../../../models/user'); + +describe('Get /:comment_id', () => { + const comments = [{ + id: 'abc', + body: 'comment 10', + asset_id: 'asset', + author_id: '123' + },{ + id: 'def', + body: 'comment 20', + asset_id: 'asset', + author_id: '456' + },{ + id: 'hij', + body: 'comment 30', + asset_id: '456' + }] + + const users = [{ + id: '123', + display_name: 'John', + },{ + id: '456', + display_name: 'Paul', + }] + + 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('should return all the comments', function(done){ + chai.request(app) + .get('/api/v1/comments') + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + if (err) return done(err); + done(); + }); + }) +}) + describe('Post /comments', () => { const users = [{ id: '123', From 6f0698b950d1cbfabb8fa137bfc124fcbac57d8f Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 11:27:05 -0800 Subject: [PATCH 2/6] 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() + }) + }) +}) From 38692ccd303a7842f66f8aaacbc024185e79bf3a Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 11:28:41 -0800 Subject: [PATCH 3/6] Ignore dist for lint --- .eslintignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintignore b/.eslintignore index b051c6c57..6cb6a3bc3 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ client +dist From 16e7a6c868c1dccaee449299ae333a3ef88ec4c0 Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 11:34:34 -0800 Subject: [PATCH 4/6] CHanges from query to body for post/put. --- routes/api/comments/index.js | 22 +++++++++++----------- tests/routes/api/comments/index.js | 6 +++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 2c9daa73f..428369ae1 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -26,11 +26,11 @@ router.get('/:comment_id', (req, res, next) => { router.post('/', (req, res, next) => { let comment = new Comment({ - body: req.query.body, - author_id: req.query.author_id, - asset_id: req.query.asset_id, - parent_id: req.query.parent_id, - status: req.query.status + body: req.body.body, + author_id: req.body.author_id, + asset_id: req.body.asset_id, + parent_id: req.body.parent_id, + status: req.body.status }); comment.save().then(({id}) => { res.status(200).send(id); @@ -39,13 +39,13 @@ router.post('/', (req, res, next) => { }); }); -router.put('/:comment_id', (req, res, next) => { +router.post('/:comment_id', (req, res, next) => { Comment.findById(req.params.comment_id).then((comment) => { - comment.body = req.query.body; - comment.author_id = req.query.author_id; - comment.asset_id = req.query.asset_id; - comment.parent_id = req.query.parent_id; - comment.status = req.query.status; + comment.body = req.body.body; + comment.author_id = req.body.author_id; + comment.asset_id = req.body.asset_id; + comment.parent_id = req.body.parent_id; + comment.status = req.body.status; comment.save().then((comment) => { res.status(200).send(comment); diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index ac8cc5327..c3694d95e 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -94,7 +94,7 @@ describe('Post /comments', () => { it('it should create a comment', function(done) { chai.request(app) .post('/api/v1/comments') - .query({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''}) + .send({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''}) .end(function(err, res){ expect(res).to.have.status(200) done() @@ -200,8 +200,8 @@ describe('Put /:comment_id', () => { it('it should update comment', function(done) { chai.request(app) - .put('/api/v1/comments/abc') - .query({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''}) + .post('/api/v1/comments/abc') + .send({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''}) .end(function(err, res){ expect(res).to.have.status(200) done() From fc80ea2c3ad0f8414c75839388545354f616b48f Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 11:49:02 -0800 Subject: [PATCH 5/6] Check it has body --- tests/routes/api/comments/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index c3694d95e..4945195a0 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -313,6 +313,7 @@ describe('Post /:comment_id/status', () => { .send({'status': 'accepted'}) .end(function(res){ expect(res).to.have.status(200) + expect(res).to.have.body done() }) }) From 9e70b86687879313c86eeb0dce74fd40c6bc736a Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 12:29:07 -0800 Subject: [PATCH 6/6] Fix tests. --- tests/routes/api/comments/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 5fb28a8e7..887ad679d 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -308,7 +308,8 @@ describe('Post /:comment_id/status', () => { chai.request(app) .post('/api/v1/comments/abc/status') .send({'status': 'accepted'}) - .end(function(res){ + .end(function(err, res){ + expect(err).to.be.null; expect(res).to.have.status(200); expect(res).to.have.body; done(); @@ -365,7 +366,8 @@ describe('Post /:comment_id/actions', () => { chai.request(app) .post('/api/v1/comments/abc/actions') .send({'user_id': '456', 'action_type': 'flag'}) - .end(function(res){ + .end(function(err, res){ + expect(err).to.be.null; expect(res).to.have.status(200); done(); });