feat: add container page api
This commit is contained in:
		| @@ -1,3 +1,4 @@ | ||||
| import { CustomError } from '@abearxiong/router'; | ||||
| import { app } from '../../app.ts'; | ||||
| import { ContainerModel, ContainerData, Container } from './models/index.ts'; | ||||
|  | ||||
| @@ -13,6 +14,21 @@ list.run = async (ctx) => { | ||||
|  | ||||
| list.addTo(app); | ||||
|  | ||||
| app | ||||
|   .route({ | ||||
|     path: 'container', | ||||
|     key: 'get', | ||||
|   }) | ||||
|   .define(async (ctx) => { | ||||
|     const id = ctx.query.id; | ||||
|     if (!id) { | ||||
|       throw new CustomError('id is required'); | ||||
|     } | ||||
|     ctx.body = await ContainerModel.findByPk(id); | ||||
|     return ctx; | ||||
|   }) | ||||
|   .addTo(app); | ||||
|  | ||||
| const add = app.route({ | ||||
|   path: 'container', | ||||
|   key: 'update', | ||||
| @@ -46,8 +62,7 @@ add.run = async (ctx) => { | ||||
|       containerModel.save(); | ||||
|     } | ||||
|   } else { | ||||
|     try{ | ||||
|  | ||||
|     try { | ||||
|       containerModel = await ContainerModel.create({ | ||||
|         ...container, | ||||
|       }); | ||||
|   | ||||
| @@ -1 +1,3 @@ | ||||
| import './container/index.ts'; | ||||
| import './container/index.ts'; | ||||
|  | ||||
| import './page/index.ts'; | ||||
| @@ -1,5 +1,29 @@ | ||||
| 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({ | ||||
| @@ -15,13 +39,20 @@ app | ||||
| app | ||||
|   .route({ | ||||
|     path: 'page', | ||||
|     key: 'add', | ||||
|     key: 'update', | ||||
|   }) | ||||
|   .define(async (ctx) => { | ||||
|     const data = ctx.query; | ||||
|     const page = await PageModel.create(data); | ||||
|     ctx.body = page; | ||||
|     return 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); | ||||
|  | ||||
| @@ -40,3 +71,148 @@ app | ||||
|     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); | ||||
|   | ||||
| @@ -27,7 +27,7 @@ PageModel.init( | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     description: { | ||||
|       type: DataTypes.STRING, | ||||
|       type: DataTypes.TEXT, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     type: { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user