@@ -6,11 +6,20 @@ import {
6
6
Patch ,
7
7
Param ,
8
8
Delete ,
9
+ UseGuards ,
9
10
} from '@nestjs/common'
10
11
import { PolicyService } from './policy.service'
11
12
import { CreatePolicyDto } from './dto/create-policy.dto'
12
- import { ApiBearerAuth , ApiOperation , ApiTags } from '@nestjs/swagger'
13
+ import {
14
+ ApiBearerAuth ,
15
+ ApiOperation ,
16
+ ApiResponse ,
17
+ ApiTags ,
18
+ } from '@nestjs/swagger'
13
19
import { UpdatePolicyDto } from './dto/update-policy.dto'
20
+ import { ResponseUtil } from 'src/utils/response'
21
+ import { JwtAuthGuard } from 'src/auth/jwt.auth.guard'
22
+ import { ApplicationAuthGuard } from 'src/auth/application.auth.guard'
14
23
15
24
@ApiTags ( 'Database' )
16
25
@ApiBearerAuth ( 'Authorization' )
@@ -19,32 +28,58 @@ export class PolicyController {
19
28
constructor ( private readonly policiesService : PolicyService ) { }
20
29
21
30
@Post ( )
22
- @ApiOperation ( { summary : 'TODO - ⌛️' } )
23
- create ( @Body ( ) createPolicyDto : CreatePolicyDto ) {
24
- return this . policiesService . create ( createPolicyDto )
31
+ @ApiOperation ( { summary : 'Create database policy' } )
32
+ @ApiResponse ( { type : ResponseUtil } )
33
+ @UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
34
+ async create ( @Param ( 'appid' ) appid : string , @Body ( ) dto : CreatePolicyDto ) {
35
+ // check name existed
36
+ const existed = await this . policiesService . findOne ( appid , dto . name )
37
+ if ( existed ) {
38
+ return ResponseUtil . error ( 'Policy name existed' )
39
+ }
40
+ const doc = await this . policiesService . create ( appid , dto )
41
+ return ResponseUtil . ok ( doc )
25
42
}
26
43
27
44
@Get ( )
28
- @ApiOperation ( { summary : 'TODO - ⌛️' } )
29
- findAll ( ) {
30
- return this . policiesService . findAll ( )
45
+ @ApiOperation ( { summary : 'Get database policy list' } )
46
+ @ApiResponse ( { type : ResponseUtil } )
47
+ @UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
48
+ async findAll ( @Param ( 'appid' ) appid : string ) {
49
+ const docs = await this . policiesService . findAll ( appid )
50
+ return ResponseUtil . ok ( docs )
31
51
}
32
52
33
- @Get ( ':id' )
34
- @ApiOperation ( { summary : 'TODO - ⌛️' } )
35
- findOne ( @Param ( 'id' ) id : string ) {
36
- return this . policiesService . findOne ( + id )
53
+ @Patch ( ':name' )
54
+ @ApiOperation ( { summary : 'Update policy rules' } )
55
+ @ApiResponse ( { type : ResponseUtil } )
56
+ @UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
57
+ async update (
58
+ @Param ( 'appid' ) appid : string ,
59
+ @Param ( 'name' ) name : string ,
60
+ @Body ( ) dto : UpdatePolicyDto ,
61
+ ) {
62
+ // check policy exists
63
+ const existed = await this . policiesService . findOne ( appid , name )
64
+ if ( ! existed ) {
65
+ return ResponseUtil . error ( 'Policy not found' )
66
+ }
67
+ const res = await this . policiesService . update ( appid , name , dto )
68
+ return ResponseUtil . ok ( res )
37
69
}
38
70
39
- @Patch ( ':id' )
40
- @ApiOperation ( { summary : 'TODO - ⌛️' } )
41
- update ( @Param ( 'id' ) id : string , @Body ( ) updatePolicyDto : UpdatePolicyDto ) {
42
- return this . policiesService . update ( + id , updatePolicyDto )
43
- }
71
+ @Delete ( ':name' )
72
+ @ApiOperation ( { summary : 'Remove a database policy' } )
73
+ @ApiResponse ( { type : ResponseUtil } )
74
+ @UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
75
+ async remove ( @Param ( 'appid' ) appid : string , @Param ( 'name' ) name : string ) {
76
+ // check policy exists
77
+ const existed = await this . policiesService . findOne ( appid , name )
78
+ if ( ! existed ) {
79
+ return ResponseUtil . error ( 'Policy not found' )
80
+ }
44
81
45
- @Delete ( ':id' )
46
- @ApiOperation ( { summary : 'TODO - ⌛️' } )
47
- remove ( @Param ( 'id' ) id : string ) {
48
- return this . policiesService . remove ( + id )
82
+ const res = await this . policiesService . remove ( appid , name )
83
+ return ResponseUtil . ok ( res )
49
84
}
50
85
}
0 commit comments