Skip to content

Commit 0770c0c

Browse files
committed
src: fix atom increase/decrease count issue
1 parent 2c97903 commit 0770c0c

File tree

7 files changed

+58
-43
lines changed

7 files changed

+58
-43
lines changed

app/controller/reply.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,13 @@ class ReplyController extends Controller {
3939
const user_id = ctx.user._id;
4040
const topicAuthor = await service.user.getUserById(topic.author_id);
4141
const newContent = content.replace('@' + topicAuthor.loginname + ' ', '');
42-
const [
43-
reply, user,
44-
] = await Promise.all([
45-
service.reply.newAndSave(content, topic_id, user_id, reply_id),
46-
service.user.getUserById(user_id),
47-
]);
42+
const reply = await service.reply.newAndSave(content, topic_id, user_id, reply_id);
4843

49-
user.score += 5;
50-
user.reply_count += 1;
5144
await Promise.all([
45+
service.user.incrementScoreAndReplyCount(user_id, 5, 1),
5246
service.topic.updateLastReply(topic_id, reply._id),
53-
user.save(),
5447
]);
48+
5549
service.at.sendMessageToMentionUsers(newContent, topic_id, user_id, reply._id);
5650
if (topic.author_id.toString() !== user_id.toString()) {
5751
await service.message.sendReplyMessage(topic.author_id, user_id, topic._id, reply._id);

app/controller/topic.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ class TopicController extends Controller {
3333

3434
const [ topic, author, replies ] = await service.topic.getFullTopic(topic_id);
3535

36+
// 增加 visit_count
3637
topic.visit_count += 1;
37-
await topic.save();
38+
// 写入 DB
39+
await service.topic.incrementVisitCount(topic_id);
3840

3941
topic.author = author;
4042
topic.replies = replies;
@@ -143,11 +145,7 @@ class TopicController extends Controller {
143145
);
144146

145147
// 发帖用户增加积分,增加发表主题数量
146-
const author = await service.user.getUserById(topic.author_id);
147-
author.score += 5;
148-
author.topic_count += 1;
149-
await author.save();
150-
ctx.user = author;
148+
await service.user.incrementScoreAndReplyCount(topic.author_id, 5, 1);
151149

152150
ctx.redirect('/topic/' + topic._id);
153151

@@ -389,13 +387,10 @@ class TopicController extends Controller {
389387
await service.topicCollect.newAndSave(ctx.user._id, topic._id);
390388
ctx.body = { status: 'success' };
391389

392-
const user = await service.user.getUserById(ctx.user._id);
393-
user.collect_topic_count += 1;
394-
await user.save();
395-
396-
ctx.user.collect_topic_count += 1;
397-
topic.collect_count += 1;
398-
await topic.save();
390+
await Promise.all([
391+
service.user.incrementCollectTopicCount(ctx.user._id),
392+
service.topic.incrementCollectCount(topic_id),
393+
]);
399394
}
400395

401396
/**

app/service/reply.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class ReplyService extends Service {
104104
getLastReplyByTopId(topicId) {
105105
const query = { topic_id: topicId, deleted: false };
106106
const opts = { sort: { create_at: -1 }, limit: 1 };
107-
return this.ctx.model.Reply.find(query, '_id', opts).exec();
107+
return this.ctx.model.Reply.findOne(query, '_id', opts).exec();
108108
}
109109

110110
getRepliesByAuthorId(authorId, opt = null) {

app/service/topic.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,16 @@ class TopicService extends Service {
113113
* @param {String} replyId 回复ID
114114
* @param {Function} callback 回调函数
115115
*/
116-
async updateLastReply(topicId, replyId) {
117-
const topic = await this.ctx.model.Topic.findOne({ _id: topicId }).exec();
118-
if (!topic) {
119-
return;
120-
}
121-
topic.last_reply = replyId;
122-
topic.last_reply_at = new Date();
123-
topic.reply_count += 1;
124-
return topic.save();
116+
updateLastReply(topicId, replyId) {
117+
const update = {
118+
last_reply: replyId,
119+
last_reply_at: new Date(),
120+
$inc: {
121+
reply_count: 1,
122+
},
123+
};
124+
const opts = { new: true };
125+
return this.ctx.model.Topic.findByIdAndUpdate(topicId, update, opts).exec();
125126
}
126127

127128
/*
@@ -138,20 +139,33 @@ class TopicService extends Service {
138139
* @param {String} id 主题ID
139140
*/
140141
async reduceCount(id) {
141-
const topic = await this.ctx.model.Topic.findOne({ _id: id }).exec();
142+
const update = { $inc: { reply_count: -1 } };
143+
const reply = await this.service.reply.getLastReplyByTopId(id);
144+
if (reply) {
145+
update.last_reply = reply._id;
146+
} else {
147+
update.last_reply = null;
148+
}
149+
const opts = { new: true };
150+
151+
const topic = await this.ctx.model.Topic.findByIdAndUpdate(id, update, opts).exec();
142152
if (!topic) {
143153
throw new Error('该主题不存在');
144154
}
145155

146-
topic.reply_count -= 1;
147-
const reply = await this.service.reply.getLastReplyByTopId(id);
148-
if (reply.length !== 0) {
149-
topic.last_reply = reply[0]._id;
150-
} else {
151-
topic.last_reply = null;
152-
}
156+
return topic;
157+
}
153158

154-
return topic.save();
159+
incrementVisitCount(id) {
160+
const query = { _id: id };
161+
const update = { $inc: { visit_count: 1 } };
162+
return this.ctx.model.Topic.findByIdAndUpdate(query, update).exec();
163+
}
164+
165+
incrementCollectCount(id) {
166+
const query = { _id: id };
167+
const update = { $inc: { collect_count: 1 } };
168+
return this.ctx.model.Topic.findByIdAndUpdate(query, update).exec();
155169
}
156170

157171
newAndSave(title, content, tab, authorId) {

app/service/user.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ class UserService extends Service {
8484
return this.ctx.model.User.findOne(query).exec();
8585
}
8686

87+
incrementScoreAndReplyCount(id, score, replyCount) {
88+
const query = { _id: id };
89+
const update = { $inc: { score, reply_count: replyCount } };
90+
return this.ctx.model.User.findByIdAndUpdate(query, update).exec();
91+
}
92+
93+
incrementCollectTopicCount(id) {
94+
const query = { _id: id };
95+
const update = { $inc: { collect_topic_count: 1 } };
96+
return this.ctx.model.User.findByIdAndUpdate(query, update).exec();
97+
}
98+
8799
newAndSave(name, loginname, pass, email, avatar_url, active) {
88100
const user = new this.ctx.model.User();
89101
user.name = loginname;

test/app/service/reply.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('reply service', () => {
9797

9898
const last_reply = await ctx.service.reply.getLastReplyByTopId(topic._id);
9999

100-
assert(last_reply[0]._id.toString() === reply._id.toString());
100+
assert(last_reply._id.toString() === reply._id.toString());
101101
});
102102

103103
it('getRepliesByAuthorId should ok', async function() {

test/app/service/topic.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('test/app/service/topic.test.js', () => {
4040

4141
it('updateLastReply should ok', async () => {
4242
const result1 = await topic.updateLastReply(topicId, replyId);
43-
assert(result1.last_reply === replyId);
43+
assert(result1.last_reply.toString() === replyId.toString());
4444
const result2 = await topic.updateLastReply();
4545
assert(!result2);
4646
});

0 commit comments

Comments
 (0)