Skip to content

Commit

Permalink
feat: add widgets marks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrxyy committed Jan 24, 2024
1 parent 0d86b65 commit 2a9db23
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { QueriesModel } from './models/Querys';
import { OpenAIModule } from './models/AI/openAi';
import { config } from 'dotenv';
import { EventsModule } from './models/events/events.module';
import { WidgetsModule } from './models/widgets';
config();
export const dbHost = process.env['DB_HOST'];
export const dbPort = Number(process.env['DB_PORT']);
Expand All @@ -32,6 +33,7 @@ const env = [
Kenx,
OpenAIModule,
EventsModule,
WidgetsModule,
],
})
export class AppModule {
Expand Down
6 changes: 3 additions & 3 deletions src/models/Schema/index.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { Schema } from './schema.model';
import { InjectModel } from '@nestjs/sequelize';
import { executeRes } from 'src/utils/response/sequeilze';
import { executeRes } from '../../utils/response/sequeilze';
import { SchemaLog } from './SchemaLog.model';
import { get, omit } from 'lodash';
import { GET_SCHEMA_INFO } from 'src/utils/prompts/schema';
import exportDsl from 'src/utils/knex/export-dsl';
import { GET_SCHEMA_INFO } from '../../utils/prompts/schema';
import exportDsl from '../../utils/knex/export-dsl';

@Injectable()
export class SchemaService {
Expand Down
14 changes: 14 additions & 0 deletions src/models/widgets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { WidgetsController } from './widgets.controller';
import { SequelizeModule } from '@nestjs/sequelize';
import { Widgets } from './widgets.model';
import { WidgetsService } from './widgets.service';

@Module({
imports: [SequelizeModule.forFeature([Widgets])],
providers: [WidgetsService],
controllers: [WidgetsController],
})
export class WidgetsModule {
constructor() {}
}
42 changes: 42 additions & 0 deletions src/models/widgets/widgets.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Param,
Post,
Put,
} from '@nestjs/common';
import { WidgetsService } from './widgets.service';
import { Widgets } from './widgets.model';

@Controller('/widgets')
export class WidgetsController {
constructor(private widgetsService: WidgetsService) {}
// 获取所有组件
@Get('/')
async getAll() {
return this.widgetsService.getAll();
}

// 创建新组件
@Post('/')
async create(@Body() createDto) {
return this.widgetsService.create(createDto);
}

// 更新组件
@Put(':id')
async update(@Param('id') id: string, @Body() updateDto) {
return this.widgetsService.update(id, updateDto);
}

// 删除组件
@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)
async delete(@Param('id') id: string) {
return this.widgetsService.delete(id);
}
}
64 changes: 64 additions & 0 deletions src/models/widgets/widgets.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
Table,
Column,
Model,
CreatedAt,
UpdatedAt,
DeletedAt,
PrimaryKey,
DataType,
Comment,
Default,
AllowNull,
Unique,
} from 'sequelize-typescript';

@Table
export class Widgets extends Model {
@PrimaryKey
@Comment('id')
@Default(DataType.UUIDV4)
@Column(DataType.UUID)
id: string;

@Comment('组件内容')
@AllowNull(false)
@Column({
type: DataType.JSON,
})
content: typeof DataType.JSON;

@Comment('组件配置')
@AllowNull(false)
@Column({
type: DataType.JSON,
})
props: typeof DataType.JSON;

@Comment('组件名称')
@Column({
type: DataType.JSON,
})
name: typeof DataType.JSON;

@Unique('widget-key')
@Comment('组件识别名称')
@Column({
type: DataType.STRING(100),
})
key: string;

@Comment('作者ID')
@AllowNull(true)
@Column({
type: DataType.STRING(1000),
})
author: string;

@CreatedAt
createdAt: Date;
@UpdatedAt
updatedAt: Date;
@DeletedAt
DeletedAt: Date;
}
55 changes: 55 additions & 0 deletions src/models/widgets/widgets.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Injectable } from '@nestjs/common';
import { Widgets } from './widgets.model';
import { InjectModel } from '@nestjs/sequelize';
import { pick } from 'lodash';

@Injectable()
export class WidgetsService {
constructor(@InjectModel(Widgets) private widgets: typeof Widgets) {}
// 获取所有组件
async getAll() {
const widget = await this.widgets.findAll();
return widget;
}

// 创建新组件
async create(
createDto: Omit<Widgets, 'id' | 'createdAt' | 'updatedAt' | 'DeletedAt'>,
) {
try {
const widget = await this.widgets.create(createDto);
return widget;
} catch (e: any) {
return pick(e, 'errors', []);
}
}

// 更新组件
async update(
id: string,
updateDto: Partial<
Omit<Widgets, 'id' | 'createdAt' | 'updatedAt' | 'DeletedAt'>
>,
) {
await this.widgets.update(updateDto, {
where: { id },
});
return this.widgets.findOne({
where: { id },
});
}

// 删除组件
async delete(id: string) {
const numberOfDeletedRows = await this.widgets.destroy({
where: { id },
});

if (numberOfDeletedRows === 0) {
throw new Error('Widget not found');
}
return this.widgets.findOne({
where: { id },
});
}
}

0 comments on commit 2a9db23

Please sign in to comment.