Commit 40f56068 authored by 刘文胜's avatar 刘文胜

1111

parent 39c167aa
......@@ -300,4 +300,96 @@ exports.getPopulateCommentById=function(cid,callback){
callback(null ,c);
}
});
};
//我的评论
var countMyComment = function(conditions,callback) {
ForumComment.find(conditions)
.count(conditions, function (err, count) {
if(err){
console.error(err);
callback(err,null);
}else{
callback(null,count);
}
});
};
function populateParentComment(doc, callback){
if (doc && doc._id && doc.level === 2) {
ForumComment.findOne({comments: { $elemMatch : doc._id },status:1}).exec(function(err,c){
if (err) {
console.error(err);
callback(null, null);
} else{
if(c){
callback(null ,c);
}else{
callback(null);
}
}
});
} else{
callback(null, null);
}
}
//我的评论
exports.getMyComment = function(conditions,pageNo,pageSize,callback){
countMyComment(conditions,function(err,count){
if(err){
callback(err);
}else{
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumComment.find(conditions)
.populate({
path: 'thread',
select: '_id content title type level tag'
}).populate({path:'from to', select:'uid nickName icon'}).limit(limit).skip(skip).sort('-created').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err,null);
} else {
var obj={};
obj.total=count;
obj.pageNo=pageNo;
obj.pageSize=pageSize;
obj.items=docs;
if (docs && docs.length > 0) {
var asyncTasks = [];
docs.forEach(function(doc){
asyncTasks.push(function(callback) {
populateParentComment(doc, function(err,c){
if(err){
callback(err,null);
}else{
var newobj = doc.toObject();
newobj.parent = c;
delete newobj.comments;
callback(null,newobj);
}
});
});
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.log(err);
callback(null, null);
} else {
obj.items=results;
callback(null,obj);
}
});
}else{
callback(null,obj);
}
}
});
}
});
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment