NestJS-通告数据模块

NestJS-通告数据模块

1、通告数据模块

接下来我们新建一个通告数据模块,实现对于通告数据部分的管理功能接口

👉生成通告数据文件

javascript 复制代码
nest g controller modules/notice --no-spec
nest g module modules/notice --no-spec
nest g service modules/notice --no-spec

创建完成以后的目录如下图所示

javascript 复制代码
src\modules\notice
  src\modules\notice\notice.controller.ts
  src\modules\notice\notice.module.ts
  src\modules\notice\notice.service.ts

//手动添加实体
  src\modules\notice\notice.entity.ts

👉notice.entity.ts实体搭建

javascript 复制代码
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity('sys_notice')
export class Notice {
  @PrimaryGeneratedColumn({ name: 'notice_id', type: 'int' })
  noticeId: number;

  @Column({ name: 'notice_title', length: 50, nullable: false })
  noticeTitle: string;

  @Column({
    name: 'notice_type',
    type: 'enum',
    enum: ['success', 'data', 'chatmsg', 'info', 'danger', 'warn'],
    nullable: true,
    default: null,
    comment: `
      | success | 系统连接成功提示
      | data    | 欢迎连接聊天室
      | chatmsg | 收到客户端消息 -- 普通聊天信息
      | info    | 通知类型消息 -- 普通通告信息
      | warn    | 警告类型消息 -- 用户求助
      | danger  | 危险类型 -- 最高级别警告信息
    `,
  })
  noticeType: 'success' | 'data' | 'chatmsg' | 'info' | 'danger' | 'warn';

  @Column({ name: 'notice_content', type: 'text', nullable: true })
  noticeContent: string;

  @Column({
    name: 'status',
    type: 'char',
    length: 1,
    default: '0',
    nullable: true,
    comment: '公告状态(0正常 1关闭)',
  })
  status: string;

  @Column({ name: 'create_by', length: 64, default: '', nullable: true })
  createBy: string;

  @Column({ name: 'create_time', type: 'datetime', nullable: true })
  createTime: Date;

  @Column({ name: 'update_by', length: 64, default: '', nullable: true })
  updateBy: string;

  @Column({ name: 'update_time', type: 'datetime', nullable: true })
  updateTime: Date;

  @Column({ name: 'remark', length: 255, nullable: true })
  remark: string;

  @Column({
    type: 'boolean',
    nullable: true,
    default: false,
    comment: '是否软删除消息(默认为false 未删除 true删除)',
  })
  isDeleted: boolean;
}

👉实体映射

在我们的src\common\entity\index.ts文件之中导入实体并进行映射

javascript 复制代码
import { notice } from '@/modules/notice/notice.entity'; // 通告数据类型实体

export const dataOrmConfig: dataOrmModuleOptions = {
  data: 'mysql', // 可以选择其他数据库,如 MySQL
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'XXXXX',
  database: 'nexusnest',
  entities: [notice], 
  synchronize: true, // 自动同步实体到数据库(开发时可以启用,生产环境应谨慎使用)
  logging: false,  // 启用日志记录
  timezone: '+08:00', // 设置时区
};

这个时候刷新我们数据库可以发现,数据库的表已经对应上我们对应的实体了

👉 notice.module.ts

在通告数据模块之中导入其他的模块部分

javascript 复制代码
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { noticeService } from './notice.service';
import { noticeController } from './notice.controller';
import { notice } from './notice.entity';


@Module({
     // 导入实体
  imports: [
    TypeOrmModule.forFeature([notice]),
  ],
  providers: [noticeService],
  controllers: [noticeController],
  exports: [noticeService],
})
export class noticeModule {}

2、搜索

接下来我们就根据对应的实体帮助我们生成一下我们的通告数据功能部分的api接口

实现通告数据功能其实跟我们其他管理的部分差不多

通告数据功能主要是我们在pc端用户进行登陆以后可以对于通告数据通告数据权限进行控制,然后可以通过通告数据对用户进行权限的统一管理

👉notice.controller.ts 控制器

javascript 复制代码
// 列表 
  @Get()
  @ApiOperation({ summary: '列表带分页' }) // 操作描述
  @ApiQuery({ name: 'pageNum', required: false, description: 'Page number', example: 1 })  // pageNum参数
  @ApiQuery({ name: 'pageSize', required: false, description: 'Number of items per page', example: 10 })  // pageSize参数
  // 获取列表所有
  async getAll(
    @Query('isDeleted') isDeleted: boolean,
    @Query('pageNum') pageNum = 1,  // 默认第1页
    @Query('pageSize') pageSize= 10, // 默认每页10条
    @Query('noticeType') noticeType: string,
  ) {
    // const queryParams: QueryParams = { name, phone, age, sex };
    const queryParams = { isDeleted: false,noticeType};
    console.log(queryParams, 'queryParams--查询');
    const ResponseData = await this.noticeService.getAll(pageNum, pageSize, queryParams);
    return ResponseData;
  }

