From 7a9cd158de28a41fc97948deb698e23cddc8f4ba Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Sun, 12 Nov 2023 22:53:22 +0800 Subject: [PATCH] Add role chang status (not finish) --- .../controller/permission/GroupController.kt | 5 ++-- .../controller/permission/PowerController.kt | 3 ++- .../controller/permission/RoleController.kt | 24 +++++++++++++++---- .../api/converter/permission/RoleConverter.kt | 15 ++++++++---- .../api/param/authentication/GroupGetParam.kt | 2 +- .../api/param/authentication/RoleAddParam.kt | 2 +- .../authentication/RoleChangeStatusParam.kt | 18 ++++++++++++++ .../api/param/authentication/RoleGetParam.kt | 2 +- .../api/service/permission/IRoleService.kt | 3 +++ .../permission/impl/RoleServiceImpl.kt | 5 ++++ 10 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/top/fatweb/api/param/authentication/RoleChangeStatusParam.kt diff --git a/src/main/kotlin/top/fatweb/api/controller/permission/GroupController.kt b/src/main/kotlin/top/fatweb/api/controller/permission/GroupController.kt index fefe3a1..1627f13 100644 --- a/src/main/kotlin/top/fatweb/api/controller/permission/GroupController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/permission/GroupController.kt @@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import top.fatweb.api.converter.permission.GroupConverter -import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.param.authentication.GroupGetParam import top.fatweb.api.service.permission.IGroupService @@ -31,8 +30,8 @@ class GroupController( @Operation(summary = "获取用户组列表") @GetMapping fun get(@Valid groupGetParam: GroupGetParam?): ResponseResult> { - return ResponseResult.success( - ResponseCode.DATABASE_SELECT_SUCCESS, data = GroupConverter.groupPageToGroupPageVo( + return ResponseResult.databaseSuccess( + data = GroupConverter.groupPageToGroupPageVo( groupService.getPage(groupGetParam) ) ) diff --git a/src/main/kotlin/top/fatweb/api/controller/permission/PowerController.kt b/src/main/kotlin/top/fatweb/api/controller/permission/PowerController.kt index 33d1e97..567ebd3 100644 --- a/src/main/kotlin/top/fatweb/api/controller/permission/PowerController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/permission/PowerController.kt @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import top.fatweb.api.converter.permission.PowerConverter +import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.service.permission.IPowerService /** @@ -19,5 +20,5 @@ class PowerController( ) { @Operation(summary = "获取权限列表") @GetMapping - fun get() = PowerConverter.powerSetToPowerSetVo(powerService.getAll()) + fun get() = ResponseResult.databaseSuccess(data = PowerConverter.powerSetToPowerSetVo(powerService.getAll())) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt b/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt index 99c2e5b..d2b9543 100644 --- a/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt @@ -8,6 +8,7 @@ import top.fatweb.api.converter.permission.RoleConverter import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.param.authentication.RoleAddParam +import top.fatweb.api.param.authentication.RoleChangeStatusParam import top.fatweb.api.param.authentication.RoleGetParam import top.fatweb.api.service.permission.IRoleService import top.fatweb.api.vo.PageVo @@ -29,8 +30,8 @@ class RoleController( @Operation(summary = "获取角色列表") @GetMapping fun get(roleGetParam: RoleGetParam?): ResponseResult> { - return ResponseResult.success( - ResponseCode.DATABASE_SELECT_SUCCESS, data = RoleConverter.rolePageToRoleWithPowerPageVo( + return ResponseResult.databaseSuccess( + data = RoleConverter.rolePageToRoleWithPowerPageVo( roleService.getPage(roleGetParam) ) ) @@ -40,7 +41,22 @@ class RoleController( @PostMapping fun add(@Valid @RequestBody roleAddParam: RoleAddParam): ResponseResult { return roleService.add(roleAddParam) - ?.let { ResponseResult.success(ResponseCode.DATABASE_INSERT_SUCCESS, data = RoleConverter.roleToRoleVo(it)) } - ?: let { ResponseResult.fail(ResponseCode.DATABASE_INSERT_FAILED) } + ?.let { + ResponseResult.databaseSuccess( + ResponseCode.DATABASE_INSERT_SUCCESS, + data = RoleConverter.roleToRoleVo(it) + ) + } + ?: let { ResponseResult.databaseFail(ResponseCode.DATABASE_INSERT_FAILED) } + } + + @Operation(summary = "修改角色状态") + @PatchMapping + fun changStatus(@Valid @RequestBody roleChangeStatusParam: RoleChangeStatusParam): ResponseResult { + return if (roleService.changeStatus(roleChangeStatusParam)) { + ResponseResult.databaseSuccess(ResponseCode.DATABASE_UPDATE_SUCCESS) + } else { + ResponseResult.databaseFail(ResponseCode.DATABASE_UPDATE_FILED) + } } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt b/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt index 876d347..8fc3b84 100644 --- a/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt @@ -4,11 +4,13 @@ import com.baomidou.mybatisplus.core.metadata.IPage import top.fatweb.api.entity.permission.Power import top.fatweb.api.entity.permission.Role import top.fatweb.api.param.authentication.RoleAddParam +import top.fatweb.api.param.authentication.RoleChangeStatusParam import top.fatweb.api.vo.PageVo -import top.fatweb.api.vo.permission.* +import top.fatweb.api.vo.permission.RoleVo +import top.fatweb.api.vo.permission.RoleWithPowerVo object RoleConverter { - fun roleToRoleVo(role: Role): RoleVo = RoleVo( + fun roleToRoleVo(role: Role) = RoleVo( id = role.id, name = role.name, enable = role.enable == 1 @@ -24,7 +26,7 @@ object RoleConverter { operations = role.operations?.map { OperationConverter.operationToOperationVo(it) } ) - fun rolePageToRoleWithPowerPageVo(rolePage: IPage): PageVo = PageVo( + fun rolePageToRoleWithPowerPageVo(rolePage: IPage) = PageVo( total = rolePage.total, pages = rolePage.pages, size = rolePage.size, @@ -34,9 +36,14 @@ object RoleConverter { } ) - fun roleAddParamToRole(roleAddParam: RoleAddParam): Role = Role().apply { + fun roleAddParamToRole(roleAddParam: RoleAddParam) = Role().apply { name = roleAddParam.name enable = if (roleAddParam.enable == true) 1 else 0 powers = roleAddParam.powerIds?.map { Power().apply { id = it } } } + + fun roleChangeStatusParamToRole(roleChangeStatusParam: RoleChangeStatusParam) = Role().apply { + id = roleChangeStatusParam.id + enable = if (roleChangeStatusParam.enable) 1 else 0 + } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/GroupGetParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/GroupGetParam.kt index f076290..e80e28b 100644 --- a/src/main/kotlin/top/fatweb/api/param/authentication/GroupGetParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/authentication/GroupGetParam.kt @@ -7,6 +7,6 @@ data class GroupGetParam( @Schema(description = "查询用户组名称") val searchName: String? = null, - @Schema(description = "查询使用正则表达式") + @Schema(description = "查询使用正则表达式", allowableValues = ["true", "false"]) val searchRegex: Boolean = false, ) : PageSortParam() diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/RoleAddParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/RoleAddParam.kt index 0a0b674..eeb168a 100644 --- a/src/main/kotlin/top/fatweb/api/param/authentication/RoleAddParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/authentication/RoleAddParam.kt @@ -8,7 +8,7 @@ data class RoleAddParam( @field:NotBlank(message = "Name can not be blank") val name: String?, - @Schema(description = "启用") + @Schema(description = "启用", allowableValues = ["true", "false"]) val enable: Boolean? = true, @Schema(description = "权限 ID 列表") diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/RoleChangeStatusParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/RoleChangeStatusParam.kt new file mode 100644 index 0000000..eec8de2 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/authentication/RoleChangeStatusParam.kt @@ -0,0 +1,18 @@ +package top.fatweb.api.param.authentication + +import com.fasterxml.jackson.databind.annotation.JsonSerialize +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer +import io.swagger.v3.oas.annotations.media.Schema +import jakarta.validation.constraints.NotNull +import jakarta.validation.constraints.Pattern + +data class RoleChangeStatusParam( + @Schema(description = "角色 ID") + @field:Pattern(regexp = "^\\d+$", message = "ID must be number") + @JsonSerialize(using = ToStringSerializer::class) + val id: Long, + + @Schema(description = "启用", allowableValues = ["true", "false"]) + @field:NotNull + val enable: Boolean +) \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/RoleGetParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/RoleGetParam.kt index 99e44e3..4d746e5 100644 --- a/src/main/kotlin/top/fatweb/api/param/authentication/RoleGetParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/authentication/RoleGetParam.kt @@ -7,6 +7,6 @@ data class RoleGetParam( @Schema(description = "查询角色名称") val searchName: String? = null, - @Schema(description = "查询使用正则表达式") + @Schema(description = "查询使用正则表达式", allowableValues = ["true", "false"]) val searchRegex: Boolean = false, ) : PageSortParam() diff --git a/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt b/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt index 16321e9..5eec26c 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage import com.baomidou.mybatisplus.extension.service.IService import top.fatweb.api.entity.permission.Role import top.fatweb.api.param.authentication.RoleAddParam +import top.fatweb.api.param.authentication.RoleChangeStatusParam import top.fatweb.api.param.authentication.RoleGetParam /** @@ -18,4 +19,6 @@ interface IRoleService : IService { fun getPage(roleGetParam: RoleGetParam?): IPage fun add(roleAddParam: RoleAddParam): Role? + + fun changeStatus(roleChangeStatusParam: RoleChangeStatusParam): Boolean } diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt index a6758d0..6a8e745 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt @@ -10,6 +10,7 @@ import top.fatweb.api.entity.permission.PowerRole import top.fatweb.api.entity.permission.Role import top.fatweb.api.mapper.permission.RoleMapper import top.fatweb.api.param.authentication.RoleAddParam +import top.fatweb.api.param.authentication.RoleChangeStatusParam import top.fatweb.api.param.authentication.RoleGetParam import top.fatweb.api.service.permission.IPowerRoleService import top.fatweb.api.service.permission.IRoleService @@ -59,4 +60,8 @@ class RoleServiceImpl( return null } + + override fun changeStatus(roleChangeStatusParam: RoleChangeStatusParam): Boolean { + return updateById(RoleConverter.roleChangeStatusParamToRole(roleChangeStatusParam)) + } }