Ai
2 Star 0 Fork 0

hashplus/ACdream

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
topic.js 2.16 KB
一键复制 编辑 原始数据 按行查看 历史
kidx 提交于 2014-06-06 11:07 +08:00 . models代码规范化
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var settings = require('../settings');
var pageNum = settings.topic_pageNum;
var OE = settings.outputErr;
function Topic(topic) {
this.id = topic.id;
this.title = topic.title;
this.content = topic.content;
this.user = topic.user;
this.cid = topic.cid;
this.inDate = topic.inDate;
};
module.exports = Topic;
var topicObj = new Schema({
id: {type: Number, index: {unique: true}},
title: String,
content: String,
user: String,
cid: Number,
inDate: Number,
reviewsQty: Number,
browseQty: Number,
top: Boolean,
lastReviewer: String,
lastReviewTime: Number,
lastComment: Number
});
mongoose.model('topics', topicObj);
var topics = mongoose.model('topics');
Topic.prototype.save = function(callback) {
topic = new topics();
topic.id = this.id;
topic.title = this.title;
topic.content = this.content;
topic.user = this.user;
topic.cid = this.cid;
topic.lastReviewTime = topic.inDate = this.inDate;
topic.reviewsQty = topic.browseQty = 0;
topic.top = false;
topic.save(function(err){
if (err) {
OE('Topic.save failed!');
}
return callback(err);
});
};
Topic.get = function(Q, page, callback) {
topics.count(Q, function(err, count){
if ((page-1)*pageNum > count) {
return callback(null, null, -1);
}
topics.find(Q).sort({top: -1, lastReviewTime: -1}).skip((page-1)*pageNum)
.limit(pageNum).exec(function(err, docs){
if (err) {
OE('Topic.get failed!');
}
return callback(err, docs, parseInt((count+pageNum-1)/pageNum, 10));
});
});
};
Topic.watch = function(tid, callback) {
topics.findOne({id: tid}, function(err, doc){
if (err) {
OE('Topic.watch failed!');
}
return callback(err, doc);
});
};
Topic.update = function(tid, H, callback) {
topics.update({id: tid}, H, function(err){
if (err) {
OE('Topic.update failed!');
}
return callback(err);
});
};
Topic.topFive = function(Q, callback) {
topics.find(Q).sort({lastReviewTime: -1}).limit(5).exec(function(err, docs){
if (err) {
OE('Topic.topFive failed!');
}
return callback(err, docs);
});
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hashplus/ACdream.git
git@gitee.com:hashplus/ACdream.git
hashplus
ACdream
ACdream
master

搜索帮助