feat: add types and list change

This commit is contained in:
2024-09-18 05:19:20 +08:00
parent 0a9e5c1d4f
commit 99f6f58f52
8 changed files with 127 additions and 271 deletions

View File

@@ -1,5 +1,5 @@
import { app } from '../../app.ts';
import { ContainerModel, ContainerData } from './models/index.ts';
import { ContainerModel, ContainerData, Container } from './models/index.ts';
const list = app.route({
path: 'container',
@@ -15,27 +15,49 @@ list.addTo(app);
const add = app.route({
path: 'container',
key: 'add',
key: 'update',
});
add.run = async (ctx) => {
// const data = ctx.query;
const data: ContainerData = {
className: 'name',
style: {
color: 'red',
const data = ctx.query.data;
const _data: Container = {
title: '',
description: '',
code: '',
source: '',
type: '',
sourceType: '',
data: {
className: '',
style: {},
showChild: true,
shadowRoot: false,
},
showChild: true,
shadowRoot: false,
};
const container = await ContainerModel.create({
title: 'title',
data: data as ContainerData,
description: 'description',
code: `console.log('hello world')`,
source: 'source',
type: 'typescript',
});
ctx.body = container;
const container = {
..._data,
...data,
};
let containerModel: any = null;
if (container.id) {
containerModel = await ContainerModel.findByPk(container.id);
if (containerModel) {
containerModel.update(container);
containerModel.save();
}
} else {
try{
containerModel = await ContainerModel.create({
...container,
});
} catch (e) {
console.log('error', e);
}
console.log('containerModel', container);
}
ctx.body = containerModel;
return ctx;
};
add.addTo(app);

View File

@@ -7,6 +7,16 @@ export interface ContainerData {
showChild?: boolean;
shadowRoot?: boolean;
}
export type Container = {
id?: string;
title?: string;
description?: string;
type?: string;
code?: string;
source?: string;
sourceType?: string;
data?: ContainerData;
};
export class ContainerModel extends Model {
declare id: string;
declare title: string;
@@ -14,6 +24,7 @@ export class ContainerModel extends Model {
declare type: string;
declare code: string;
declare source: string;
declare sourceType: string;
declare data: ContainerData;
}
ContainerModel.init(
@@ -37,13 +48,17 @@ ContainerModel.init(
defaultValue: '',
},
code: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
defaultValue: '',
},
source: {
type: DataTypes.STRING,
defaultValue: '',
},
sourceType: {
type: DataTypes.STRING,
defaultValue: '',
},
data: {
type: DataTypes.JSON,
defaultValue: {},