👉notice.service.ts 服务层

javascript 复制代码
  // 通用查询
  async getAll(pageNum: number, pageSize: number, queryParams: any): Promise<ResponseDto<notice[]>> {
    console.log(queryParams, 'queryParams');
    // 新版本完善-通用分页查询
    const where = await getListApi(this.noticeRepository, pageNum, pageSize, queryParams);
    return where;
  }

查询一下,接口ok

3、增加

👉notice.controller.ts 控制器

javascript 复制代码
// 新增
  @ApiOperation({ summary: '新增' }) // 操作描述
  @Post()
  async create(@Body() addFormData) {
    console.log(addFormData, 'addFormData--添加参数');
    return this.controlService.addOne(addFormData);
  }

👉notice.service.ts 服务层

javascript 复制代码
 // 通用添加
async addOne(addFormData) {
  const resdata = await addApi(this.NoyiceRepository, addFormData);
  return resdata;
}

尝试一下我们的新增接口

增加功能ok,并且能正确返回我们想要的东西

4、详情

详情,通过id然后查到对应的内容

👉notice.controller.ts 控制器

javascript 复制代码
 // 查询单个
  @Get('/:id')
  @ApiOperation({ summary: '查询单个' }) // 操作描述
  async getOne(@Param('id') id: string) {
    return this.controlService.getOne(id);
  }

👉notice.service.ts 服务层

javascript 复制代码
// 通用详情
async getOne(id) {
  const resdata = await getOneApi(this.NoyiceRepository, { noticeId: id });
  return resdata;
}

简单测试一下,详情接口没问题,我们采用的也是通过ID的方式进行获取的

5、修改

接下来我们完善一下我们的修改接口:

👉notice.controller.ts 控制器

javascript 复制代码
@Put()
@ApiOperation({ summary: '更新' }) // 操作描述
async updateOne(@Body() updateFormData) {
  console.log(updateFormData, 'updateFormData');
  // console.log(updateFormData, 'updateFormData');
  return this.controlService.updateOne(updateFormData.noyiceId, updateFormData);
}

👉notice.service.ts 服务层

javascript 复制代码
// 通用更新
async updateOne(id, updateFormData) {
  const resdata = await updateOneApi(this.NoyiceRepository, updateFormData, { noticeId: id });
  return resdata;
}

简单测试一下,修改接口没问题

5、删除

👉notice.controller.ts 控制器

javascript 复制代码
// 删除
@Delete('/:id')
@ApiOperation({ summary: '删除' }) // 操作描述
async deleteOne(@Param('id') id: number) {
  return this.controlService.deleteOne(id);
}

👉notice.service.ts 服务层

javascript 复制代码
// 通用删除
async deleteOne(id) {
  // const resdata = await deleteOneApi(this.NoyiceRepository,{noticeId:id});
  const resdata = await delOneApiPermanent(this.NoyiceRepository, { noticeId: id });
  return resdata;
}

测试可以发现,我们的删除功能已经好了!

相关推荐
胡gh2 小时前
页面卡成PPT?重排重绘惹的祸!依旧性能优化
前端·javascript·面试
言兴2 小时前
# 深度解析 ECharts:从零到一构建企业级数据可视化看板
前端·javascript·面试
山有木兮木有枝_2 小时前
TailWind CSS
前端·css·postcss
烛阴3 小时前
TypeScript 的“读心术”:让类型在代码中“流动”起来
前端·javascript·typescript
杨荧3 小时前
基于Python的农作物病虫害防治网站 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python
Moment4 小时前
毕业一年了,分享一下我的四个开源项目!😊😊😊
前端·后端·开源
程序视点5 小时前
Escrcpy 3.0投屏控制软件使用教程:无线/有线连接+虚拟显示功能详解
前端·后端
silent_missile5 小时前
element-plus穿梭框transfer的调整
前端·javascript·vue.js
专注VB编程开发20年5 小时前
OpenXml、NPOI、EPPlus、Spire.Office组件对EXCEL ole对象附件的支持
前端·.net·excel·spire.office·npoi·openxml·spire.excel
古蓬莱掌管玉米的神5 小时前
coze娱乐ai换脸
前端