diff --git a/models/comment.js b/models/comment.js index d6a814e64..c82725855 100644 --- a/models/comment.js +++ b/models/comment.js @@ -1,5 +1,7 @@ const mongoose = require('../mongoose'); const uuid = require('uuid'); +const Action = require('./action'); + const Schema = mongoose.Schema; const CommentSchema = new Schema({ @@ -30,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}); }; /** @@ -44,6 +46,31 @@ CommentSchema.statics.findByAssetId = function(asset_id) { return Comment.find({asset_id}); }; +/** + * Change the status of a comment. + * @param {String} id identifier of the comment (uuid) + * @param {String} status the new status of the comment +*/ +CommentSchema.statics.changeStatus = function(id, status) { + return Comment.findOneAndUpdate({'id': id}, {$set: {'status': status}}, {upsert: false, new: true}); +}; + +/** + * Add an action to the comment. + * @param {String} id identifier of the comment (uuid) + * @param {String} action the new action to the comment +*/ +CommentSchema.statics.addAction = function(id, user_id, action_type) { + // check that the comment exist + let action = new Action({ + action_type: action_type, + item_type: 'comment', + item_id: id, + user_id: user_id + }); + return action.save(); +}; + const Comment = mongoose.model('Comment', CommentSchema); module.exports = Comment; diff --git a/package.json b/package.json index 856db50c6..9f60ab29e 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,6 @@ "homepage": "https://github.com/coralproject/talk#readme", "dependencies": { "body-parser": "^1.15.2", - "chai-http": "^3.0.0", "debug": "^2.2.0", "express": "^4.14.0", "mongoose": "^4.6.5", @@ -63,7 +62,7 @@ "babel-preset-es2015-minimal": "^2.1.0", "babel-preset-stage-0": "^6.16.0", "chai": "^3.5.0", - "chai-http": "^1.0.0", + "chai-http": "^3.0.0", "copy-webpack-plugin": "^3.0.1", "eslint": "^3.9.1", "exports-loader": "^0.6.3", diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 212c47ac6..b0458ada7 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -7,8 +7,12 @@ const router = express.Router(); // Routes //============================================================================== -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) => { @@ -20,35 +24,52 @@ 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 - }); + const {body, author_id, asset_id, parent_id, status} = req.body; + let comment = new Comment({body, author_id, asset_id, parent_id, status}); comment.save().then(({id}) => { - res.status(201).send(id); + res.status(200).send(id); }).catch(error => { next(error); }); - }); -router.put('/:comment_id', (req, res) => { - res.send('Update a comment'); +router.post('/:comment_id', (req, res, next) => { + Comment.findById(req.params.comment_id).then((comment) => { + 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; + return comment.save(); + }).then((comment) => { + res.status(200).send(comment); + }).catch(error => { + next(error); + }); }); -router.delete('/:comment_id', (req, res) => { - res.send('Delete a comment'); +router.delete('/:comment_id', (req, res, next) => { + Comment.remove(req.params.comment_id).then(() => { + res.status(201).send('OK. Deleted'); + }).catch(error => { + next(error); + }); }); -router.post('/:comment_id/status', (req, res) => { - res.send('Update a comment status'); +router.post('/:comment_id/status', (req, res, next) => { + 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 1115a189b..887ad679d 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -14,13 +14,65 @@ 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: '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('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); + done(); + }); + }); +}); + describe('Post /comments', () => { const users = [{ id: '123', - display_name: 'John', + display_name: 'Ana', }, { id: '456', - display_name: 'Paul', + display_name: 'Maria', }]; const actions = [{ @@ -40,13 +92,12 @@ 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(201); + expect(res).to.have.status(200); done(); }); }); - }); describe('Get /:comment_id', () => { @@ -68,10 +119,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 = [{ @@ -102,3 +153,223 @@ describe('Get /:comment_id', () => { }); }); }); + +describe('Put /: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: '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 comment', function(done) { + chai.request(app) + .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(); + }); + }); +}); + +describe('Delete /: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: '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 remove comment', function(done) { + chai.request(app) + .delete('/api/v1/comments/abc') + .end(function(err, res){ + expect(res).to.have.status(201); + done(); + }); + }); +}); + +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(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + expect(res).to.have.body; + 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 actions', function(done) { + chai.request(app) + .post('/api/v1/comments/abc/actions') + .send({'user_id': '456', 'action_type': 'flag'}) + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + done(); + }); + }); +});