219 lines
4.7 KiB
TypeScript
219 lines
4.7 KiB
TypeScript
import { CustomError } from '@abearxiong/router';
|
|
import { app } from '../../app.ts';
|
|
import { PageModel } from './models/index.ts';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { ContainerModel } from '../container/models/index.ts';
|
|
import { Op } from 'sequelize';
|
|
|
|
app
|
|
.route({
|
|
path: 'page',
|
|
key: 'get',
|
|
})
|
|
.define(async (ctx) => {
|
|
const id = ctx.query.id;
|
|
if (!id) {
|
|
throw new CustomError('id is required');
|
|
}
|
|
try {
|
|
const page = await PageModel.findByPk(id);
|
|
ctx.body = page;
|
|
} catch (e) {
|
|
console.log('error', e);
|
|
throw new CustomError(e.message || 'get error');
|
|
}
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'page',
|
|
key: 'list',
|
|
})
|
|
.define(async (ctx) => {
|
|
ctx.body = await PageModel.findAll();
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'page',
|
|
key: 'update',
|
|
})
|
|
.define(async (ctx) => {
|
|
const { data, id, ...rest } = ctx.query.data;
|
|
if (id) {
|
|
const page = await PageModel.findByPk(id);
|
|
if (page) {
|
|
const newPage = await page.update({ data: data, ...rest });
|
|
ctx.body = newPage;
|
|
}
|
|
} else if (data) {
|
|
const page = await PageModel.create({ data, ...rest });
|
|
ctx.body = page;
|
|
}
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'page',
|
|
key: 'delete',
|
|
})
|
|
.define(async (ctx) => {
|
|
const id = ctx.query.id;
|
|
const page = await PageModel.findByPk(id);
|
|
if (page) {
|
|
await page.destroy();
|
|
}
|
|
ctx.body = page;
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'page',
|
|
key: 'addDemo',
|
|
})
|
|
.define(async (ctx) => {
|
|
const id = uuidv4();
|
|
const data = {
|
|
// id: 'container-1',
|
|
id,
|
|
title: 'demo',
|
|
description: 'demo',
|
|
type: 'conainer',
|
|
data: {
|
|
edges: [
|
|
{
|
|
id: 'e1',
|
|
// source: 'container-1',
|
|
source: id,
|
|
target: 'container-2',
|
|
},
|
|
{
|
|
id: 'e2',
|
|
// source: 'container-1',
|
|
source: id,
|
|
target: 'container-3',
|
|
},
|
|
{
|
|
id: 'e3',
|
|
source: 'container-2',
|
|
target: 'container-4',
|
|
},
|
|
],
|
|
nodes: [
|
|
{
|
|
// id: 'container-1',
|
|
id,
|
|
type: 'container',
|
|
data: {
|
|
label: '开始',
|
|
title: 'demo-hello-world',
|
|
cid: 'a6652ce0-82fb-432a-a6b0-2033a655b02c',
|
|
root: true,
|
|
},
|
|
position: {
|
|
x: 50,
|
|
y: 125,
|
|
},
|
|
},
|
|
{
|
|
id: 'container-2',
|
|
type: 'container',
|
|
data: {
|
|
label: '容器',
|
|
title: 'demo-child-01',
|
|
cid: '67e5b2ff-98dc-43ab-8ad9-9b062096f8eb',
|
|
},
|
|
position: {
|
|
x: 350,
|
|
y: 125,
|
|
},
|
|
},
|
|
{
|
|
id: 'container-3',
|
|
type: 'container',
|
|
data: {
|
|
label: '容器',
|
|
title: 'demo-child-03',
|
|
cid: '208c3e36-dc7d-46af-b2f0-81d5f43c974d',
|
|
},
|
|
position: {
|
|
x: 350,
|
|
y: 325,
|
|
},
|
|
},
|
|
{
|
|
id: 'container-4',
|
|
type: 'container',
|
|
data: {
|
|
label: '容器',
|
|
title: 'demo-child-04',
|
|
cid: '170c0b55-8c13-4d6e-bf35-3f935d979a0d',
|
|
},
|
|
position: {
|
|
x: 650,
|
|
y: 125,
|
|
},
|
|
},
|
|
],
|
|
viewport: {},
|
|
},
|
|
};
|
|
try {
|
|
const page = await PageModel.create(data);
|
|
ctx.body = page;
|
|
} catch (e) {
|
|
console.log('error', e);
|
|
throw new CustomError('addDemo error');
|
|
}
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'page',
|
|
key: 'getDeck',
|
|
})
|
|
.define(async (ctx) => {
|
|
const id = ctx.query.id;
|
|
if (!id) {
|
|
throw new CustomError('id is required');
|
|
}
|
|
|
|
try {
|
|
const page = await PageModel.findByPk(id);
|
|
if (!page) {
|
|
throw new CustomError(404, 'panel not found');
|
|
}
|
|
const { data } = page;
|
|
const { nodes = [], edges } = data;
|
|
const containerList = nodes
|
|
.map((item) => {
|
|
const { data } = item;
|
|
return data?.cid;
|
|
})
|
|
.filter((item) => item);
|
|
const quchong = Array.from(new Set(containerList));
|
|
const containers = await ContainerModel.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: quchong,
|
|
},
|
|
},
|
|
});
|
|
ctx.body = {
|
|
page,
|
|
containerList: containers,
|
|
};
|
|
} catch (e) {
|
|
console.log('error', e);
|
|
throw new CustomError(e.message || 'get error');
|
|
}
|
|
})
|
|
.addTo(app);
|