mirror of
https://github.com/cnodejs/nodeclub.git
synced 2025-12-08 19:55:55 +00:00
增加主题收藏的相关接口
This commit is contained in:
parent
ddfff93ba0
commit
e1c09fae8e
@ -1,6 +1,7 @@
|
||||
var models = require('../../models');
|
||||
var TopicModel = models.Topic;
|
||||
var TopicProxy = require('../../proxy').Topic;
|
||||
var TopicCollect = require('../../proxy').TopicCollect;
|
||||
var UserProxy = require('../../proxy').User;
|
||||
var UserModel = models.User;
|
||||
var config = require('../../config');
|
||||
@ -145,3 +146,74 @@ var create = function (req, res, next) {
|
||||
};
|
||||
|
||||
exports.create = create;
|
||||
|
||||
exports.collect = function (req, res, next) {
|
||||
var topic_id = req.body.topic_id;
|
||||
TopicProxy.getTopic(topic_id, function (err, topic) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (!topic) {
|
||||
res.json({error_msg: '主题不存在'});
|
||||
}
|
||||
|
||||
TopicCollect.getTopicCollect(req.session.user._id, topic._id, function (err, doc) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (doc) {
|
||||
res.json({success: true});
|
||||
return;
|
||||
}
|
||||
|
||||
TopicCollect.newAndSave(req.session.user._id, topic._id, function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.json({success: true});
|
||||
});
|
||||
UserProxy.getUserById(req.session.user._id, function (err, user) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
user.collect_topic_count += 1;
|
||||
user.save();
|
||||
});
|
||||
|
||||
req.session.user.collect_topic_count += 1;
|
||||
topic.collect_count += 1;
|
||||
topic.save();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
exports.de_collect = function (req, res, next) {
|
||||
var topic_id = req.body.topic_id;
|
||||
TopicProxy.getTopic(topic_id, function (err, topic) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (!topic) {
|
||||
res.json({error_msg: '主题不存在'});
|
||||
}
|
||||
TopicCollect.remove(req.session.user._id, topic._id, function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.json({success: true});
|
||||
});
|
||||
|
||||
UserProxy.getUserById(req.session.user._id, function (err, user) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
user.collect_topic_count -= 1;
|
||||
user.save();
|
||||
});
|
||||
|
||||
topic.collect_count -= 1;
|
||||
topic.save();
|
||||
|
||||
req.session.user.collect_topic_count -= 1;
|
||||
});
|
||||
};
|
||||
|
||||
@ -3,6 +3,7 @@ var eventproxy = require('eventproxy');
|
||||
var UserProxy = require('../../proxy').User;
|
||||
var TopicProxy = require('../../proxy').Topic;
|
||||
var ReplyProxy = require('../../proxy').Reply;
|
||||
var TopicCollect = require('../../proxy').TopicCollect;
|
||||
|
||||
var show = function (req, res, next) {
|
||||
var loginname = req.params.loginname;
|
||||
@ -31,8 +32,20 @@ var show = function (req, res, next) {
|
||||
TopicProxy.getTopicsByQuery(query, opt, ep.done('recent_replies'));
|
||||
}));
|
||||
|
||||
ep.all('recent_topics', 'recent_replies',
|
||||
function (recent_topics, recent_replies) {
|
||||
TopicCollect.getTopicCollectsByUserId(user._id,
|
||||
ep.done(function (collections) {
|
||||
var topic_ids = [];
|
||||
for (var i = 0; i < collections.length; i++) {
|
||||
if (topic_ids.indexOf(collections[i].topic_id.toString()) < 0) {
|
||||
topic_ids.push(collections[i].topic_id.toString());
|
||||
}
|
||||
}
|
||||
var query = {_id: {'$in': topic_ids}};
|
||||
var opt = {sort: '-create_at'};
|
||||
TopicProxy.getTopicsByQuery(query, opt, ep.done('collect_topics'));
|
||||
}));
|
||||
ep.all('recent_topics', 'recent_replies', 'collect_topics',
|
||||
function (recent_topics, recent_replies, collect_topics) {
|
||||
|
||||
user = _.pick(user, ['loginname', 'avatar_url', 'githubUsername',
|
||||
'create_at', 'score']);
|
||||
@ -47,6 +60,11 @@ var show = function (req, res, next) {
|
||||
topic = _.pick(topic, ['id', 'author', 'title', 'last_reply_at']);
|
||||
return topic;
|
||||
});
|
||||
user.collect_topics = collect_topics.map(function (topic) {
|
||||
topic.author = _.pick(topic.author, ['loginname', 'avatar_url']);
|
||||
topic = _.pick(topic, ['id', 'author', 'title', 'last_reply_at']);
|
||||
return topic;
|
||||
});
|
||||
|
||||
res.send({data: user});
|
||||
});
|
||||
|
||||
@ -13,6 +13,8 @@ var router = express.Router();
|
||||
router.get('/topics', topicController.index);
|
||||
router.get('/topic/:id', topicController.show);
|
||||
router.post('/topics', middleware.auth, topicController.create);
|
||||
router.post('/topic/collect', middleware.auth, topicController.collect); // 关注某话题
|
||||
router.post('/topic/de_collect', middleware.auth, topicController.de_collect); // 取消关注某话题
|
||||
|
||||
// 用户
|
||||
router.get('/user/:loginname', userController.show);
|
||||
|
||||
@ -45,6 +45,32 @@
|
||||
{success: true, topic_id: '5433d5e4e737cbe96dcef312'}
|
||||
```
|
||||
|
||||
#### post /topic/collect 收藏主题
|
||||
|
||||
接收 post 参数
|
||||
|
||||
* accesstoken `String` 用户的 accessToken
|
||||
* topic_id `String` 被收藏的主题id
|
||||
|
||||
返回值示例
|
||||
|
||||
```js
|
||||
{success: true}
|
||||
```
|
||||
|
||||
#### post /topic/de_collect 取消收藏
|
||||
|
||||
接收 post 参数
|
||||
|
||||
* accesstoken `String` 用户的 accessToken
|
||||
* topic_id `String` 被取消收藏的主题id
|
||||
|
||||
返回值示例
|
||||
|
||||
```js
|
||||
{success: true}
|
||||
```
|
||||
|
||||
### 评论
|
||||
|
||||
#### post /topic/:topic_id/replies 新建评论
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user