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"
},
"devDependencies": {
"@compodoc/compodoc": "^1.1.21",
"@nestjs/cache-manager": "^2.1.0",
"@nestjs/cli": "^9.0.0",
"@nestjs/config": "^2.3.1",

View File

@ -23,4 +23,23 @@ export class BaseService {
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 { IsNumber, IsOptional, Min } from 'class-validator';
import { IsNumber, IsOptional, IsString, Matches, Min } from 'class-validator';
/**
* DTO
@ -12,6 +12,14 @@ import { IsNumber, IsOptional, Min } from 'class-validator';
* ```
*/
export class PaginationDto {
/**
*
* @example 'id:desc'
*/
@IsOptional()
@Matches(/^(\w+:\w+,)*\w+:\w+$/)
sort?: string = 'id:desc';
/**
*
* @example 1

View File

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

View File

@ -25,19 +25,19 @@ export class RoleController {
@Get(':id')
@ApiOperation({ description: '查询角色', operationId: 'getRole' })
findOne(@Param('id') id: string) {
findOne(@Param('id') id: number) {
return this.roleService.findOne(+id);
}
@Patch(':id')
@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);
}
@Delete(':id')
@ApiOperation({ description: '删除角色', operationId: 'delRole' })
remove(@Param('id') id: string) {
remove(@Param('id') id: number) {
return this.roleService.remove(+id);
}
}

View File

@ -17,7 +17,6 @@ export class UploadController {
@ApiBody({ description: '要上传的文件', type: CreateUploadDto })
@ApiOperation({ description: '上传文件', operationId: 'addFile' })
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);
}
@ -30,7 +29,7 @@ export class UploadController {
@Get(':id')
@ApiOperation({ description: '查询', operationId: 'getFile' })
findOne(@Param('id') id: string) {
findOne(@Param('id') id: number) {
return this.uploadService.findOne(+id);
}
@ -42,7 +41,7 @@ export class UploadController {
@Delete(':id')
@ApiOperation({ description: '删除', operationId: 'delFile' })
remove(@Param('id') id: string) {
remove(@Param('id') id: number) {
return this.uploadService.remove(+id);
}
}

View File

@ -1,6 +1,6 @@
import { BaseController } from '@/common/base';
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 { CreateUserDto } from './dto/create-user.dto';
import { FindUserDto } from './dto/find-user.dto';
@ -37,7 +37,7 @@ export class UserController extends BaseController {
* ID
*/
@Get(':id')
getUser(@Param('id', ParseIntPipe) id: number): Promise<User> {
getUser(@Param('id') id: number): Promise<User> {
return this.userService.findOne(id);
}
@ -53,7 +53,7 @@ export class UserController extends BaseController {
* ID
*/
@Delete(':id')
delUser(@Param('id', ParseIntPipe) id: number) {
delUser(@Param('id') id: number) {
return this.userService.remove(+id);
}
}

View File

@ -29,9 +29,9 @@ export class UserService extends BaseService {
*
*/
async findMany(findUserdto: FindUserDto) {
const { nickname: _nickname, page, size } = findUserdto;
const { nickname: _nickname, } = findUserdto;
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 } });
}