-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: post topic/create #69
Conversation
Codecov Report
@@ Coverage Diff @@
## master #69 +/- ##
=========================================
Coverage ? 89.25%
=========================================
Files ? 35
Lines ? 1238
Branches ? 0
=========================================
Hits ? 1105
Misses ? 133
Partials ? 0
Continue to review full report at Codecov.
|
|
||
if (ctx.status === 302) { | ||
// 新建话题成功 | ||
// TODO: 此处正确做法应该是使用redis的原子操作 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个地方应该用incr
操作的,对redis库不熟,就暂时这样实现了,后面抽空看一下。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incr 后面不是实现了吗。可以改改。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app/controller/topic.js
Outdated
const { body } = ctx.request; | ||
const title = (_.isString(body.title) && body.title.trim()) || ''; | ||
const tab = (_.isString(body.tab) && body.tab.trim()) || ''; | ||
const content = (_.isString(body.t_content) && body.t_content.trim()) || ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
减少对 lodash 的依赖
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参数验证还是用 egg-validate
吧
|
||
if (ctx.status === 302) { | ||
// 新建话题成功 | ||
// TODO: 此处正确做法应该是使用redis的原子操作 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incr 后面不是实现了吗。可以改改。
app/controller/topic.js
Outdated
const { body } = ctx.request; | ||
const title = (_.isString(body.title) && body.title.trim()) || ''; | ||
const tab = (_.isString(body.tab) && body.tab.trim()) || ''; | ||
const content = (_.isString(body.t_content) && body.t_content.trim()) || ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#70 看下这里的示例。
app/controller/topic.js
Outdated
const { body } = ctx.request; | ||
const title = (_.isString(body.title) && body.title.trim()) || ''; | ||
const tab = (_.isString(body.tab) && body.tab.trim()) || ''; | ||
const content = (_.isString(body.t_content) && body.t_content.trim()) || ''; | ||
|
||
// 得到所有的 tab, e.g. ['ask', 'share', ..] | ||
const allTabs = tabs.map(function(tPair) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const allTabs = tabs.map(tPair => tPair[0])
麻烦做下 rebase 吧。 |
$ git checkout topic/create
$ git remote add cnode https://github.com/cnodejs/egg-cnode.git
$ get fetch cnode
$ git rebase -i cnode/master 除了第一条外,其余全部用 f |
@JacksonTian ,好的,你说的这个方法太好用了! |
app/controller/topic.js
Outdated
// 通知被@的用户 | ||
await service.at.sendMessageToMentionUsers( | ||
content, | ||
topic._id, | ||
ctx.user._id | ||
); | ||
|
||
await ctx.redirect('/topic/' + topic._id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redirect 可以不用 await 的。不过这个好像不是你写的。
app/controller/topic.js
Outdated
const tab = ctx.request.body.tab.trim(); | ||
const content = ctx.request.body.t_content.trim(); | ||
const { body } = ctx.request; | ||
const title = (_.isString(body.title) && body.title.trim()) || ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
减少对 lodash 的依赖。
const YYYYMMDD = moment().format('YYYYMMDD'); | ||
const key = `topic_per_user_per_day_${user._id}_${YYYYMMDD}`; | ||
|
||
let currentCount = await service.cache.get(key) || 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个好像有歧义吧。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已改
module.exports = options => { | ||
|
||
return async function(ctx, next) { | ||
const { perDayPerUserLimitCount = 10 } = options || {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里改成箭头函数吧,默认值 {}
移动到参数里面
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
试过这样,但看起来并不简洁呢
const perDayPerUserLimitCount = ({ perDayPerUserLimitCount = 10 }) => (perDayPerUserLimitCount)(options);
改成了现在这样
app/controller/topic.js
Outdated
|
||
// 储存新主题帖 | ||
const topic = await service.topic.newAndSave( | ||
title, | ||
content, | ||
t_content, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个地方叫 t_content 的原因是?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最初的代码是这样,前端是这样传的吧,我可以把前端一起改了
app/controller/topic.js
Outdated
tab, | ||
ctx.user._id | ||
); | ||
|
||
ctx.logger.info(topic); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这句日志是必要的吗
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
忘记去掉了
Checklist
npm test
passesAffected core subsystem(s)
topic/create
Description of change
refactor: finish post topic/create