diff --git a/api/app/lib/controllers/tags/index.js b/api/app/lib/controllers/tags/index.js index c2e7848..6148b9c 100644 --- a/api/app/lib/controllers/tags/index.js +++ b/api/app/lib/controllers/tags/index.js @@ -1,5 +1,5 @@ 'use strict'; - +//获取标签集列表 async function getTags(ctx) { try { const models = ctx.fs.dc.models; @@ -8,7 +8,7 @@ async function getTags(ctx) { include: [{ model: models.Tag, }], - order: [['id', 'asc']], + order: [['id', 'asc'], [models.Tag, 'id', 'asc']], }); ctx.status = 200; ctx.body = rslt; @@ -20,7 +20,7 @@ async function getTags(ctx) { } } } - +//新增标签集 async function postTagSets(ctx) { try { const { name } = ctx.request.body @@ -28,7 +28,7 @@ async function postTagSets(ctx) { const postOne = await models.TagSet.findOne({ where: { name } }); if (postOne) { ctx.status = 400; - ctx.body = { message: '该标签集名已存在' } + ctx.body = { message: '该标签集名称已存在' } } else { await models.TagSet.create({ name }); ctx.body = { message: '添加标签集成功' } @@ -42,7 +42,7 @@ async function postTagSets(ctx) { } } } - +//修改标签集 async function putTagSets(ctx) { try { const { id } = ctx.params; @@ -50,8 +50,8 @@ async function putTagSets(ctx) { 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) { + const putOne = await models.TagSet.findOne({ where: { id: { $not: id }, name: name } }); + if (putOne) { ctx.status = 400; ctx.body = { message: '该标签集名称已存在' } } else { @@ -71,37 +71,43 @@ async function putTagSets(ctx) { } } } - -async function deleteTagSets(ctx) { +//删除标签集 +async function delTagSets(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; + let tagSetInfo = await models.TagSet.findOne({ where: { id } }); + if (tagSetInfo) { + 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.TagDatabase.findOne({ where: { tag: { $in: tagIds } } }); + let tagFileInfo = await models.TagFile.findOne({ where: { tag: { $in: tagIds } } }); + let tagRestapiInfo = await models.TagRestapi.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: '删除标签集成功' } + 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: '删除标签集成功' } + } + } else { + ctx.status = 400; + ctx.body = { message: '该标签集不存在' } } } catch (error) { await transaction.rollback(); @@ -110,10 +116,92 @@ async function deleteTagSets(ctx) { ctx.body = { message: '删除标签集失败' } } } +//新增标签 +async function postTags(ctx) { + try { + const { tagSetId, name } = ctx.request.body + const models = ctx.fs.dc.models; + const postOne = await models.Tag.findOne({ where: { name } }); + if (postOne) { + ctx.status = 400; + ctx.body = { message: '该标签名称已存在' } + } else { + await models.Tag.create({ name: name, tagSet: tagSetId }); + 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 putTags(ctx) { + try { + const { id } = ctx.params; + const { name } = ctx.request.body; + const models = ctx.fs.dc.models; + let tagInfo = await models.Tag.findOne({ where: { id } }); + if (tagInfo) { + const putOne = await models.Tag.findOne({ where: { id: { $not: id }, name: name } }); + if (putOne) { + ctx.status = 400; + ctx.body = { message: '该标签名称已存在' } + } else { + await models.Tag.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 delTags(ctx) { + try { + const models = ctx.fs.dc.models; + const { id } = ctx.params; + let tagInfo = await models.Tag.findOne({ where: { id } }); + if (tagInfo) { + let tagDatabaseInfo = await models.TagDatabase.findOne({ where: { tag: id } }); + let tagFileInfo = await models.TagFile.findOne({ where: { tag: id } }); + let tagRestapiInfo = await models.TagRestapi.findOne({ where: { tag: id } }); + if (tagDatabaseInfo || tagFileInfo || tagRestapiInfo) { + ctx.status = 400; + ctx.body = { message: '该标签已打标元数据,禁止删除' } + } else { + await models.Tag.destroy({ 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: '删除标签失败' } + } +} module.exports = { getTags, postTagSets, putTagSets, - deleteTagSets + delTagSets, + postTags, + putTags, + delTags } \ No newline at end of file diff --git a/api/app/lib/routes/tags/index.js b/api/app/lib/routes/tags/index.js index 1b5c495..86ca386 100644 --- a/api/app/lib/routes/tags/index.js +++ b/api/app/lib/routes/tags/index.js @@ -13,15 +13,15 @@ module.exports = function (app, router, opts) { 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); + router.delete('/tag-sets/:id', tags.delTagSets); - // app.fs.api.logAttr['POST /tags'] = { content: '新增标签', visible: true }; - // router.post('/tags', tags.postTags); + 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['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); + app.fs.api.logAttr['DEL /tags/:id'] = { content: '删除标签', visible: true }; + router.delete('/tags/:id', tags.delTags); }; \ No newline at end of file