Browse Source

标签增删改api 暂交、待验证

master
zmh 2 years ago
parent
commit
12c9ee1a9e
  1. 116
      api/app/lib/controllers/tags/index.js
  2. 14
      api/app/lib/routes/tags/index.js

116
api/app/lib/controllers/tags/index.js

@ -1,5 +1,5 @@
'use strict'; 'use strict';
//获取标签集列表
async function getTags(ctx) { async function getTags(ctx) {
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
@ -8,7 +8,7 @@ async function getTags(ctx) {
include: [{ include: [{
model: models.Tag, model: models.Tag,
}], }],
order: [['id', 'asc']], order: [['id', 'asc'], [models.Tag, 'id', 'asc']],
}); });
ctx.status = 200; ctx.status = 200;
ctx.body = rslt; ctx.body = rslt;
@ -20,7 +20,7 @@ async function getTags(ctx) {
} }
} }
} }
//新增标签集
async function postTagSets(ctx) { async function postTagSets(ctx) {
try { try {
const { name } = ctx.request.body const { name } = ctx.request.body
@ -28,7 +28,7 @@ async function postTagSets(ctx) {
const postOne = await models.TagSet.findOne({ where: { name } }); const postOne = await models.TagSet.findOne({ where: { name } });
if (postOne) { if (postOne) {
ctx.status = 400; ctx.status = 400;
ctx.body = { message: '该标签集名已存在' } ctx.body = { message: '该标签集名已存在' }
} else { } else {
await models.TagSet.create({ name }); await models.TagSet.create({ name });
ctx.body = { message: '添加标签集成功' } ctx.body = { message: '添加标签集成功' }
@ -42,7 +42,7 @@ async function postTagSets(ctx) {
} }
} }
} }
//修改标签集
async function putTagSets(ctx) { async function putTagSets(ctx) {
try { try {
const { id } = ctx.params; const { id } = ctx.params;
@ -50,8 +50,8 @@ async function putTagSets(ctx) {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
let tagSetInfo = await models.TagSet.findOne({ where: { id } }); let tagSetInfo = await models.TagSet.findOne({ where: { id } });
if (tagSetInfo) { if (tagSetInfo) {
const putTOne = await models.TagSet.findOne({ where: { id: { $not: id }, name: name } }); const putOne = await models.TagSet.findOne({ where: { id: { $not: id }, name: name } });
if (putTOne) { if (putOne) {
ctx.status = 400; ctx.status = 400;
ctx.body = { message: '该标签集名称已存在' } ctx.body = { message: '该标签集名称已存在' }
} else { } else {
@ -71,22 +71,24 @@ async function putTagSets(ctx) {
} }
} }
} }
//删除标签集
async function deleteTagSets(ctx) { async function delTagSets(ctx) {
const transaction = await ctx.fs.dc.orm.transaction(); const transaction = await ctx.fs.dc.orm.transaction();
try { try {
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const { id } = ctx.params; const { id } = ctx.params;
let tagSetInfo = await models.TagSet.findOne({ where: { id } });
if (tagSetInfo) {
let tagInfo = await models.Tag.findAll({ attributes: ['id'], where: { tagSet: id } }); let tagInfo = await models.Tag.findAll({ attributes: ['id'], where: { tagSet: id } });
let tagIds = tagInfo.map(r => r.id); let tagIds = tagInfo.map(r => r.id);
let deletable = true; let deletable = true;
if (tagIds.length) { if (tagIds.length) {
let tagDatabaseInfo = await models.MetadataDatabase.findOne({ where: { tag: { $in: tagIds } } }); let tagDatabaseInfo = await models.TagDatabase.findOne({ where: { tag: { $in: tagIds } } });
let tagFileInfo = await models.MetadataDatabase.findOne({ where: { tag: { $in: tagIds } } }); let tagFileInfo = await models.TagFile.findOne({ where: { tag: { $in: tagIds } } });
let tagRestapiInfo = await models.MetadataDatabase.findOne({ where: { tag: { $in: tagIds } } }); let tagRestapiInfo = await models.TagRestapi.findOne({ where: { tag: { $in: tagIds } } });
if (tagDatabaseInfo || tagFileInfo || tagRestapiInfo) { if (tagDatabaseInfo || tagFileInfo || tagRestapiInfo) {
ctx.status = 400; ctx.status = 400;
ctx.body = { message: '已打标元数据,禁止删除' } ctx.body = { message: '该标签集下存在标签已打标元数据,禁止删除' }
deletable = false; deletable = false;
} }
} }
@ -103,6 +105,10 @@ async function deleteTagSets(ctx) {
ctx.status = 200; ctx.status = 200;
ctx.body = { message: '删除标签集成功' } ctx.body = { message: '删除标签集成功' }
} }
} else {
ctx.status = 400;
ctx.body = { message: '该标签集不存在' }
}
} catch (error) { } catch (error) {
await transaction.rollback(); await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
@ -110,10 +116,92 @@ async function deleteTagSets(ctx) {
ctx.body = { message: '删除标签集失败' } 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 = { module.exports = {
getTags, getTags,
postTagSets, postTagSets,
putTagSets, putTagSets,
deleteTagSets delTagSets,
postTags,
putTags,
delTags
} }

14
api/app/lib/routes/tags/index.js

@ -13,15 +13,15 @@ module.exports = function (app, router, opts) {
router.put('/tag-sets/:id', tags.putTagSets); router.put('/tag-sets/:id', tags.putTagSets);
app.fs.api.logAttr['DEL /tag-sets/:id'] = { content: '删除标签集', visible: true }; 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 }; app.fs.api.logAttr['POST /tags'] = { content: '新增标签', visible: true };
// router.post('/tags', tags.postTags); router.post('/tags', tags.postTags);
// app.fs.api.logAttr['PUT /tags/:id'] = { content: '修改标签', visible: true }; app.fs.api.logAttr['PUT /tags/:id'] = { content: '修改标签', visible: true };
// router.put('/tags/:id', tags.putTags); router.put('/tags/:id', tags.putTags);
// app.fs.api.logAttr['DEL /tags/:id'] = { content: '删除标签', visible: true }; app.fs.api.logAttr['DEL /tags/:id'] = { content: '删除标签', visible: true };
// router.delete('/tags/:id', tags.delTags); router.delete('/tags/:id', tags.delTags);
}; };
Loading…
Cancel
Save