Finish role add and get

This commit is contained in:
2023-11-09 23:28:43 +08:00
parent 5af0c8283e
commit d176cc684b
9 changed files with 83 additions and 25 deletions

View File

@@ -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<PageVo<RoleVo>> {
fun get(roleGetParam: RoleGetParam?): ResponseResult<PageVo<RoleWithPowerVo>> {
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<RoleVo> {
fun add(@Valid @RequestBody roleAddParam: RoleAddParam): ResponseResult<RoleVo> {
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) }
}
}

View File

@@ -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<Role>): PageVo<RoleVo> = PageVo(
fun rolePageToRoleWithPowerPageVo(rolePage: IPage<Role>): PageVo<RoleWithPowerVo> = 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 } }
}

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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<Long>? = null

View File

@@ -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
/**
* <p>
@@ -18,5 +17,5 @@ import top.fatweb.api.vo.permission.RoleVo
interface IRoleService : IService<Role> {
fun getPage(roleGetParam: RoleGetParam?): IPage<Role>
fun add(roleAddParam: RoleAddParam): RoleVo?
fun add(roleAddParam: RoleAddParam): Role?
}

View File

@@ -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
/**
* <p>
@@ -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
}
}

View File

@@ -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?
)

View File

@@ -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<ModuleVo>?,
@Schema(description = "菜单列表")
val menus: List<MenuVo>?,
@Schema(description = "页面元素列表")
val elements: List<ElementVo>?,
@Schema(description = "功能列表")
val operations: List<OperationVo>?
)