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;
}

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

相关推荐
寅时码7 分钟前
无需安装,纯浏览器实现跨平台文件、文本传输,支持断点续传、二维码、房间码加入、粘贴传输、拖拽传输、多文件传输
前端·后端·架构
哔哩哔哩技术29 分钟前
哔哩哔哩Android视频编辑页的架构升级
前端
小小小小宇37 分钟前
重提Vue 3 性能提升
前端
eason_fan38 分钟前
React 源码执行流程
前端·源码阅读
will_we1 小时前
服务器主动推送之SSE (Server-Sent Events)探讨
前端·后端
yume_sibai1 小时前
Less Less基础
前端·css·less
小小小小宇1 小时前
重提Vue3 的 Diff 算法
前端
清岚_lxn1 小时前
前端js通过a标签直接预览pdf文件,弹出下载页面问题
前端·javascript·pdf
不爱说话郭德纲1 小时前
别再花冤枉钱!手把手教你免费生成iOS证书(.p12) + 打包IPA(超详细)
前端·ios·app
代码的余温1 小时前
Vue多请求并行处理实战指南
前端·javascript·vue.js