添加删除文件
This commit is contained in:
125
src/routes/app-manager/domain/manager.ts
Normal file
125
src/routes/app-manager/domain/manager.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { AppDomainModel } from '../module/app-domain.ts';
|
||||
import { AppModel } from '../module/app.ts';
|
||||
import { CustomError } from '@kevisual/router';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app.domain.manager',
|
||||
key: 'list',
|
||||
middleware: ['auth-admin'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { page = 1, pageSize = 999 } = ctx.query.data || {};
|
||||
const { count, rows } = await AppDomainModel.findAndCountAll({
|
||||
offset: (page - 1) * pageSize,
|
||||
limit: pageSize,
|
||||
});
|
||||
ctx.body = { count, list: rows, pagination: { page, pageSize } };
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app.domain.manager',
|
||||
key: 'update',
|
||||
middleware: ['auth-admin'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { domain, data, id, ...rest } = ctx.query.data || {};
|
||||
if (!domain) {
|
||||
ctx.throw(400, 'domain is required');
|
||||
}
|
||||
let domainInfo: AppDomainModel;
|
||||
if (id) {
|
||||
domainInfo = await AppDomainModel.findByPk(id);
|
||||
} else {
|
||||
domainInfo = await AppDomainModel.create({ domain });
|
||||
}
|
||||
const checkAppId = async () => {
|
||||
const isUUID = (id: string) => {
|
||||
return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(id);
|
||||
};
|
||||
if (rest.appId) {
|
||||
if (!isUUID(rest.appId)) {
|
||||
ctx.throw(400, 'appId is not valid');
|
||||
}
|
||||
const appInfo = await AppModel.findByPk(rest.appId);
|
||||
if (!appInfo) {
|
||||
ctx.throw(400, 'appId is not exist');
|
||||
}
|
||||
}
|
||||
};
|
||||
try {
|
||||
if (!domainInfo) {
|
||||
domainInfo = await AppDomainModel.create({ domain, data: {}, ...rest });
|
||||
await checkAppId();
|
||||
} else {
|
||||
if (rest.status && domainInfo.status !== rest.status) {
|
||||
await domainInfo.clearCache();
|
||||
}
|
||||
await checkAppId();
|
||||
await domainInfo.update({
|
||||
domain,
|
||||
data: {
|
||||
...domainInfo.data,
|
||||
...data,
|
||||
},
|
||||
...rest,
|
||||
});
|
||||
}
|
||||
ctx.body = domainInfo;
|
||||
} catch (error) {
|
||||
if (error.code) {
|
||||
ctx.throw(error.code, error.message);
|
||||
}
|
||||
console.error(error);
|
||||
ctx.throw(500, 'update domain failed, please check the data');
|
||||
}
|
||||
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app.domain.manager',
|
||||
key: 'delete',
|
||||
middleware: ['auth-admin'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { id, domain } = ctx.query.data || {};
|
||||
if (!id && !domain) {
|
||||
ctx.throw(400, 'id or domain is required');
|
||||
}
|
||||
if (id) {
|
||||
await AppDomainModel.destroy({ where: { id }, force: true });
|
||||
} else {
|
||||
await AppDomainModel.destroy({ where: { domain }, force: true });
|
||||
}
|
||||
|
||||
ctx.body = { message: 'delete domain success' };
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app.domain.manager',
|
||||
key: 'get',
|
||||
middleware: ['auth-admin'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { id, domain } = ctx.query.data || {};
|
||||
if (!id && !domain) {
|
||||
ctx.throw(400, 'id or domain is required');
|
||||
}
|
||||
const domainInfo = await AppDomainModel.findOne({ where: { id } });
|
||||
if (!domainInfo) {
|
||||
ctx.throw(404, 'domain not found');
|
||||
}
|
||||
ctx.body = domainInfo;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
Reference in New Issue
Block a user