exception.controller.ts
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Controller, Get, Post, Patch, Query, Delete, Body, Param, Headers, UseFilters, HttpException, HttpStatus, ParseIntPipe } from '@nestjs/common'
import { ApiTags, ApiBearerAuth, ApiBody, ApiParam } from '@nestjs/swagger'
import { ExceptionService } from './exception.service'
import { HttpExceptionFilter } from '../../common/filters/http-exception.filter'
@ApiBearerAuth()
@ApiTags('exception')
// @UseFilters(new HttpExceptionFilter())
@Controller('/exception')
export class ExceptionController {
constructor(private readonly exceptionService: ExceptionService) {}
// 查询
@Get()
fetch(@Query() { id }, @Headers('token') token): string {
if (!id) {
throw new HttpException({ status: HttpStatus.BAD_REQUEST, message: '请求参数id 必传', error: 'id is required' }, HttpStatus.BAD_REQUEST)
}
return this.exceptionService.fetch(id)
}
// 创建
@Post()
@ApiBody({ description: '填写更新内容' })
save(@Body() { message, id }): string {
return this.exceptionService.save(message)
}
// 更新
@Patch(':id')
@ApiParam({ name: 'id' })
@ApiBody({ description: '请输入message' })
// 有时,我们希望参数的类型为数字,则可以通过管道进行转换
// !现nest自带部分管道
update(@Param('id', new ParseIntPipe()) id, @Body() { message }): string {
console.log(typeof id)
return this.exceptionService.update(id, message)
}
// 删除
@Delete()
remove(@Query() { id }): string {
return this.exceptionService.remove(id)
}
}