Skip to content

Commit

Permalink
refactor: improve setting function
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 29, 2017
1 parent 9a0c195 commit f165474
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
12 changes: 12 additions & 0 deletions assets/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
"en": "mock",
"zh": "mock"
},
{
"category": "basic",
"name": "setting",
"en": "setting",
"zh": "配置"
},
{
"category": "basic",
"name": "name",
Expand Down Expand Up @@ -149,6 +155,12 @@
"en": "Please Login Your Account",
"zh": "请登录您的账号"
},
{
"category": "user",
"name": "notAllowAcess",
"en": "This feature does not allow the current user to use",
"zh": "此功能不允许当前用户使用"
},
{
"category": "user",
"name": "account",
Expand Down
18 changes: 10 additions & 8 deletions controllers/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const _ = require('lodash');

const settingService = require('../services/setting');

const validateSchema = {
name: Joi.string().max(30).required(),
data: Joi.object(),
disabled: Joi.boolean(),
description: Joi.string(),
};

/**
* 增加一个系统配置
Expand All @@ -25,10 +31,7 @@ const settingService = require('../services/setting');
* @return {Body} {Setting}
*/
exports.add = async function add(ctx) {
const data = Joi.validate(ctx.request.body, {
name: Joi.string().max(30).required(),
data: Joi.object(),
});
const data = Joi.validate(ctx.request.body, validateSchema);
data.creator = ctx.session.user.account;
const doc = await settingService.add(data);
ctx.status = 201;
Expand Down Expand Up @@ -89,10 +92,9 @@ exports.get = async function get(ctx) {
*/
exports.update = async function update(ctx) {
const id = Joi.attempt(ctx.params.id, Joi.objectId());
const data = Joi.validate(ctx.request.body, {
disabled: Joi.boolean(),
data: Joi.object(),
});
const data = Joi.validate(ctx.request.body, _.extend({
name: Joi.string().max(30),
}, _.omit(validateSchema, ['name'])));
if (!_.isEmpty(data)) {
await settingService.findByIdAndUpdate(id, data);
}
Expand Down
1 change: 1 addition & 0 deletions models/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ module.exports = {
},
// mongodb-update 的plugin会自动增加该字段
updatedAt: String,
description: String,
},
};

0 comments on commit f165474

Please sign in to comment.