From 889f00bde8da02cacaadc982c33e18199bb493bd Mon Sep 17 00:00:00 2001 From: tikkhun Date: Thu, 27 Nov 2025 16:15:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AD=97=E5=85=B8):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AD=97=E5=85=B8=E5=92=8C=E5=AD=97=E5=85=B8=E9=A1=B9=E7=9A=84?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现字典和字典项的完整CRUD功能,包括: - 字典的创建、查询、更新和删除 - 字典项的创建、查询、更新和删除 - 支持按条件查询字典和字典项 - 使用DTO进行参数校验 --- src/controller/dict.controller.ts | 62 +++++++++++++++++++++++ src/dto/dict.dto.ts | 40 +++++++++++++++ src/service/dict.service.ts | 84 +++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 src/controller/dict.controller.ts create mode 100644 src/dto/dict.dto.ts create mode 100644 src/service/dict.service.ts diff --git a/src/controller/dict.controller.ts b/src/controller/dict.controller.ts new file mode 100644 index 0000000..a8e082a --- /dev/null +++ b/src/controller/dict.controller.ts @@ -0,0 +1,62 @@ +import { Inject, Controller, Get, Post, Put, Del, Query, Body, Param } from '@midwayjs/core'; +import { DictService } from '../service/dict.service'; +import { CreateDictDTO, UpdateDictDTO, CreateDictItemDTO, UpdateDictItemDTO } from '../dto/dict.dto'; +import { Validate } from '@midwayjs/validate'; + +@Controller('/api') +export class DictController { + @Inject() + dictService: DictService; + + // 获取字典列表 + @Get('/dicts') + async getDicts(@Query('title') title?: string) { + return this.dictService.getDicts(title); + } + + // 创建新字典 + @Post('/dicts') + @Validate() + async createDict(@Body() createDictDTO: CreateDictDTO) { + return this.dictService.createDict(createDictDTO); + } + + // 更新字典 + @Put('/dicts/:id') + @Validate() + async updateDict(@Param('id') id: number, @Body() updateDictDTO: UpdateDictDTO) { + return this.dictService.updateDict(id, updateDictDTO); + } + + // 删除字典 + @Del('/dicts/:id') + async deleteDict(@Param('id') id: number) { + return this.dictService.deleteDict(id); + } + + // 获取字典项列表 + @Get('/dict-items') + async getDictItems(@Query('dictId') dictId?: number) { + return this.dictService.getDictItems(dictId); + } + + // 创建新字典项 + @Post('/dict-items') + @Validate() + async createDictItem(@Body() createDictItemDTO: CreateDictItemDTO) { + return this.dictService.createDictItem(createDictItemDTO); + } + + // 更新字典项 + @Put('/dict-items/:id') + @Validate() + async updateDictItem(@Param('id') id: number, @Body() updateDictItemDTO: UpdateDictItemDTO) { + return this.dictService.updateDictItem(id, updateDictItemDTO); + } + + // 删除字典项 + @Del('/dict-items/:id') + async deleteDictItem(@Param('id') id: number) { + return this.dictService.deleteDictItem(id); + } +} diff --git a/src/dto/dict.dto.ts b/src/dto/dict.dto.ts new file mode 100644 index 0000000..b84ac8e --- /dev/null +++ b/src/dto/dict.dto.ts @@ -0,0 +1,40 @@ +import { Rule, RuleType } from '@midwayjs/validate'; + +// 创建字典的数据传输对象 +export class CreateDictDTO { + @Rule(RuleType.string().required()) + name: string; // 字典名称 + + @Rule(RuleType.string().required()) + title: string; // 字典标题 +} + +// 更新字典的数据传输对象 +export class UpdateDictDTO { + @Rule(RuleType.string()) + name?: string; // 字典名称 (可选) + + @Rule(RuleType.string()) + title?: string; // 字典标题 (可选) +} + +// 创建字典项的数据传输对象 +export class CreateDictItemDTO { + @Rule(RuleType.string().required()) + name: string; // 字典项名称 + + @Rule(RuleType.string().required()) + title: string; // 字典项标题 + + @Rule(RuleType.number().required()) + dictId: number; // 所属字典的ID +} + +// 更新字典项的数据传输对象 +export class UpdateDictItemDTO { + @Rule(RuleType.string()) + name?: string; // 字典项名称 (可选) + + @Rule(RuleType.string()) + title?: string; // 字典项标题 (可选) +} diff --git a/src/service/dict.service.ts b/src/service/dict.service.ts new file mode 100644 index 0000000..8055007 --- /dev/null +++ b/src/service/dict.service.ts @@ -0,0 +1,84 @@ +import { Provide } from '@midwayjs/core'; +import { InjectEntityModel } from '@midwayjs/typeorm'; +import { Repository, Like } from 'typeorm'; +import { Dict } from '../entity/dict.entity'; +import { DictItem } from '../entity/dict_item.entity'; +import { CreateDictDTO, UpdateDictDTO } from '../dto/dict.dto'; +import { CreateDictItemDTO, UpdateDictItemDTO } from '../dto/dict.dto'; + +@Provide() +export class DictService { + @InjectEntityModel(Dict) + dictModel: Repository; + + @InjectEntityModel(DictItem) + dictItemModel: Repository; + + // 获取字典列表,支持按标题搜索 + async getDicts(title?: string) { + // 如果提供了标题,则使用模糊查询 + if (title) { + return this.dictModel.find({ where: { title: Like(`%${title}%`) } }); + } + // 否则,返回所有字典 + return this.dictModel.find(); + } + + // 创建新字典 + async createDict(createDictDTO: CreateDictDTO) { + const dict = new Dict(); + dict.name = createDictDTO.name; + dict.title = createDictDTO.title; + return this.dictModel.save(dict); + } + + // 更新字典 + async updateDict(id: number, updateDictDTO: UpdateDictDTO) { + await this.dictModel.update(id, updateDictDTO); + return this.dictModel.findOneBy({ id }); + } + + // 删除字典及其所有字典项 + async deleteDict(id: number) { + // 首先删除该字典下的所有字典项 + await this.dictItemModel.delete({ dict: { id } }); + // 然后删除字典本身 + const result = await this.dictModel.delete(id); + return result.affected > 0; + } + + // 获取字典项列表,支持按 dictId 过滤 + async getDictItems(dictId?: number) { + // 如果提供了 dictId,则只返回该字典下的项 + if (dictId) { + return this.dictItemModel.find({ where: { dict: { id: dictId } } }); + } + // 否则,返回所有字典项 + return this.dictItemModel.find(); + } + + // 创建新字典项 + async createDictItem(createDictItemDTO: CreateDictItemDTO) { + const dict = await this.dictModel.findOneBy({ id: createDictItemDTO.dictId }); + if (!dict) { + throw new Error('指定的字典不存在'); + } + const item = new DictItem(); + item.name = createDictItemDTO.name; + item.title = createDictItemDTO.title; + item.dict = dict; + return this.dictItemModel.save(item); + } + + // 更新字典项 + async updateDictItem(id: number, updateDictItemDTO: UpdateDictItemDTO) { + await this.dictItemModel.update(id, updateDictItemDTO); + return this.dictItemModel.findOneBy({ id }); + } + + // 删除字典项 + async deleteDictItem(id: number) { + const result = await this.dictItemModel.delete(id); + return result.affected > 0; + } +}