feat(字典): 添加字典和字典项的管理功能
实现字典和字典项的完整CRUD功能,包括: - 字典的创建、查询、更新和删除 - 字典项的创建、查询、更新和删除 - 支持按条件查询字典和字典项 - 使用DTO进行参数校验
This commit is contained in:
parent
46bdcf0c69
commit
889f00bde8
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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; // 字典项标题 (可选)
|
||||||
|
}
|
||||||
|
|
@ -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<Dict>;
|
||||||
|
|
||||||
|
@InjectEntityModel(DictItem)
|
||||||
|
dictItemModel: Repository<DictItem>;
|
||||||
|
|
||||||
|
// 获取字典列表,支持按标题搜索
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue