政务数据资源中心(Government data Resource center) 03专项3期主要建设内容
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
8.0 KiB

'use strict';
//获取资源目录
async function getResourceCatalog(ctx) {
try {
const models = ctx.fs.dc.models;
const rslt = await models.ResourceCatalog.findAll({
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 postResourceCatalog(ctx) {
try {
const { name, code } = ctx.request.body;
const models = ctx.fs.dc.models;
const postOne = await models.ResourceCatalog.findOne({
where: { $or: [{ name: name }, { code: code }] }
});
if (postOne) {
ctx.status = 400;
ctx.body = { message: '该资源目录名称或代码已存在' }
} else {
await models.ResourceCatalog.create(ctx.request.body);
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 putResourceCatalog(ctx) {
try {
const { id } = ctx.params;
const { name, code, description } = ctx.request.body;
const models = ctx.fs.dc.models;
let resourceCatalogInfo = await models.ResourceCatalog.findOne({ where: { id } });
if (resourceCatalogInfo) {
const putOne = await models.ResourceCatalog.findOne({ where: { id: { $not: id }, $or: [{ name: name }, { code: code }] } });
if (putOne) {
ctx.status = 400;
ctx.body = { message: '该资源目录名称或代码已存在' }
} else {
await models.ResourceCatalog.update(ctx.request.body, { 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 delResourceCatalog(ctx) {
const transaction = await ctx.fs.dc.orm.transaction();
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
let resourceCatalogInfo = await models.ResourceCatalog.findOne({ where: { id } });
if (resourceCatalogInfo) {
let deletable = true;
let childResourceCatalogInfo = await models.ResourceCatalog.findOne({ where: { parent: id } });
let databaseInfo = await models.MetadataDatabase.findOne({ where: { catalog: id } });
let fileInfo = await models.MetadataFile.findOne({ where: { catalog: id } });
let restapiInfo = await models.MetadataRestapi.findOne({ where: { catalog: id } });
if (childResourceCatalogInfo || databaseInfo || fileInfo || restapiInfo) {
ctx.status = 400;
ctx.body = { message: '存在关联子类目录或元数据,请删除相关数据,再删除该资源目录' }
deletable = false;
}
if (deletable) {
await models.ResourceCatalog.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();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '删除资源目录失败' }
}
}
//获取库表元数据列表
async function getMetadataDatabases(ctx) {
try {
const models = ctx.fs.dc.models;
const { catalog, limit, offset, keywords, orderBy = 'createAt', orderDirection = 'desc' } = ctx.query;
const where = { catalog: catalog };
if (keywords) {
where[$or] = [{ name: { $like: keywords } }, { code: { $like: keywords } }, { type: { $like: keywords } }]
}
const rslt = await models.MetadataDatabase.findAll({
include: [
{
model: models.User,
attributes: ['id', 'name'],
},
{
model: models.TagDatabase,
include: [{
model: models.Tag,
}]
}],
where: where,
order: [[orderBy, orderDirection]],
offset: Number(offset) * Number(limit),
limit: Number(limit),
distinct: true
});
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 getMetadataFiles(ctx) {
try {
const models = ctx.fs.dc.models;
const { catalog, limit, offset, keywords, orderBy = 'createAt', orderDirection = 'desc' } = ctx.query;
const where = { catalog: catalog };
if (keywords) {
where[$or] = [{ name: { $like: keywords } }, { type: { $like: keywords } }]
}
const rslt = await models.MetadataFile.findAll({
include: [
{
model: models.User,
attributes: ['id', 'name'],
},
{
model: models.TagFile,
include: [{
model: models.Tag,
}]
}],
where: where,
order: [[orderBy, orderDirection]],
offset: Number(offset) * Number(limit),
limit: Number(limit),
distinct: true
});
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 getMetadataRestapis(ctx) {
try {
const models = ctx.fs.dc.models;
const { catalog, limit, offset, keywords, orderBy = 'createAt', orderDirection = 'desc' } = ctx.query;
const where = { catalog: catalog };
if (keywords) {
where.name = { $like: keywords };
}
const rslt = await models.MetadataRestapi.findAll({
include: [
{
model: models.User,
attributes: ['id', 'name'],
},
{
model: models.TagRestapi,
include: [{
model: models.Tag,
}]
}],
where: where,
order: [[orderBy, orderDirection]],
offset: Number(offset) * Number(limit),
limit: Number(limit),
distinct: true
});
ctx.status = 200;
ctx.body = rslt;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取接口元数据列表失败"
}
}
}
module.exports = {
getResourceCatalog,
postResourceCatalog,
putResourceCatalog,
delResourceCatalog,
getMetadataDatabases,
getMetadataFiles,
getMetadataRestapis
}