Skip to content

Commit b08a11e

Browse files
committed
api /topics/update
1 parent 390ac14 commit b08a11e

File tree

6 files changed

+110
-4
lines changed

6 files changed

+110
-4
lines changed

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ test: install pretest
2626
--timeout $(TEST_TIMEOUT) \
2727
$(TESTS)
2828

29+
testfile:
30+
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
31+
--reporter $(MOCHA_REPORTER) \
32+
-r should \
33+
-r test/env \
34+
--timeout $(TEST_TIMEOUT) \
35+
$(FILE)
36+
2937
test-cov cov: install pretest
3038
@NODE_ENV=test node \
3139
node_modules/.bin/istanbul cover --preserve-comments \
@@ -37,6 +45,7 @@ test-cov cov: install pretest
3745
--timeout $(TEST_TIMEOUT) \
3846
$(TESTS)
3947

48+
4049
build:
4150
@./node_modules/loader-builder/bin/builder views .
4251

@@ -49,4 +58,4 @@ start: install build
4958
restart: install build
5059
@NODE_ENV=production ./node_modules/.bin/pm2 restart "cnode"
5160

52-
.PHONY: install test cov test-cov build run start restart
61+
.PHONY: install test testfile cov test-cov build run start restart

api/v1/topic.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ var create = function (req, res, next) {
130130
editError = '标题不能为空';
131131
} else if (title.length < 5 || title.length > 100) {
132132
editError = '标题字数太多或太少';
133-
} else if (!tab || allTabs.indexOf(tab) === -1) {
133+
} else if (!tab || !_.includes(allTabs, tab)) {
134134
editError = '必须选择一个版块';
135135
} else if (content === '') {
136136
editError = '内容不可为空';
@@ -171,3 +171,61 @@ var create = function (req, res, next) {
171171

172172
exports.create = create;
173173

174+
exports.update = function (req, res, next) {
175+
var topic_id = _.trim(req.body.topic_id);
176+
var title = _.trim(req.body.title);
177+
var tab = _.trim(req.body.tab);
178+
var content = _.trim(req.body.content);
179+
180+
// 得到所有的 tab, e.g. ['ask', 'share', ..]
181+
var allTabs = config.tabs.map(function (tPair) {
182+
return tPair[0];
183+
});
184+
185+
TopicProxy.getTopicById(topic_id, function (err, topic, tags) {
186+
if (!topic) {
187+
res.status(400);
188+
return res.send({success: false, error_msg: '此话题不存在或已被删除。'});
189+
}
190+
191+
if (topic.author_id.equals(req.user._id) || req.user.is_admin) {
192+
// 验证
193+
var editError;
194+
if (title === '') {
195+
editError = '标题不能是空的。';
196+
} else if (title.length < 5 || title.length > 100) {
197+
editError = '标题字数太多或太少。';
198+
} else if (!tab || !_.includes(allTabs, tab)) {
199+
editError = '必须选择一个版块。';
200+
}
201+
// END 验证
202+
203+
if (editError) {
204+
return res.send({success: false, error_msg: editError});
205+
}
206+
207+
//保存话题
208+
topic.title = title;
209+
topic.content = content;
210+
topic.tab = tab;
211+
topic.update_at = new Date();
212+
213+
topic.save(function (err) {
214+
if (err) {
215+
return next(err);
216+
}
217+
//发送at消息
218+
at.sendMessageToMentionUsers(content, topic._id, req.user._id);
219+
220+
res.send({
221+
success: true,
222+
topic_id: topic.id
223+
});
224+
});
225+
} else {
226+
res.status(403)
227+
return res.send({success: false, error_msg: '对不起,你不能编辑此话题。'});
228+
}
229+
});
230+
};
231+

api_router_v1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var router = express.Router();
1616
router.get('/topics', topicController.index);
1717
router.get('/topic/:id', middleware.tryAuth, topicController.show);
1818
router.post('/topics', middleware.auth, limit.peruserperday('create_topic', config.create_post_per_day, true), topicController.create);
19+
router.post('/topics/update', middleware.auth, topicController.update);
1920

2021

2122
// 主题收藏

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"jpush-sdk": "3.2.1",
2828
"loader-builder": "2.1.0",
2929
"loader": "2.1.1",
30-
"lodash": "4.6.1",
30+
"lodash": "4.16.2",
3131
"log4js": "^0.6.29",
3232
"markdown-it": "6.0.0",
3333
"memory-cache": "0.1.4",

test/api/v1/topic.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ var support = require('../../support/support');
66
describe('test/api/v1/topic.test.js', function () {
77

88
var mockUser, mockTopic;
9-
9+
10+
var createdTopicId = null;
11+
1012
before(function (done) {
1113
support.createUser(function (err, user) {
1214
mockUser = user;
@@ -98,6 +100,7 @@ describe('test/api/v1/topic.test.js', function () {
98100
should.not.exists(err);
99101
res.body.success.should.true();
100102
res.body.topic_id.should.be.String();
103+
createdTopicId = res.body.topic_id
101104
done();
102105
});
103106
});
@@ -166,5 +169,24 @@ describe('test/api/v1/topic.test.js', function () {
166169
});
167170

168171
});
172+
173+
describe('post /api/v1/topics/update', function () {
174+
it('should update a topic', function (done) {
175+
request.post('/api/v1/topics/update')
176+
.send({
177+
accesstoken: mockUser.accessToken,
178+
topic_id: createdTopicId,
179+
title: '我是API测试标题',
180+
tab: 'share',
181+
content: '我是API测试内容 /api/v1/topics/update'
182+
})
183+
.end(function (err, res) {
184+
should.not.exists(err);
185+
res.body.success.should.true();
186+
res.body.topic_id.should.eql(createdTopicId);
187+
done();
188+
});
189+
})
190+
})
169191

170192
});

views/static/api.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@
5050
{success: true, topic_id: '5433d5e4e737cbe96dcef312'}
5151
```
5252

53+
#### post /topics/update 编辑主题
54+
55+
接收 post 参数
56+
57+
* accesstoken `String` 用户的 accessToken
58+
* topic_id `String` 主题id
59+
* title `String` 标题
60+
* tab `String` 目前有 `ask` `share` `job`
61+
* content `String` 主体内容
62+
63+
返回值示例
64+
65+
```js
66+
{success: true, topic_id: '5433d5e4e737cbe96dcef312'}
67+
```
68+
5369

5470
### 主题收藏
5571

0 commit comments

Comments
 (0)