Vue3 + Nest 实现博客管理系统 后端篇(五):标签页面接口增删改查

"本章节主要编写博客标签接口的增删改查,也为接下来编写博客的添加做铺垫,标签分为父标签和子标签"

创建标签模块

bash 复制代码
nest g res tag

同样我们选择REST API以及生成crud

添加标签实体

ts 复制代码
import { Base } from 'libs/Entities/base.entity';
import { Column, Entity, ManyToMany, ManyToOne } from 'typeorm';

@Entity('tag')
export class Tag extends Base {
  @Column({
    type: 'varchar',
    comment: '标签名称',
  })
  name: string;

  @ManyToOne((type) => Tag, (tag) => tag.superiors)
  superiorsid: Tag;

  @ManyToMany((type) => Tag, (tag) => tag.superiorsid)
  superiors: Tag[];

  @Column()
  grade: string;
}

重启之后可以看到数据库已经生成了tsg表

添加创建接口

ts 复制代码
  constructor(@InjectRepository(Tag) private tagRepository: Repository<Tag>) {}

  async create(createTagDto: CreateTagDto) {
    return await this.tagRepository.save(createTagDto);
  }

然后打开apifox进行接口测试。

OK,没有问题

标签查询接口

首先修改tag.controller.ts,添加入参

修改tag.service.ts,设置入参,以及分页

ts 复制代码
interface SearchType {
  // 搜索条件
  search: any;
  pageNo: number;
  pageSize: number;
}




Apiresult = new ApiresultService();
async findAll(body: SearchType) {
  const { pageNo, pageSize, search } = body;

  const condition: Record<string, any> = {}; // 用于设置模糊查询

  const skipCount = (pageNo - 1) * pageSize; // 分页查询,计算要跳过的条数

   if (search && search.name) {
     const { name } = search;
     condition.name = Like(`%${name}%`); // 模糊查询
   }
  condition.grade = 1; // 因为我们是只有二级,所以要过滤一下

  const [list, total] = await this.tagRepository
    .createQueryBuilder('entity') // 设置表别名
    .leftJoinAndSelect('entity.superiors', 'children')
    .where(condition) // 设置查询条件
    .skip(skipCount)
    .take(pageSize)
    .getManyAndCount();

  return {
    ...this.Apiresult.MESSAGE(200, '查询成功', list),
    total,
  };
}

我这里已经添加了几条数据,接下来使用apifox进行查询测试

分页查询没有问题,然后测试name条件查询

条件模糊查询没问题

标签修改和删除

ts 复制代码
  async update(body) {
    if (!body.id) return this.Apiresult.MESSAGE(500, '缺少id');
    const data = await this.tagRepository.update(body.id, body);
    console.log(data);
    if (data.affected > 0) {
      return this.Apiresult.MESSAGE(200, '修改成功');
    }
    return this.Apiresult.MESSAGE(500, '修改失败, 请检查入参');
  }

同理添加删除

ts 复制代码
  async remove(body) {
    if (!body.id) return this.Apiresult.MESSAGE(500, '缺少id');
    const data = await this.tagRepository.delete(body.id);

    if (data.affected > 0) {
      return this.Apiresult.MESSAGE(200, '删除成功');
    }
    return this.Apiresult.MESSAGE(500, '删除失败, 请检查入参');
  }

测试一下

相关推荐
泯泷3 分钟前
手搓JSVMM第 8 篇:函数调用:参数、返回值与调用帧
前端·javascript·前端框架
Sherotree27 分钟前
Google Antigravity——Agent 被提到主界面
前端·ide·后端·edge浏览器
2601_962056231 小时前
Spring Boot 入门 与 无法解析符号 springframework 的解决
java·spring boot·后端
里欧跑得慢1 小时前
Flutter 像素级还原:设计稿到代码的视觉无损转换技巧
前端·css·flutter·web
里欧跑得慢1 小时前
Flutter 三方库 function_tree — 鸿蒙应用开发中的动态数学公式解析与计算神器,实现鸿蒙深度适配下的复杂函数逻辑运行时求值实战(适配鸿蒙 HarmonyOS Next ohos)
android·前端·安全·flutter·华为·harmonyos
你驴我1 小时前
WhatsApp 多账号场景下的会话归档与历史消息检索优化实践
后端·python
苏灿烤鱼2 小时前
没有机密文件,也没有付费数据,在浏览器里造一颗"间谍卫星"
前端·javascript·github
学长毕业设计2 小时前
基于SpringBoot的瑜伽馆网站的设计与实现(源码+文档+讲解视频)
java·spring boot·后端
泯泷2 小时前
手搓JSVM第 5 篇:写第一个编译器:从 AST 生成 IR
前端·javascript·算法
泯泷2 小时前
手搓JSVM第 7 篇:控制流:if、while 与 jump
前端·javascript·算法