More endpoints

This commit is contained in:
gaba
2016-11-07 11:27:05 -08:00
parent cffee1ec18
commit 6f0698b950
3 changed files with 146 additions and 22 deletions
+14 -9
View File
@@ -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
});
+7 -3
View File
@@ -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;
+125 -10
View File
@@ -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()
})
})
})