feat: 优化部分接口

master
luoer 2023-08-11 17:35:17 +08:00
parent 06eb67ad55
commit 2877bf29fc
9 changed files with 42 additions and 29 deletions

Binary file not shown.

View File

@ -38,7 +38,6 @@
"rxjs": "^7.2.0" "rxjs": "^7.2.0"
}, },
"devDependencies": { "devDependencies": {
"@compodoc/compodoc": "^1.1.21",
"@nestjs/cache-manager": "^2.1.0", "@nestjs/cache-manager": "^2.1.0",
"@nestjs/cli": "^9.0.0", "@nestjs/cli": "^9.0.0",
"@nestjs/config": "^2.3.1", "@nestjs/config": "^2.3.1",

View File

@ -23,4 +23,23 @@ export class BaseService {
take: size == 0 ? this.config.defaultPageSize : size, take: size == 0 ? this.config.defaultPageSize : size,
}; };
} }
paginizate(paging: { page?: number; size?: number; sort?: string }, options: { full?: boolean } = {}) {
const page = paging.page || this.config.defaultPage;
const size = paging.size || this.config.defaultPageSize;
const sort = paging.sort || 'id:desc';
if (size == 0 && options.full) {
return {};
}
return {
skip: (page - 1) * size,
take: size == 0 ? this.config.defaultPageSize : size,
order: sort.split(',').map((item) => {
const [field, order] = item.split(':');
return {
[field]: order,
};
}),
};
}
} }

View File

