Skip to content

Commit

Permalink
api /topics/update
Browse files Browse the repository at this point in the history
  • Loading branch information
alsotang committed Sep 30, 2016
1 parent 390ac14 commit b08a11e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -37,6 +45,7 @@ test-cov cov: install pretest
--timeout $(TEST_TIMEOUT) \
$(TESTS)


build:
@./node_modules/loader-builder/bin/builder views .

Expand All @@ -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
60 changes: 59 additions & 1 deletion api/v1/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '内容不可为空';
Expand Down Expand Up @@ -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: '对不起,你不能编辑此话题。'});
}
});
};

1 change: 1 addition & 0 deletions api_router_v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);


// 主题收藏
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 23 additions & 1 deletion test/api/v1/topic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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();
});
})
})

});
16 changes: 16 additions & 0 deletions views/static/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
```


### 主题收藏

Expand Down

0 comments on commit b08a11e

Please sign in to comment.