zmh
2 years ago
3 changed files with 149 additions and 0 deletions
@ -0,0 +1,119 @@ |
|||
'use strict'; |
|||
|
|||
async function getTags(ctx) { |
|||
try { |
|||
const models = ctx.fs.dc.models; |
|||
const rslt = await models.TagSet.findAll({ |
|||
attributes: [['id', 'tagSetId'], ['name', 'tagSetName']], |
|||
include: [{ |
|||
model: models.Tag, |
|||
}], |
|||
order: [['id', 'asc']], |
|||
}); |
|||
ctx.status = 200; |
|||
ctx.body = rslt; |
|||
} catch (error) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|||
ctx.status = 400; |
|||
ctx.body = { |
|||
"message": "获取标签列表失败" |
|||
} |
|||
} |
|||
} |
|||
|
|||
async function postTagSets(ctx) { |
|||
try { |
|||
const { name } = ctx.request.body |
|||
const models = ctx.fs.dc.models; |
|||
const postOne = await models.TagSet.findOne({ where: { name } }); |
|||
if (postOne) { |
|||
ctx.status = 400; |
|||
ctx.body = { message: '该标签集名已存在' } |
|||
} else { |
|||
await models.TagSet.create({ name }); |
|||
ctx.body = { message: '添加标签集成功' } |
|||
ctx.status = 200; |
|||
} |
|||
} catch (error) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|||
ctx.status = 400; |
|||
ctx.body = { |
|||
"message": "添加标签集失败" |
|||
} |
|||
} |
|||
} |
|||
|
|||
async function putTagSets(ctx) { |
|||
try { |
|||
const { id } = ctx.params; |
|||
const { name } = ctx.request.body; |
|||
const models = ctx.fs.dc.models; |
|||
let tagSetInfo = await models.TagSet.findOne({ where: { id } }); |
|||
if (tagSetInfo) { |
|||
const putTOne = await models.TagSet.findOne({ where: { id: { $not: id }, name: name } }); |
|||
if (putTOne) { |
|||
ctx.status = 400; |
|||
ctx.body = { message: '该标签集名称已存在' } |
|||
} else { |
|||
await models.TagSet.update({ name: name }, { where: { id: id } }); |
|||
ctx.status = 200; |
|||
ctx.body = { message: '修改标签集成功' } |
|||
} |
|||
} else { |
|||
ctx.status = 400; |
|||
ctx.body = { message: '该标签集不存在' } |
|||
} |
|||
} catch (error) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|||
ctx.status = 400; |
|||
ctx.body = { |
|||
"message": "修改标签集失败" |
|||
} |
|||
} |
|||
} |
|||
|
|||
async function deleteTagSets(ctx) { |
|||
const transaction = await ctx.fs.dc.orm.transaction(); |
|||
try { |
|||
const models = ctx.fs.dc.models; |
|||
const { id } = ctx.params; |
|||
let tagInfo = await models.Tag.findAll({ attributes: ['id'], where: { tagSet: id } }); |
|||
let tagIds = tagInfo.map(r => r.id); |
|||
let deletable = true; |
|||
if (tagIds.length) { |
|||
let tagDatabaseInfo = await models.MetadataDatabase.findOne({ where: { tag: { $in: tagIds } } }); |
|||
let tagFileInfo = await models.MetadataDatabase.findOne({ where: { tag: { $in: tagIds } } }); |
|||
let tagRestapiInfo = await models.MetadataDatabase.findOne({ where: { tag: { $in: tagIds } } }); |
|||
if (tagDatabaseInfo || tagFileInfo || tagRestapiInfo) { |
|||
ctx.status = 400; |
|||
ctx.body = { message: '已打标元数据,禁止删除' } |
|||
deletable = false; |
|||
} |
|||
} |
|||
if (deletable) { |
|||
await models.Tag.destroy({ |
|||
where: { tagSet: id }, |
|||
transaction |
|||
}) |
|||
await models.TagSet.destroy({ |
|||
where: { id: id }, |
|||
transaction |
|||
}) |
|||
await transaction.commit(); |
|||
ctx.status = 200; |
|||
ctx.body = { message: '删除标签集成功' } |
|||
} |
|||
} catch (error) { |
|||
await transaction.rollback(); |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|||
ctx.status = 400; |
|||
ctx.body = { message: '删除标签集失败' } |
|||
} |
|||
} |
|||
|
|||
module.exports = { |
|||
getTags, |
|||
postTagSets, |
|||
putTagSets, |
|||
deleteTagSets |
|||
} |
@ -0,0 +1,27 @@ |
|||
'use strict'; |
|||
|
|||
const tags = require('../../controllers/tags'); |
|||
|
|||
module.exports = function (app, router, opts) { |
|||
app.fs.api.logAttr['GET/tags'] = { content: '获取标签集列表', visible: false }; |
|||
router.get('/tags', tags.getTags); |
|||
|
|||
app.fs.api.logAttr['POST /tag-sets'] = { content: '新增标签集', visible: true }; |
|||
router.post('/tag-sets', tags.postTagSets); |
|||
|
|||
app.fs.api.logAttr['PUT /tag-sets/:id'] = { content: '修改标签集', visible: true }; |
|||
router.put('/tag-sets/:id', tags.putTagSets); |
|||
|
|||
app.fs.api.logAttr['DEL /tag-sets/:id'] = { content: '删除标签集', visible: true }; |
|||
router.delete('/tag-sets/:id', tags.deleteTagSets); |
|||
|
|||
// app.fs.api.logAttr['POST /tags'] = { content: '新增标签', visible: true };
|
|||
// router.post('/tags', tags.postTags);
|
|||
|
|||
// app.fs.api.logAttr['PUT /tags/:id'] = { content: '修改标签', visible: true };
|
|||
// router.put('/tags/:id', tags.putTags);
|
|||
|
|||
// app.fs.api.logAttr['DEL /tags/:id'] = { content: '删除标签', visible: true };
|
|||
// router.delete('/tags/:id', tags.delTags);
|
|||
|
|||
}; |
Loading…
Reference in new issue