diff --git a/models/comment.js b/models/comment.js index 468478810..5cf9f2aa0 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({ @@ -44,6 +46,33 @@ CommentSchema.statics.findByAssetId = function(asset_id) { return Comment.find({asset_id}); }; +/** + * Change the status of a comment. + * @param {String} comment_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(); +}; + +/** + * 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 + var item_type = 'comment'; + let action = new Action({ + action_type: action_type, + item_type: item_type, + item_id: id, + user_id: user_id + }); + return action.save(); +}; const Comment = mongoose.model('Comment', CommentSchema); diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index c468d427f..80107bad1 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -33,23 +33,44 @@ router.post('/', (req, res, next) => { status: req.query.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.put('/: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.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.findById(req.params.comment_id).then((comment) => { + comment.remove().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.query.status).then((comment) => { + res.status(200).send(comment); + }).catch(error => { + next(error); + }); }); router.post('/:comment_id/actions', (req, res) => { diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index db986db0c..7cce20d85 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -61,6 +61,7 @@ describe('Get /:comment_id', () => { .end(function(err, res){ expect(err).to.be.null; expect(res).to.have.status(200); + //expect(res).to.have.a.length(3); // it fails if (err) return done(err); done(); }); @@ -95,11 +96,10 @@ describe('Post /comments', () => { .post('/api/v1/comments') .query({'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', () => { @@ -154,7 +154,109 @@ describe('Get /:comment_id', () => { done(); }); }) - - - +}) + +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: '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('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': ''}) + .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: '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('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() + }) + }) })