mirror of
https://github.com/wassname/talk.git
synced 2026-06-29 03:55:09 +08:00
Move all DB functions into models. Clean up tests.
This commit is contained in:
+27
-1
@@ -28,7 +28,7 @@ ActionSchema.statics.findById = function(id) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds users in an array of ids.
|
||||
* Finds actions in an array of ids.
|
||||
* @param {String} ids array of user identifiers (uuid)
|
||||
*/
|
||||
ActionSchema.statics.findByItemIdArray = function(item_ids) {
|
||||
@@ -37,6 +37,32 @@ ActionSchema.statics.findByItemIdArray = function(item_ids) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds all comments for a specific action.
|
||||
* @param {String} action_type type of action
|
||||
* @param {String} item_type type of item the action is on
|
||||
*/
|
||||
ActionSchema.statics.findByType = function(action_type, item_type) {
|
||||
return Action.find({
|
||||
'action_type': action_type,
|
||||
'item_type': item_type
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds all comments ids for a specific action.
|
||||
* @param {String} action_type type of action
|
||||
* @param {String} item_type type of item the action is on
|
||||
*/
|
||||
ActionSchema.statics.findCommentsIdByActionType = function(action_type, item_type) {
|
||||
return Action.find({
|
||||
'action_type': action_type,
|
||||
'item_type': item_type
|
||||
},
|
||||
'item_id'
|
||||
);
|
||||
};
|
||||
|
||||
const Action = mongoose.model('Action', ActionSchema);
|
||||
|
||||
module.exports = Action;
|
||||
|
||||
@@ -31,6 +31,23 @@ const CommentSchema = new Schema({
|
||||
}
|
||||
});
|
||||
|
||||
//==============================================================================
|
||||
// New Statics
|
||||
//==============================================================================
|
||||
|
||||
/**
|
||||
* Create a comment.
|
||||
* @param {String} body content of comment
|
||||
*/
|
||||
CommentSchema.statics.new = function(body, author_id, asset_id, parent_id, status, username) {
|
||||
let comment = new Comment({body, author_id, asset_id, parent_id, status, username});
|
||||
return comment.save();
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
// Find Statics
|
||||
//==============================================================================
|
||||
|
||||
/**
|
||||
* Finds a comment by the id.
|
||||
* @param {String} id identifier of comment (uuid)
|
||||
@@ -47,6 +64,28 @@ CommentSchema.statics.findByAssetId = function(asset_id) {
|
||||
return Comment.find({asset_id});
|
||||
};
|
||||
|
||||
/**
|
||||
* Find comments by an action that was performed on them.
|
||||
* @param {String} action_type the type of action that was performed on the comment
|
||||
*/
|
||||
CommentSchema.statics.findByActionType = function(action_type) {
|
||||
return Action.findCommentsIdByActionType(action_type, 'comment').then((actions) => {
|
||||
return Comment.find({'id': {'$in': actions.map(function(a){return a.item_id;})}});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Find comments by their status.
|
||||
* @param {String} status the status to search for
|
||||
*/
|
||||
CommentSchema.statics.findByStatus = function(status) {
|
||||
return Comment.find({'status': status});
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
// Update Statics
|
||||
//==============================================================================
|
||||
|
||||
/**
|
||||
* Change the status of a comment.
|
||||
* @param {String} id identifier of the comment (uuid)
|
||||
@@ -72,6 +111,19 @@ CommentSchema.statics.addAction = function(id, user_id, action_type) {
|
||||
return action.save();
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
// Remove Statics
|
||||
//==============================================================================
|
||||
|
||||
/**
|
||||
* 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.removeById = function(id) {
|
||||
return Comment.remove({'id': id});
|
||||
};
|
||||
|
||||
const Comment = mongoose.model('Comment', CommentSchema);
|
||||
|
||||
module.exports = Comment;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
const express = require('express');
|
||||
const Comment = require('../../../models/comment');
|
||||
const Action = require('../../../models/action');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -29,17 +28,15 @@ router.get('/:comment_id', (req, res, next) => {
|
||||
//==============================================================================
|
||||
|
||||
router.get('/action/:action_type', (req, res, next) => {
|
||||
Action.find({'action_type': req.params.action_type, 'item_type': 'comment'}).then((actions) => {
|
||||
Comment.find({'id': {'$in': actions.map(function(a){return a.item_id;})}}).exec(function(err, comments){
|
||||
res.status(200).json(comments);
|
||||
});
|
||||
Comment.findByActionType(req.params.action_type).then((comments) => {
|
||||
res.status(200).json(comments);
|
||||
}).catch(error => {
|
||||
next(error);
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/status/rejected', (req, res, next) => {
|
||||
Comment.find({'status': 'rejected'}).then((comments) => {
|
||||
Comment.findByStatus('rejected').then((comments) => {
|
||||
res.status(200).json(comments);
|
||||
}).catch(error => {
|
||||
next(error);
|
||||
@@ -47,7 +44,7 @@ router.get('/status/rejected', (req, res, next) => {
|
||||
});
|
||||
|
||||
router.get('/status/pending', (req, res, next) => {
|
||||
Comment.find({'status': ''}).then((comments) => {
|
||||
Comment.findByStatus('').then((comments) => {
|
||||
res.status(200).json(comments);
|
||||
}).catch(error => {
|
||||
next(error);
|
||||
@@ -60,12 +57,18 @@ router.get('/status/pending', (req, res, next) => {
|
||||
|
||||
router.post('/', (req, res, next) => {
|
||||
const {body, author_id, asset_id, parent_id, status, username} = req.body;
|
||||
let comment = new Comment({body, author_id, asset_id, parent_id, status, username});
|
||||
comment.save().then(({id}) => {
|
||||
res.status(200).send({'id': id});
|
||||
Comment.new(body, author_id, asset_id, parent_id, status, username).then((comment) => {
|
||||
res.status(200).send({'id': comment.id});
|
||||
}).catch(error => {
|
||||
next(error);
|
||||
});
|
||||
|
||||
// let comment = new Comment({body, author_id, asset_id, parent_id, status, username});
|
||||
// comment.save().then(({id}) => {
|
||||
// res.status(200).send({'id': id});
|
||||
// }).catch(error => {
|
||||
// next(error);
|
||||
// });
|
||||
});
|
||||
|
||||
router.post('/:comment_id', (req, res, next) => {
|
||||
@@ -104,8 +107,8 @@ router.post('/:comment_id/actions', (req, res, next) => {
|
||||
//==============================================================================
|
||||
|
||||
router.delete('/:comment_id', (req, res, next) => {
|
||||
Comment.remove(req.params.comment_id).then(() => {
|
||||
res.status(201).send('OK. Deleted');
|
||||
Comment.removeById(req.params.comment_id).then(() => {
|
||||
res.status(201).send('OK. Removed');
|
||||
}).catch(error => {
|
||||
next(error);
|
||||
});
|
||||
|
||||
@@ -297,7 +297,7 @@ describe('Put /:comment_id', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Delete /:comment_id', () => {
|
||||
describe('Remove /:comment_id', () => {
|
||||
|
||||
const comments = [{
|
||||
id: 'abc',
|
||||
@@ -343,9 +343,10 @@ describe('Delete /:comment_id', () => {
|
||||
chai.request(app)
|
||||
.delete('/api/v1/comments/abc')
|
||||
.end(function(err, res){
|
||||
expect(err).to.be.null;
|
||||
expect(res).to.have.status(201);
|
||||
Comment.findById({'id': 'abc'}).then((comment) => {
|
||||
expect(comment).to.be.null;
|
||||
Comment.findById('abc').then((comment) => {
|
||||
expect(comment).to.be.empty;
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -405,8 +406,7 @@ describe('Post /:comment_id/status', () => {
|
||||
expect(err).to.be.null;
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.have.body;
|
||||
expect(res.body).to.have.property('status');
|
||||
expect(res.body.status).to.equal('accepted');
|
||||
expect(res.body).to.have.property('status', 'accepted');
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -465,14 +465,10 @@ describe('Post /:comment_id/actions', () => {
|
||||
expect(err).to.be.null;
|
||||
expect(res).to.have.status(200);
|
||||
expect(res).to.have.body;
|
||||
expect(res.body).to.have.property('item_type');
|
||||
expect(res.body.item_type).to.equal('comment');
|
||||
expect(res.body).to.have.property('action_type');
|
||||
expect(res.body.action_type).to.equal('flag');
|
||||
expect(res.body).to.have.property('item_id');
|
||||
expect(res.body.item_id).to.equal('abc');
|
||||
expect(res.body).to.have.property('user_id');
|
||||
expect(res.body.user_id).to.equal('456');
|
||||
expect(res.body).to.have.property('item_type', 'comment');
|
||||
expect(res.body).to.have.property('action_type', 'flag');
|
||||
expect(res.body).to.have.property('item_id', 'abc');
|
||||
expect(res.body).to.have.property('user_id', '456');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user