@ -1,5 +1,5 @@
import { Transform } from 'class-transformer'; import { Transform } from 'class-transformer';
import { IsNumber, IsOptional, Min } from 'class-validator'; import { IsNumber, IsOptional, IsString, Matches, Min } from 'class-validator';
/** /**
* DTO * DTO
@ -12,6 +12,14 @@ import { IsNumber, IsOptional, Min } from 'class-validator';
* ``` * ```
*/ */
export class PaginationDto { export class PaginationDto {
/**
*
* @example 'id:desc'
*/
@IsOptional()
@Matches(/^(\w+:\w+,)*\w+:\w+$/)
sort?: string = 'id:desc';
/** /**
* *
* @example 1 * @example 1

View File

@ -1,16 +1,4 @@
import { import { Body, Controller, Delete, Get, NotFoundException, Param, Patch, Post, Query, Res } from '@nestjs/common';
Body,
Controller,
Delete,
Get,
NotFoundException,
Param,
ParseIntPipe,
Patch,
Post,
Query,
Res,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreatePostDto } from './dto/create-post.dto'; import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto'; import { UpdatePostDto } from './dto/update-post.dto';
@ -58,19 +46,19 @@ export class PostController extends BaseController {
@Get(':id') @Get(':id')
@ApiOperation({ description: '查询文章', operationId: 'getPost' }) @ApiOperation({ description: '查询文章', operationId: 'getPost' })
findOne(@Param('id', ParseIntPipe) id: number) { findOne(@Param('id') id: number) {
return this.postService.findOne(id); return this.postService.findOne(id);
} }
@Patch(':id') @Patch(':id')
@ApiOperation({ description: '更新文章', operationId: 'updatePost' }) @ApiOperation({ description: '更新文章', operationId: 'updatePost' })
update(@Param('id', ParseIntPipe) id: number, @Body() updatePostDto: UpdatePostDto) { update(@Param('id') id: number, @Body() updatePostDto: UpdatePostDto) {
return this.postService.update(id, updatePostDto); return this.postService.update(id, updatePostDto);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ description: '删除文章', operationId: 'delPost' }) @ApiOperation({ description: '删除文章', operationId: 'delPost' })
remove(@Param('id', ParseIntPipe) id: number) { remove(@Param('id') id: number) {
return this.postService.remove(+id); return this.postService.remove(+id);
} }
} }

View File

@ -25,19 +25,19 @@ export class RoleController {
@Get(':id') @Get(':id')
@ApiOperation({ description: '查询角色', operationId: 'getRole' }) @ApiOperation({ description: '查询角色', operationId: 'getRole' })
findOne(@Param('id') id: string) { findOne(@Param('id') id: number) {
return this.roleService.findOne(+id); return this.roleService.findOne(+id);
} }
@Patch(':id') @Patch(':id')
@ApiOperation({ description: '更新角色', operationId: 'updateRole' }) @ApiOperation({ description: '更新角色', operationId: 'updateRole' })
update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) { update(@Param('id') id: number, @Body() updateRoleDto: UpdateRoleDto) {
return this.roleService.update(+id, updateRoleDto); return this.roleService.update(+id, updateRoleDto);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ description: '删除角色', operationId: 'delRole' }) @ApiOperation({ description: '删除角色', operationId: 'delRole' })
remove(@Param('id') id: string) { remove(@Param('id') id: number) {
return this.roleService.remove(+id); return this.roleService.remove(+id);
} }
} }

View File

@ -17,7 +17,6 @@ export class UploadController {
@ApiBody({ description: '要上传的文件', type: CreateUploadDto }) @ApiBody({ description: '要上传的文件', type: CreateUploadDto })
@ApiOperation({ description: '上传文件', operationId: 'addFile' }) @ApiOperation({ description: '上传文件', operationId: 'addFile' })
create(@UploadedFile() file: Express.Multer.File, @Req() req: Request, @Ip() ip: string) { create(@UploadedFile() file: Express.Multer.File, @Req() req: Request, @Ip() ip: string) {
console.log(`ip: ${ip}, req: ${JSON.stringify(req.user)}`);
return this.uploadService.create(file); return this.uploadService.create(file);
} }
@ -30,7 +29,7 @@ export class UploadController {
@Get(':id') @Get(':id')
@ApiOperation({ description: '查询', operationId: 'getFile' }) @ApiOperation({ description: '查询', operationId: 'getFile' })
findOne(@Param('id') id: string) { findOne(@Param('id') id: number) {
return this.uploadService.findOne(+id); return this.uploadService.findOne(+id);
} }
@ -42,7 +41,7 @@ export class UploadController {
@Delete(':id') @Delete(':id')
@ApiOperation({ description: '删除', operationId: 'delFile' }) @ApiOperation({ description: '删除', operationId: 'delFile' })
remove(@Param('id') id: string) { remove(@Param('id') id: number) {
return this.uploadService.remove(+id); return this.uploadService.remove(+id);
} }
} }

View File

@ -1,6 +1,6 @@
import { BaseController } from '@/common/base'; import { BaseController } from '@/common/base';
import { Respond, RespondType } from '@/common/response'; import { Respond, RespondType } from '@/common/response';
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from '@nestjs/common'; import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { CreateUserDto } from './dto/create-user.dto'; import { CreateUserDto } from './dto/create-user.dto';
import { FindUserDto } from './dto/find-user.dto'; import { FindUserDto } from './dto/find-user.dto';
@ -37,7 +37,7 @@ export class UserController extends BaseController {
* ID * ID
*/ */
@Get(':id') @Get(':id')
getUser(@Param('id', ParseIntPipe) id: number): Promise<User> { getUser(@Param('id') id: number): Promise<User> {
return this.userService.findOne(id); return this.userService.findOne(id);
} }
@ -53,7 +53,7 @@ export class UserController extends BaseController {
* ID * ID
*/ */
@Delete(':id') @Delete(':id')
delUser(@Param('id', ParseIntPipe) id: number) { delUser(@Param('id') id: number) {
return this.userService.remove(+id); return this.userService.remove(+id);
} }
} }

View File

@ -29,9 +29,9 @@ export class UserService extends BaseService {
* *
*/ */
async findMany(findUserdto: FindUserDto) { async findMany(findUserdto: FindUserDto) {
const { nickname: _nickname, page, size } = findUserdto; const { nickname: _nickname, } = findUserdto;
const nickname = _nickname && Like(`%${_nickname}%`); const nickname = _nickname && Like(`%${_nickname}%`);
const { skip, take } = this.formatPagination(page, size, true); const { skip, take } = this.paginizate(findUserdto, { full: true });
return this.userRepository.findAndCount({ skip, take, where: { nickname } }); return this.userRepository.findAndCount({ skip, take, where: { nickname } });
} }