Permalink
Browse files

api /topics/update

  • Loading branch information...
1 parent 390ac14 commit b08a11ee380ba09ec88d17968a85f6bb247f76ea @alsotang alsotang committed Sep 30, 2016
Showing with 110 additions and 4 deletions.
  1. +10 −1 Makefile
  2. +59 −1 api/v1/topic.js
  3. +1 −0 api_router_v1.js
  4. +1 −1 package.json
  5. +23 −1 test/api/v1/topic.test.js
  6. +16 −0 views/static/api.html
View
@@ -26,6 +26,14 @@ test: install pretest
--timeout $(TEST_TIMEOUT) \
$(TESTS)
+testfile:
+ @NODE_ENV=test ./node_modules/mocha/bin/mocha \
+ --reporter $(MOCHA_REPORTER) \
+ -r should \
+ -r test/env \
+ --timeout $(TEST_TIMEOUT) \
+ $(FILE)
+
test-cov cov: install pretest
@NODE_ENV=test node \
node_modules/.bin/istanbul cover --preserve-comments \
@@ -37,6 +45,7 @@ test-cov cov: install pretest
--timeout $(TEST_TIMEOUT) \
$(TESTS)
+
build:
@./node_modules/loader-builder/bin/builder views .
@@ -49,4 +58,4 @@ start: install build
restart: install build
@NODE_ENV=production ./node_modules/.bin/pm2 restart "cnode"
-.PHONY: install test cov test-cov build run start restart
+.PHONY: install test testfile cov test-cov build run start restart
View
@@ -130,7 +130,7 @@ var create = function (req, res, next) {
editError = '标题不能为空';
} else if (title.length < 5 || title.length > 100) {
editError = '标题字数太多或太少';
- } else if (!tab || allTabs.indexOf(tab) === -1) {
+ } else if (!tab || !_.includes(allTabs, tab)) {
editError = '必须选择一个版块';
} else if (content === '') {
editError = '内容不可为空';
@@ -171,3 +171,61 @@ var create = function (req, res, next) {
exports.create = create;
+exports.update = function (req, res, next) {
+ var topic_id = _.trim(req.body.topic_id);
+ var title = _.trim(req.body.title);
+ var tab = _.trim(req.body.tab);
+ var content = _.trim(req.body.content);
+
+ // 得到所有的 tab, e.g. ['ask', 'share', ..]
+ var allTabs = config.tabs.map(function (tPair) {
+ return tPair[0];
+ });
+
+ TopicProxy.getTopicById(topic_id, function (err, topic, tags) {
+ if (!topic) {
+ res.status(400);
+ return res.send({success: false, error_msg: '此话题不存在或已被删除。'});
+ }
+
+ if (topic.author_id.equals(req.user._id) || req.user.is_admin) {
+ // 验证
+ var editError;
+ if (title === '') {
+ editError = '标题不能是空的。';
+ } else if (title.length < 5 || title.length > 100) {
+ editError = '标题字数太多或太少。';
+ } else if (!tab || !_.includes(allTabs, tab)) {
+ editError = '必须选择一个版块。';
+ }
+ // END 验证
+
+ if (editError) {
+ return res.send({success: false, error_msg: editError});
+ }
+
+ //保存话题
+ topic.title = title;
+ topic.content = content;
+ topic.tab = tab;
+ topic.update_at = new Date();
+
+ topic.save(function (err) {
+ if (err) {
+ return next(err);
+ }
+ //发送at消息
+ at.sendMessageToMentionUsers(content, topic._id, req.user._id);
+
+ res.send({
+ success: true,
+ topic_id: topic.id
+ });
+ });
+ } else {
+ res.status(403)
+ return res.send({success: false, error_msg: '对不起,你不能编辑此话题。'});
+ }
+ });
+};
+
View
@@ -16,6 +16,7 @@ var router = express.Router();
router.get('/topics', topicController.index);
router.get('/topic/:id', middleware.tryAuth, topicController.show);
router.post('/topics', middleware.auth, limit.peruserperday('create_topic', config.create_post_per_day, true), topicController.create);
+router.post('/topics/update', middleware.auth, topicController.update);
// 主题收藏
View
@@ -27,7 +27,7 @@
"jpush-sdk": "3.2.1",
"loader-builder": "2.1.0",
"loader": "2.1.1",
- "lodash": "4.6.1",
+ "lodash": "4.16.2",
"log4js": "^0.6.29",
"markdown-it": "6.0.0",
"memory-cache": "0.1.4",
@@ -6,7 +6,9 @@ var support = require('../../support/support');
describe('test/api/v1/topic.test.js', function () {
var mockUser, mockTopic;
-
+
+ var createdTopicId = null;
+
before(function (done) {
support.createUser(function (err, user) {
mockUser = user;
@@ -98,6 +100,7 @@ describe('test/api/v1/topic.test.js', function () {
should.not.exists(err);
res.body.success.should.true();
res.body.topic_id.should.be.String();
+ createdTopicId = res.body.topic_id
done();
});
});
@@ -166,5 +169,24 @@ describe('test/api/v1/topic.test.js', function () {
});
});
+
+ describe('post /api/v1/topics/update', function () {
+ it('should update a topic', function (done) {
+ request.post('/api/v1/topics/update')
+ .send({
+ accesstoken: mockUser.accessToken,
+ topic_id: createdTopicId,
+ title: '我是API测试标题',
+ tab: 'share',
+ content: '我是API测试内容 /api/v1/topics/update'
+ })
+ .end(function (err, res) {
+ should.not.exists(err);
+ res.body.success.should.true();
+ res.body.topic_id.should.eql(createdTopicId);
+ done();
+ });
+ })
+ })
});
View
@@ -50,6 +50,22 @@
{success: true, topic_id: '5433d5e4e737cbe96dcef312'}
```
+#### post /topics/update 编辑主题
+
+接收 post 参数
+
+* accesstoken `String` 用户的 accessToken
+* topic_id `String` 主题id
+* title `String` 标题
+* tab `String` 目前有 `ask` `share` `job`
+* content `String` 主体内容
+
+返回值示例
+
+```js
+{success: true, topic_id: '5433d5e4e737cbe96dcef312'}
+```
+
### 主题收藏

0 comments on commit b08a11e

Please sign in to comment.