-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
}); | ||
} | ||
} |