11package com.softeno.template.app.permission.api
22
33import com.softeno.template.app.common.PrincipalHandler
4- import com.softeno.template.app.permission.db.getPageRequest
54import com.softeno.template.app.permission.mapper.PermissionDto
65import com.softeno.template.app.permission.service.PermissionService
76import io.micrometer.tracing.Tracer
87import kotlinx.coroutines.flow.Flow
98import kotlinx.coroutines.reactor.awaitSingle
10- import kotlinx.coroutines.reactor.awaitSingleOrNull
119import org.apache.commons.logging.LogFactory
1210import org.slf4j.MDC
11+ import org.springframework.data.domain.PageRequest
12+ import org.springframework.data.domain.Sort
1313import org.springframework.http.ResponseEntity
1414import org.springframework.validation.annotation.Validated
15- import org.springframework.web.bind.annotation.DeleteMapping
16- import org.springframework.web.bind.annotation.GetMapping
17- import org.springframework.web.bind.annotation.PathVariable
18- import org.springframework.web.bind.annotation.PostMapping
19- import org.springframework.web.bind.annotation.PutMapping
20- import org.springframework.web.bind.annotation.RequestBody
21- import org.springframework.web.bind.annotation.RequestParam
22- import org.springframework.web.bind.annotation.RestController
15+ import org.springframework.web.bind.annotation.*
2316import reactor.core.publisher.Mono
2417import java.security.Principal
2518
@@ -31,11 +24,24 @@ class PermissionController(
3124) : PrincipalHandler {
3225 private val log = LogFactory .getLog(javaClass)
3326
27+ data class PermissionSearch (
28+ val search : String? ,
29+ val createdBy : String? ,
30+ val createdFrom : Long? ,
31+ val createdTo : Long?
32+ )
33+
3434 @GetMapping(" /permissions" )
35- suspend fun getPermissions (@RequestParam(required = false , defaultValue = " 0" ) page : Int ,
36- @RequestParam(required = false , defaultValue = " 10" ) size : Int ,
37- @RequestParam(required = false , defaultValue = " id" ) sort : String ,
38- @RequestParam(required = false , defaultValue = " ASC" ) direction : String , monoPrincipal : Mono <Principal >
35+ suspend fun getPermissions (
36+ @RequestParam(required = false , defaultValue = " 0" ) page : Int ,
37+ @RequestParam(required = false , defaultValue = " 10" ) size : Int ,
38+ @RequestParam(required = false , defaultValue = " id" ) sort : String ,
39+ @RequestParam(required = false , defaultValue = " ASC" ) direction : String ,
40+ @RequestParam(required = false ) search : String? = null,
41+ @RequestParam(required = false ) createdBy : String? = null,
42+ @RequestParam(required = false ) createdFrom : Long? = null,
43+ @RequestParam(required = false ) createdTo : Long? = null,
44+ monoPrincipal : Mono <Principal >
3945 ): Flow <PermissionDto > {
4046 showPrincipal(log, monoPrincipal)
4147
@@ -45,7 +51,25 @@ class PermissionController(
4551 val mdcSpan = MDC .get(" spanId" )
4652 log.debug(" Show traceId=$traceId , mdcTraceId=$mdc and mdcSpanId=$mdcSpan " )
4753
48- return permissionService.getAllPermissions(getPageRequest(page, size, sort, direction))
54+ log.debug(" Show request params: " +
55+ " page=$page , size=$size , sort=$sort , direction=$direction , " +
56+ " search=$search , createdBy=$createdBy , createdFrom=$createdFrom , createdTo=$createdTo "
57+ )
58+
59+ return permissionService.getAllPermissions(
60+ getPageRequest(page, size, sort, direction),
61+ PermissionSearch (search, createdBy, createdFrom, createdTo)
62+ )
63+ }
64+
65+ @GetMapping(" /permissions/count" )
66+ suspend fun getPermissionsCount (
67+ @RequestParam(required = false ) search : String? = null,
68+ @RequestParam(required = false ) createdBy : String? = null,
69+ @RequestParam(required = false ) createdFrom : Long? = null,
70+ @RequestParam(required = false ) createdTo : Long? = null,
71+ ): Long {
72+ return permissionService.countPermissions(PermissionSearch (search, createdBy, createdFrom, createdTo))
4973 }
5074
5175 @GetMapping(" /permissions/{id}" )
@@ -62,7 +86,11 @@ class PermissionController(
6286 }
6387
6488 @PutMapping(" /permissions/{id}" )
65- suspend fun updatePermission (@PathVariable id : Long , @RequestBody permissionDto : PermissionDto , monoPrincipal : Mono <Principal >): ResponseEntity <PermissionDto > {
89+ suspend fun updatePermission (
90+ @PathVariable id : Long ,
91+ @RequestBody permissionDto : PermissionDto ,
92+ monoPrincipal : Mono <Principal >
93+ ): ResponseEntity <PermissionDto > {
6694 val result = permissionService.updatePermission(id, permissionDto, principal = monoPrincipal.awaitSingle())
6795 return ResponseEntity .ok(result)
6896 }
@@ -71,4 +99,8 @@ class PermissionController(
7199 suspend fun deletePermission (@PathVariable id : Long ) {
72100 permissionService.deletePermission(id)
73101 }
74- }
102+ }
103+
104+ fun getPageRequest (page : Int , size : Int , sort : String , direction : String ) =
105+ Sort .by(Sort .Order (if (direction == " ASC" ) Sort .Direction .ASC else Sort .Direction .DESC , sort))
106+ .let { PageRequest .of(page, size, it) }
0 commit comments