From d176cc684beb2736334dad5b882be20b0fd81902 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Thu, 9 Nov 2023 23:28:43 +0800 Subject: [PATCH] Finish role add and get --- .../controller/permission/RoleController.kt | 15 +++---- .../api/converter/permission/RoleConverter.kt | 45 ++++++++++++++++--- .../api/converter/permission/UserConverter.kt | 3 +- .../fatweb/api/handler/ExceptionHandler.kt | 6 +++ .../api/param/authentication/RoleAddParam.kt | 6 +-- .../api/service/permission/IRoleService.kt | 3 +- .../permission/impl/RoleServiceImpl.kt | 11 +++-- .../top/fatweb/api/vo/permission/MenuVo.kt | 5 ++- .../api/vo/permission/RoleWithPowerVo.kt | 14 +++++- 9 files changed, 83 insertions(+), 25 deletions(-) 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 32af4e6..99c2e5b 100644 --- a/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt @@ -2,10 +2,8 @@ package top.fatweb.api.controller.permission import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.tags.Tag -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RestController +import jakarta.validation.Valid +import org.springframework.web.bind.annotation.* import top.fatweb.api.converter.permission.RoleConverter import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseResult @@ -14,6 +12,7 @@ import top.fatweb.api.param.authentication.RoleGetParam import top.fatweb.api.service.permission.IRoleService import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.permission.RoleVo +import top.fatweb.api.vo.permission.RoleWithPowerVo /** * 角色表 前端控制器 @@ -29,9 +28,9 @@ class RoleController( ) { @Operation(summary = "获取角色列表") @GetMapping - fun get(roleGetParam: RoleGetParam?): ResponseResult> { + fun get(roleGetParam: RoleGetParam?): ResponseResult> { return ResponseResult.success( - ResponseCode.DATABASE_SELECT_SUCCESS, data = RoleConverter.rolePageToRolePageVo( + ResponseCode.DATABASE_SELECT_SUCCESS, data = RoleConverter.rolePageToRoleWithPowerPageVo( roleService.getPage(roleGetParam) ) ) @@ -39,9 +38,9 @@ class RoleController( @Operation(summary = "添加角色") @PostMapping - fun add(roleAddParam: RoleAddParam): ResponseResult { + fun add(@Valid @RequestBody roleAddParam: RoleAddParam): ResponseResult { return roleService.add(roleAddParam) - ?.let { ResponseResult.success(ResponseCode.DATABASE_INSERT_SUCCESS, data = it) } + ?.let { ResponseResult.success(ResponseCode.DATABASE_INSERT_SUCCESS, data = RoleConverter.roleToRoleVo(it)) } ?: let { ResponseResult.fail(ResponseCode.DATABASE_INSERT_FAILED) } } } \ 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 2a0924f..04d92c0 100644 --- a/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt @@ -5,26 +5,61 @@ 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.vo.PageVo -import top.fatweb.api.vo.permission.RoleVo +import top.fatweb.api.vo.permission.* object RoleConverter { - fun rolePageToRolePageVo(rolePage: IPage): PageVo = PageVo( + fun rolePageToRoleWithPowerPageVo(rolePage: IPage): PageVo = PageVo( rolePage.total, rolePage.pages, rolePage.size, rolePage.current, rolePage.records.map { - RoleVo( + RoleWithPowerVo( id = it.id, name = it.name, - enable = it.enable == 1 + enable = it.enable == 1, + modules = it.modules?.map { module -> + ModuleVo( + id = module.id, + name = module.name, + powerId = module.powerId + ) + }, + menus = it.menus?.map { menu -> + MenuVo( + id = menu.id, + name = menu.name, + url = menu.url, + powerId = menu.powerId, + parentId = menu.powerId, + moduleId = menu.moduleId + ) + }, + elements = it.elements?.map { element -> + ElementVo( + id = element.id, + name = element.name, + powerId = element.powerId, + parentId = element.parentId, + menuId = element.menuId + ) + }, + operations = it.operations?.map { operation -> + OperationVo( + id = operation.id, + name = operation.name, + code = operation.code, + powerId = operation.powerId, + elementId = operation.elementId + ) + } ) } ) fun roleAddParamToRole(roleAddParam: RoleAddParam): Role = Role().apply { name = roleAddParam.name - enable = if (roleAddParam.enable) 1 else 0 + enable = if (roleAddParam.enable == true) 1 else 0 powers = roleAddParam.powerIds?.map { Power().apply { id = it } } } diff --git a/src/main/kotlin/top/fatweb/api/converter/permission/UserConverter.kt b/src/main/kotlin/top/fatweb/api/converter/permission/UserConverter.kt index 4453481..f983c49 100644 --- a/src/main/kotlin/top/fatweb/api/converter/permission/UserConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/permission/UserConverter.kt @@ -49,7 +49,8 @@ object UserConverter { name = it.name, url = it.url, powerId = it.powerId, - parentId = it.parentId + parentId = it.parentId, + moduleId = it.moduleId ) }, elements = user.elements?.map { diff --git a/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt b/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt index 398a19a..afe00a1 100644 --- a/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt +++ b/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt @@ -5,6 +5,7 @@ import com.auth0.jwt.exceptions.SignatureVerificationException import com.auth0.jwt.exceptions.TokenExpiredException import org.slf4j.Logger import org.slf4j.LoggerFactory +import org.springframework.dao.DuplicateKeyException import org.springframework.http.converter.HttpMessageNotReadableException import org.springframework.jdbc.BadSqlGrammarException import org.springframework.security.authentication.BadCredentialsException @@ -76,6 +77,11 @@ class ExceptionHandler { ResponseResult.fail(ResponseCode.DATABASE_EXECUTE_ERROR, "Incorrect SQL syntax", null) } + is DuplicateKeyException -> { + logger.debug(e.localizedMessage, e) + ResponseResult.fail(ResponseCode.DATABASE_INSERT_FAILED, "Duplicate key", null) + } + else -> { logger.error(e.localizedMessage, e) ResponseResult.fail(ResponseCode.SYSTEM_ERROR, data = null) 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 ca80a38..0a0b674 100644 --- a/src/main/kotlin/top/fatweb/api/param/authentication/RoleAddParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/authentication/RoleAddParam.kt @@ -5,11 +5,11 @@ import jakarta.validation.constraints.NotBlank data class RoleAddParam( @Schema(description = "角色名称") - @field:NotBlank - val name: String, + @field:NotBlank(message = "Name can not be blank") + val name: String?, @Schema(description = "启用") - val enable: Boolean = true, + val enable: Boolean? = true, @Schema(description = "权限 ID 列表") val powerIds: List? = null 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 a019bc6..16321e9 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt @@ -5,7 +5,6 @@ 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.RoleGetParam -import top.fatweb.api.vo.permission.RoleVo /** *

@@ -18,5 +17,5 @@ import top.fatweb.api.vo.permission.RoleVo interface IRoleService : IService { fun getPage(roleGetParam: RoleGetParam?): IPage - fun add(roleAddParam: RoleAddParam): RoleVo? + fun add(roleAddParam: RoleAddParam): Role? } 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 931d6d9..a6758d0 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 @@ -14,7 +14,6 @@ import top.fatweb.api.param.authentication.RoleGetParam import top.fatweb.api.service.permission.IPowerRoleService import top.fatweb.api.service.permission.IRoleService import top.fatweb.api.util.PageUtil -import top.fatweb.api.vo.permission.RoleVo /** *

@@ -38,11 +37,15 @@ class RoleServiceImpl( @Transactional - override fun add(roleAddParam: RoleAddParam): RoleVo? { + override fun add(roleAddParam: RoleAddParam): Role? { val role = RoleConverter.roleAddParamToRole(roleAddParam) if (baseMapper.insert(role) == 1) { + if (roleAddParam.powerIds.isNullOrEmpty()) { + return role + } + if (powerRoleService.saveBatch( - roleAddParam.powerIds?.map { + roleAddParam.powerIds.map { PowerRole().apply { roleId = role.id powerId = it @@ -50,7 +53,7 @@ class RoleServiceImpl( } ) ) { - return RoleConverter.roleToRoleVo(role) + return role } } diff --git a/src/main/kotlin/top/fatweb/api/vo/permission/MenuVo.kt b/src/main/kotlin/top/fatweb/api/vo/permission/MenuVo.kt index fad69af..32f61ef 100644 --- a/src/main/kotlin/top/fatweb/api/vo/permission/MenuVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/permission/MenuVo.kt @@ -16,5 +16,8 @@ data class MenuVo( val powerId: Long?, @Schema(description = "父 ID") - val parentId: Long? + val parentId: Long?, + + @Schema(description = "模块 ID") + val moduleId: Long? ) diff --git a/src/main/kotlin/top/fatweb/api/vo/permission/RoleWithPowerVo.kt b/src/main/kotlin/top/fatweb/api/vo/permission/RoleWithPowerVo.kt index da12b43..04f5bae 100644 --- a/src/main/kotlin/top/fatweb/api/vo/permission/RoleWithPowerVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/permission/RoleWithPowerVo.kt @@ -13,5 +13,17 @@ data class RoleWithPowerVo( val name: String?, @Schema(description = "启用", example = "true") - val enable: Boolean? + val enable: Boolean?, + + @Schema(description = "模块列表") + val modules: List?, + + @Schema(description = "菜单列表") + val menus: List?, + + @Schema(description = "页面元素列表") + val elements: List?, + + @Schema(description = "功能列表") + val operations: List? )