${params.message}
`, + 更新了分类 ${cate.name} + 下的接口 ${interfaceData.title + }${params.message}
`, type: 'project', uid: this.getUid(), username: username, @@ -796,9 +792,8 @@ class interfaceController extends baseController { let project = await this.projectModel.getBaseInfo(interfaceData.project_id); - let interfaceUrl = `${ctx.request.origin}/project/${ - interfaceData.project_id - }/interface/api/${id}`; + let interfaceUrl = `${ctx.request.origin}/project/${interfaceData.project_id + }/interface/api/${id}`; yapi.commons.sendNotice(interfaceData.project_id, { title: `${username} 更新了接口`, @@ -876,9 +871,8 @@ class interfaceController extends baseController { let username = this.getUsername(); this.catModel.get(data.catid).then(cate => { yapi.commons.saveLog({ - content: `${username} 删除了分类 ${cate.name} 下的接口 "${data.title}"`, + content: `${username} 删除了分类 ${cate.name} 下的接口 "${data.title}"`, type: 'project', uid: this.getUid(), username: username, @@ -961,9 +955,8 @@ class interfaceController extends baseController { let username = this.getUsername(); yapi.commons.saveLog({ - content: `${username} 添加了分类 ${params.name}`, + content: `${username} 添加了分类 ${params.name}`, type: 'project', uid: this.getUid(), username: username, @@ -995,9 +988,8 @@ class interfaceController extends baseController { }); yapi.commons.saveLog({ - content: `${username} 更新了分类 ${cate.name}`, + content: `${username} 更新了分类 ${cate.name}`, type: 'project', uid: this.getUid(), username: username, @@ -1027,9 +1019,8 @@ class interfaceController extends baseController { let username = this.getUsername(); yapi.commons.saveLog({ - content: `${username} 删除了分类 "${ - catData.name - }" 及该分类下的接口`, + content: `${username} 删除了分类 "${catData.name + }" 及该分类下的接口`, type: 'project', uid: this.getUid(), username: username, @@ -1167,7 +1158,7 @@ class interfaceController extends baseController { params.forEach(item => { if (item.id) { this.Model.upIndex(item.id, item.index).then( - res => {}, + res => { }, err => { yapi.commons.log(err.message, 'error'); } @@ -1200,7 +1191,7 @@ class interfaceController extends baseController { params.forEach(item => { if (item.id) { this.catModel.upCatIndex(item.id, item.index).then( - res => {}, + res => { }, err => { yapi.commons.log(err.message, 'error'); } @@ -1264,6 +1255,85 @@ class interfaceController extends baseController { ctx.body = yapi.commons.resReturn(null, 402, err.message); } } + async copyCat(ctx) { + try { + let params = ctx.request.body; + let catid = params.catid; + + if (!catid) { + return (ctx.body = yapi.commons.resReturn(null, 400, 'catid cannot be empty')); + } + + let catData = await this.catModel.get(catid); + if (!catData) { + return (ctx.body = yapi.commons.resReturn(null, 400, 'Category not found')); + } + + if (!this.$tokenAuth) { + let auth = await this.checkAuth(catData.project_id, 'project', 'edit'); + if (!auth) { + return (ctx.body = yapi.commons.resReturn(null, 400, 'No Permission')); + } + } + + // 1. Create new Category + let newCatName = catData.name + '_copy'; + let newCat = await this.catModel.save({ + name: newCatName, + project_id: catData.project_id, + desc: catData.desc, + uid: this.getUid(), + add_time: yapi.commons.time(), + up_time: yapi.commons.time() + }); + + // 2. Get interfaces in old category + let interfaces = await this.Model.listByCatid(catid); + + // 3. Copy interfaces + for (let i = 0; i < interfaces.length; i++) { + let item = interfaces[i].toObject(); + let newItem = Object.assign({}, item); + delete newItem._id; + delete newItem.add_time; + delete newItem.up_time; + delete newItem.uid; + delete newItem.__v; + + newItem.catid = newCat._id; + newItem.uid = this.getUid(); + newItem.add_time = yapi.commons.time(); + newItem.up_time = yapi.commons.time(); + + // Handle path uniqueness within the same project + let checkRepeat = await this.Model.checkRepeat(newItem.project_id, newItem.path, newItem.method); + if (checkRepeat > 0) { + newItem.path = newItem.path + '_copy'; + let checkRepeat2 = await this.Model.checkRepeat(newItem.project_id, newItem.path, newItem.method); + if (checkRepeat2 > 0) { + newItem.path = newItem.path + '_' + yapi.commons.time(); + } + } + + await this.Model.save(newItem); + } + + let username = this.getUsername(); + yapi.commons.saveLog({ + content: `${username} 复制了分类 ${newCatName}`, + type: 'project', + uid: this.getUid(), + username: username, + typeid: catData.project_id + }); + + ctx.body = yapi.commons.resReturn(newCat); + + } catch (e) { + ctx.body = yapi.commons.resReturn(null, 402, e.message); + } + } } module.exports = interfaceController; diff --git a/server/router.js b/server/router.js index 4190596049..7d25f1e7bb 100755 --- a/server/router.js +++ b/server/router.js @@ -61,7 +61,7 @@ let routerConfig = { method: 'get' }, - + { action: 'list', path: 'list', @@ -363,6 +363,11 @@ let routerConfig = { path: 'del_cat', method: 'post' }, + { + action: 'copyCat', + path: 'copy_cat', + method: 'post' + }, { action: 'getCustomField', path: 'get_custom_field',