Skip to content

Commit

Permalink
refactor: optimize dto type
Browse files Browse the repository at this point in the history
  • Loading branch information
kuizuo committed Nov 24, 2023
1 parent 6279f5e commit ab58714
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 105 deletions.
4 changes: 2 additions & 2 deletions apps/api/src/modules/system/menu/menu.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { flattenDeep } from 'lodash'

import { MenuDto, MenuQueryDto } from './menu.dto'
import { MenuDto, MenuQueryDto, MenuUpdateDto } from './menu.dto'
import { MenuService } from './menu.service'
import { ApiResult } from '@/common/decorators/api-result.decorator'
import { IdParam } from '@/common/decorators/id-param.decorator'
Expand Down Expand Up @@ -72,7 +72,7 @@ export class MenuController {
@Permission(Permissions.UPDATE)
async update(
@IdParam() id: number,
@Body() dto: Partial<MenuDto>,
@Body() dto: MenuUpdateDto,
): Promise<void> {
// check
await this.menuService.check(dto)
Expand Down
28 changes: 3 additions & 25 deletions apps/api/src/modules/system/menu/menu.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '@nestjs/swagger'
import { ApiProperty, PartialType } from '@nestjs/swagger'
import {
IsIn,
IsInt,
Expand Down Expand Up @@ -71,28 +71,6 @@ export class MenuDto {
component?: string
}

export class MenuQueryDto {
@ApiProperty({ description: '菜单名称' })
@IsString()
@IsOptional()
name?: string

@ApiProperty({ description: '路由' })
@IsString()
@IsOptional()
path?: string

@ApiProperty({ description: '权限标识' })
@IsString()
@IsOptional()
permission?: string
export class MenuUpdateDto extends PartialType(MenuDto) {}

@ApiProperty({ description: '组件' })
@IsString()
@IsOptional()
component?: string

@ApiProperty({ description: '状态' })
@IsOptional()
status?: number
}
export class MenuQueryDto extends PartialType(MenuDto) {}
4 changes: 2 additions & 2 deletions apps/api/src/modules/system/menu/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { concat, isEmpty, uniq } from 'lodash'
import { In, IsNull, Like, Not, Repository } from 'typeorm'

import { RoleService } from '../role/role.service'
import { MenuDto, MenuQueryDto } from './menu.dto'
import { MenuDto, MenuQueryDto, MenuUpdateDto } from './menu.dto'
import { BusinessException } from '@/common/exceptions/biz.exception'
import { ErrorEnum } from '@/constants/error-code.constant'
import { MenuEntity } from '@/modules/system/menu/menu.entity'
Expand Down Expand Up @@ -58,7 +58,7 @@ export class MenuService {
await this.menuRepository.save(menu)
}

async update(id: number, menu: Partial<MenuDto>): Promise<void> {
async update(id: number, menu: MenuUpdateDto): Promise<void> {
await this.menuRepository.update(id, menu)
}

Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/modules/system/role/role.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ApiOperation, ApiTags } from '@nestjs/swagger'

import { MenuService } from '../menu/menu.service'
import { RoleDto } from './role.dto'
import { RoleDto, RoleUpdateDto } from './role.dto'
import { RoleService } from './role.service'
import { ApiResult } from '@/common/decorators/api-result.decorator'
import { IdParam } from '@/common/decorators/id-param.decorator'
Expand Down Expand Up @@ -65,7 +65,7 @@ export class RoleController {
@Permission(Permissions.UPDATE)
async update(
@IdParam() id: number,
@Body() dto: Partial<RoleDto>,
@Body() dto: RoleUpdateDto,
): Promise<void> {
await this.roleService.update(id, dto)
await this.menuService.refreshOnlineUserPerms()
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/modules/system/role/role.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '@nestjs/swagger'
import { ApiProperty, PartialType } from '@nestjs/swagger'
import {
IsArray,
IsIn,
Expand Down Expand Up @@ -34,3 +34,5 @@ export class RoleDto {
@IsArray()
menuIds?: number[]
}

export class RoleUpdateDto extends PartialType(RoleDto) {}
4 changes: 2 additions & 2 deletions apps/api/src/modules/system/role/role.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm'
import { isEmpty } from 'lodash'
import { EntityManager, In, Repository } from 'typeorm'

import { RoleDto } from './role.dto'
import { RoleDto, RoleUpdateDto } from './role.dto'
import { RoleInfo } from './role.model'
import { PageOptionsDto } from '@/common/dto/page-options.dto'
import { IAppConfig } from '@/config'
Expand Down Expand Up @@ -82,7 +82,7 @@ export class RoleService {
/**
* 更新角色信息
*/
async update(id, { menuIds, ...data }: Partial<RoleDto>): Promise<void> {
async update(id, { menuIds, ...data }: RoleUpdateDto): Promise<void> {
await this.roleRepository.update(id, data)

if (!isEmpty(menuIds)) {
Expand Down
7 changes: 2 additions & 5 deletions apps/api/src/modules/system/task/task.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'

import { TaskDto, TaskQueryDto } from './task.dto'
import { TaskDto, TaskQueryDto, TaskUpdateDto } from './task.dto'
import { TaskService } from './task.service'
import { ApiResult } from '@/common/decorators/api-result.decorator'
import { IdParam } from '@/common/decorators/id-param.decorator'
Expand Down Expand Up @@ -48,10 +48,7 @@ export class TaskController {
@Put(':id')
@ApiOperation({ summary: '更新任务' })
@Permission(Permissions.UPDATE)
async update(
@IdParam() id: number,
@Body() dto: Partial<TaskDto>,
): Promise<void> {
async update(@IdParam() id: number, @Body() dto: TaskUpdateDto): Promise<void> {
const serviceCall = dto.service.split('.')
await this.taskService.checkHasMissionMeta(serviceCall[0], serviceCall[1])
await this.taskService.update(id, dto)
Expand Down
25 changes: 3 additions & 22 deletions apps/api/src/modules/system/task/task.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BadRequestException } from '@nestjs/common'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { ApiProperty, ApiPropertyOptional, IntersectionType, PartialType } from '@nestjs/swagger'
import {
IsDateString,
IsIn,
Expand Down Expand Up @@ -100,25 +100,6 @@ export class TaskDto {
remark?: string
}

export class TaskQueryDto extends PageOptionsDto {
@ApiProperty({ description: '任务名称' })
@IsOptional()
@IsString()
name?: string

@ApiProperty({ description: '调用的服务' })
@IsOptional()
@IsString()
@MinLength(1)
service: string
export class TaskUpdateDto extends PartialType(TaskDto) {}

@ApiProperty({ description: '任务类别:cron | interval' })
@IsOptional()
@IsIn([0, 1])
type?: number

@ApiProperty({ description: '任务状态' })
@IsOptional()
@IsIn([0, 1])
status?: number
}
export class TaskQueryDto extends IntersectionType(PageOptionsDto, PartialType(TaskDto)) {}
4 changes: 2 additions & 2 deletions apps/api/src/modules/system/task/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
SYS_TASK_QUEUE_PREFIX,
TaskStatus,
} from './constant'
import { TaskDto, TaskQueryDto } from './task.dto'
import { TaskDto, TaskQueryDto, TaskUpdateDto } from './task.dto'
import { BusinessException } from '@/common/exceptions/biz.exception'
import { ErrorEnum } from '@/constants/error-code.constant'

Expand Down Expand Up @@ -159,7 +159,7 @@ export class TaskService implements OnModuleInit {
await this.start(task)
}

async update(id: number, dto: Partial<TaskDto>): Promise<void> {
async update(id: number, dto: TaskUpdateDto): Promise<void> {
await this.taskRepository.update(id, dto)
const task = await this.info(id)
if (task.status === 0)
Expand Down
12 changes: 7 additions & 5 deletions apps/api/src/modules/todo/todo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
Get,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'

import { TodoDto } from './todo.dto'
import { TodoDto, TodoQueryDto, TodoUpdateDto } from './todo.dto'
import { TodoService } from './todo.service'
import { ApiResult } from '@/common/decorators/api-result.decorator'
import { IdParam } from '@/common/decorators/id-param.decorator'
Expand All @@ -19,6 +20,7 @@ import { Resource } from '@/modules/auth/decorators/resource.decorator'

import { ResourceGuard } from '@/modules/auth/guards/resource.guard'
import { TodoEntity } from '@/modules/todo/todo.entity'
import { Pagination } from '@/helper/paginate/pagination'

export const Permissions = {
LIST: 'todo:list',
Expand All @@ -30,16 +32,16 @@ export const Permissions = {

@ApiTags('Business - Todo模块')
@UseGuards(ResourceGuard)
@Controller('todo')
@Controller('todos')
export class TodoController {
constructor(private readonly todoService: TodoService) {}

@Get()
@ApiOperation({ summary: '获取Todo列表' })
@ApiResult({ type: [TodoEntity] })
@Permission(Permissions.LIST)
async list(): Promise<TodoEntity[]> {
return this.todoService.list()
async list(@Query() dto: TodoQueryDto): Promise<Pagination<TodoEntity>> {
return this.todoService.list(dto)
}

@Get(':id')
Expand All @@ -63,7 +65,7 @@ export class TodoController {
@Resource(TodoEntity)
async update(
@IdParam() id: number,
@Body() dto: Partial<TodoDto>,
@Body() dto: TodoUpdateDto,
): Promise<void> {
await this.todoService.update(id, dto)
}
Expand Down
6 changes: 4 additions & 2 deletions apps/api/src/modules/todo/todo.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '@nestjs/swagger'
import { ApiProperty, IntersectionType, PartialType } from '@nestjs/swagger'
import { IsString } from 'class-validator'

import { PageOptionsDto } from '@/common/dto/page-options.dto'
Expand All @@ -9,4 +9,6 @@ export class TodoDto {
value: string
}

export class TodoQueryDto extends PageOptionsDto<TodoDto> {}
export class TodoUpdateDto extends PartialType(TodoDto) {}

export class TodoQueryDto extends IntersectionType(PageOptionsDto, TodoDto) {}
15 changes: 4 additions & 11 deletions apps/api/src/modules/todo/todo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'

import { TodoDto, TodoQueryDto } from './todo.dto'
import { TodoDto, TodoQueryDto, TodoUpdateDto } from './todo.dto'
import { paginate } from '@/helper/paginate'
import { Pagination } from '@/helper/paginate/pagination'
import { TodoEntity } from '@/modules/todo/todo.entity'
Expand All @@ -14,11 +14,7 @@ export class TodoService {
private todoRepository: Repository<TodoEntity>,
) {}

async list(): Promise<TodoEntity[]> {
return this.todoRepository.find()
}

async page({
async list({
page,
pageSize,
}: TodoQueryDto): Promise<Pagination<TodoEntity>> {
Expand All @@ -34,13 +30,10 @@ export class TodoService {
}

async create(dto: TodoDto) {
let test = new TodoEntity()
test = Object.assign(dto)

await this.todoRepository.save(test)
await this.todoRepository.save(dto)
}

async update(id: number, dto: Partial<TodoDto>) {
async update(id: number, dto: TodoUpdateDto) {
await this.todoRepository.update(id, dto)
}

Expand Down
18 changes: 3 additions & 15 deletions apps/api/src/modules/user/dto/user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '@nestjs/swagger'
import { ApiProperty, IntersectionType, PartialType } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import {
ArrayMaxSize,
Expand Down Expand Up @@ -81,26 +81,14 @@ export class UserDto {
status: number
}

export class UserListDto extends PageOptionsDto<UserDto> {
@ApiProperty({ description: '登录账号' })
@IsString()
@IsOptional()
username: string

@ApiProperty({ description: '呢称' })
@IsOptional()
nickname: string
export class UserUpdateDto extends PartialType(UserDto) {}

export class UserQueryDto extends IntersectionType(PageOptionsDto<UserDto>, PartialType(UserDto)) {
@ApiProperty({ description: '归属大区', example: 1 })
@IsInt()
@IsOptional()
deptId: number

@ApiProperty({ description: '邮箱', example: 'hi@kuizuo.cn' })
@IsEmail()
@IsOptional()
email: string

@ApiProperty({ description: '状态', example: 0 })
@IsInt()
@IsOptional()
Expand Down
8 changes: 4 additions & 4 deletions apps/api/src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'

import { UserPasswordDto } from './dto/password.dto'
import { UserDto, UserListDto } from './dto/user.dto'
import { UserDto, UserQueryDto , UserUpdateDto } from './dto/user.dto'
import { UserService } from './user.service'
import { IdParam } from '@/common/decorators/id-param.decorator'
import { ApiSecurityAuth } from '@/common/decorators/swagger.decorator'
Expand Down Expand Up @@ -32,8 +32,8 @@ export class UserController {
@Get()
@ApiOperation({ summary: '获取用户列表' })
@Permission(Permissions.LIST)
async list(@Query() dto: UserListDto) {
return this.userService.findAll(dto)
async list(@Query() dto: UserQueryDto ) {
return this.userService.list(dto)
}

@Get(':id')
Expand All @@ -55,7 +55,7 @@ export class UserController {
@Permission(Permissions.UPDATE)
async update(
@IdParam() id: number,
@Body() dto: Partial<UserDto>,
@Body() dto: UserUpdateDto,
): Promise<void> {
await this.userService.update(id, dto)
await this.menuService.refreshPerms(id)
Expand Down
Loading

0 comments on commit ab58714

Please sign in to comment.