1
+ import { ApiOperation , ApiTags } from '@nestjs/swagger' ;
1
2
import {
2
3
Body ,
3
4
Controller ,
@@ -7,19 +8,20 @@ import {
7
8
Post ,
8
9
Put ,
9
10
Query ,
11
+ Req ,
10
12
UseGuards ,
11
13
UsePipes ,
12
14
ValidationPipe ,
13
15
} from '@nestjs/common' ;
14
- import { ApiOperation , ApiTags } from '@nestjs/swagger' ;
15
16
16
17
import { Pagination , PaginationOptions } from '@/utils/pagination.util' ;
18
+ import { AdministrationGuard } from '@/common/guards/administration.guard' ;
19
+ import { AuthenticatedGuard } from '@/common/guards/authenticated.guard' ;
17
20
18
- import { User as UserModel } from './entities/user.entity' ;
19
21
import { CreateUserDto } from './dto/create-user.dto' ;
20
22
import { UpdateUserDto } from './dto/update-user.dto' ;
23
+ import { User as UserModel } from './entities/user.entity' ;
21
24
import { UserService } from './user.service' ;
22
- import { AuthenticatedGuard } from '@/common/guards/authenticated.guard' ;
23
25
24
26
@ApiTags ( 'Users' )
25
27
@Controller ( {
@@ -30,7 +32,7 @@ export class UserControllerV1 {
30
32
constructor ( private readonly _userService : UserService ) { }
31
33
32
34
@ApiOperation ( { summary : '列出用户列表' } )
33
- @UseGuards ( AuthenticatedGuard )
35
+ @UseGuards ( AuthenticatedGuard , AdministrationGuard )
34
36
@UsePipes ( new ValidationPipe ( { transform : true } ) )
35
37
@Get ( )
36
38
async list (
@@ -40,34 +42,50 @@ export class UserControllerV1 {
40
42
}
41
43
42
44
@ApiOperation ( { summary : '获取特定id的用户' } )
43
- @UseGuards ( AuthenticatedGuard )
45
+ @UseGuards ( AuthenticatedGuard , AdministrationGuard )
44
46
@UsePipes ( new ValidationPipe ( { transform : true } ) )
45
47
@Get ( ':id' )
46
48
async get ( @Param ( 'id' ) userId : number ) : Promise < UserModel > {
47
49
return await this . _userService . get ( userId ) ;
48
50
}
49
51
50
52
@ApiOperation ( { summary : '添加用户' } )
51
- @UseGuards ( AuthenticatedGuard )
52
- @Post ( )
53
+ @UseGuards ( AuthenticatedGuard , AdministrationGuard )
53
54
@UsePipes ( new ValidationPipe ( { transform : true } ) )
55
+ @Post ( )
54
56
async create ( @Body ( ) body : CreateUserDto ) {
55
57
return await this . _userService . create ( body ) ;
56
58
}
57
59
58
60
@ApiOperation ( { summary : '更新用户' } )
59
- @UseGuards ( AuthenticatedGuard )
60
- @Put ( ':id' )
61
+ @UseGuards ( AuthenticatedGuard , AdministrationGuard )
61
62
@UsePipes ( new ValidationPipe ( { transform : true } ) )
63
+ @Put ( ':id' )
62
64
async update ( @Param ( 'id' ) userId : number , @Body ( ) body : UpdateUserDto ) {
63
65
return await this . _userService . update ( userId , body ) ;
64
66
}
65
67
66
68
@ApiOperation ( { summary : '删除用户' } )
67
- @UseGuards ( AuthenticatedGuard )
68
- @Delete ( ':id' )
69
+ @UseGuards ( AuthenticatedGuard , AdministrationGuard )
69
70
@UsePipes ( new ValidationPipe ( { transform : true } ) )
71
+ @Delete ( ':id' )
70
72
async destroy ( @Param ( 'id' ) userId : number ) {
71
73
return await this . _userService . destroy ( userId ) ;
72
74
}
75
+
76
+ @ApiOperation ( { summary : '获取自己的用户信息' } )
77
+ @UseGuards ( AuthenticatedGuard )
78
+ @Get ( 'me' )
79
+ async me ( @Req ( ) req : any ) {
80
+ return req . user ;
81
+ }
82
+
83
+ @ApiOperation ( { summary : '更新自己的用户信息' } )
84
+ @UseGuards ( AuthenticatedGuard )
85
+ @UsePipes ( new ValidationPipe ( { transform : true } ) )
86
+ @Put ( 'me' )
87
+ async updateMe ( @Req ( ) req : any , @Body ( ) body : UpdateUserDto ) {
88
+ const userId = req . user . userId ;
89
+ return await this . _userService . update ( userId , body ) ;
90
+ }
73
91
}
0 commit comments