Add get role controller. Optimize code.

This commit is contained in:
2023-11-09 18:17:00 +08:00
parent 65ddc644fb
commit 5af0c8283e
29 changed files with 454 additions and 93 deletions

View File

@@ -0,0 +1,40 @@
package top.fatweb.api.controller.permission
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
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
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.permission.GroupVo
/**
* <p>
* 用户组表 前端控制器
* </p>
*
* @author FatttSnake
* @since 2023-11-09
*/
@Tag(name = "用户组管理", description = "用户组管理相关接口")
@RestController
@RequestMapping("/system/group")
class GroupController(
val groupService: IGroupService
) {
@Operation(summary = "获取用户组列表")
@GetMapping
fun get(@Valid groupGetParam: GroupGetParam?): ResponseResult<PageVo<GroupVo>> {
return ResponseResult.success(
ResponseCode.DATABASE_SELECT_SUCCESS, data = GroupConverter.groupPageToGroupPageVo(
groupService.getPage(groupGetParam)
)
)
}
}

View File

@@ -0,0 +1,47 @@
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 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.RoleGetParam
import top.fatweb.api.service.permission.IRoleService
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.permission.RoleVo
/**
* 角色表 前端控制器
*
* @author FatttSnake
* @since 2023-11-09
*/
@Tag(name = "角色管理", description = "角色管理相关接口")
@RestController
@RequestMapping("/system/role")
class RoleController(
private val roleService: IRoleService
) {
@Operation(summary = "获取角色列表")
@GetMapping
fun get(roleGetParam: RoleGetParam?): ResponseResult<PageVo<RoleVo>> {
return ResponseResult.success(
ResponseCode.DATABASE_SELECT_SUCCESS, data = RoleConverter.rolePageToRolePageVo(
roleService.getPage(roleGetParam)
)
)
}
@Operation(summary = "添加角色")
@PostMapping
fun add(roleAddParam: RoleAddParam): ResponseResult<RoleVo> {
return roleService.add(roleAddParam)
?.let { ResponseResult.success(ResponseCode.DATABASE_INSERT_SUCCESS, data = it) }
?: let { ResponseResult.fail(ResponseCode.DATABASE_INSERT_FAILED) }
}
}

View File

@@ -0,0 +1,22 @@
package top.fatweb.api.converter.permission
import com.baomidou.mybatisplus.core.metadata.IPage
import top.fatweb.api.entity.permission.Group
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.permission.GroupVo
object GroupConverter {
fun groupPageToGroupPageVo(groupPage: IPage<Group>): PageVo<GroupVo> = PageVo(
groupPage.total,
groupPage.pages,
groupPage.size,
groupPage.current,
groupPage.records.map {
GroupVo(
id = it.id,
name = it.name,
enable = it.enable == 1
)
}
)
}

View File

@@ -0,0 +1,36 @@
package top.fatweb.api.converter.permission
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.vo.PageVo
import top.fatweb.api.vo.permission.RoleVo
object RoleConverter {
fun rolePageToRolePageVo(rolePage: IPage<Role>): PageVo<RoleVo> = PageVo(
rolePage.total,
rolePage.pages,
rolePage.size,
rolePage.current,
rolePage.records.map {
RoleVo(
id = it.id,
name = it.name,
enable = it.enable == 1
)
}
)
fun roleAddParamToRole(roleAddParam: RoleAddParam): Role = Role().apply {
name = roleAddParam.name
enable = if (roleAddParam.enable) 1 else 0
powers = roleAddParam.powerIds?.map { Power().apply { id = it } }
}
fun roleToRoleVo(role: Role): RoleVo = RoleVo(
id = role.id,
name = role.name,
enable = role.enable == 1
)
}

View File

@@ -37,6 +37,9 @@ class Role : Serializable {
@Version
var version: Int? = null
@TableField(exist = false)
var modules: List<Module>? = null
@TableField(exist = false)
var menus: List<Menu>? = null
@@ -50,6 +53,6 @@ class Role : Serializable {
var powers: List<Power>? = null
override fun toString(): String {
return "Role(id=$id, name=$name, enable=$enable, deleted=$deleted, version=$version, menus=$menus, elements=$elements, operations=$operations, powers=$powers)"
return "Role(id=$id, name=$name, enable=$enable, deleted=$deleted, version=$version, modules=$modules, menus=$menus, elements=$elements, operations=$operations, powers=$powers)"
}
}

View File

@@ -1,6 +1,7 @@
package top.fatweb.api.mapper.permission
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.baomidou.mybatisplus.core.metadata.IPage
import org.apache.ibatis.annotations.Mapper
import top.fatweb.api.entity.permission.Group
@@ -13,4 +14,6 @@ import top.fatweb.api.entity.permission.Group
* @since 2023-10-25
*/
@Mapper
interface GroupMapper : BaseMapper<Group>
interface GroupMapper : BaseMapper<Group> {
fun selectPage(page: IPage<Group>): IPage<Group>
}

View File

@@ -1,6 +1,7 @@
package top.fatweb.api.mapper.permission
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.baomidou.mybatisplus.core.metadata.IPage
import org.apache.ibatis.annotations.Mapper
import top.fatweb.api.entity.permission.Role
@@ -13,4 +14,6 @@ import top.fatweb.api.entity.permission.Role
* @since 2023-10-25
*/
@Mapper
interface RoleMapper : BaseMapper<Role>
interface RoleMapper : BaseMapper<Role> {
fun selectPage(page: IPage<Role>): IPage<Role>
}

View File

@@ -3,7 +3,7 @@ package top.fatweb.api.param
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.Min
open class PageParam {
open class PageSortParam {
@Schema(description = "分页页码", example = "1", defaultValue = "1")
@field:Min(1, message = "Pagination page number must be a positive integer")
var currentPage: Long = 1
@@ -11,4 +11,10 @@ open class PageParam {
@Schema(description = "分页大小", example = "20", defaultValue = "20")
@field:Min(1, message = "The number of data per page must be a positive integer")
var pageSize: Long = 20
@Schema(description = "排序字段", example = "id")
val sortField: String? = null
@Schema(description = "排序方式", example = "desc", allowableValues = ["desc", "asc"])
val sortOrder: String? = null
}

View File

@@ -0,0 +1,12 @@
package top.fatweb.api.param.authentication
import io.swagger.v3.oas.annotations.media.Schema
import top.fatweb.api.param.PageSortParam
data class GroupGetParam(
@Schema(description = "查询用户组名称")
val searchName: String? = null,
@Schema(description = "查询使用正则表达式")
val searchRegex: Boolean = false,
) : PageSortParam()

View File

@@ -0,0 +1,16 @@
package top.fatweb.api.param.authentication
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotBlank
data class RoleAddParam(
@Schema(description = "角色名称")
@field:NotBlank
val name: String,
@Schema(description = "启用")
val enable: Boolean = true,
@Schema(description = "权限 ID 列表")
val powerIds: List<Long>? = null
)

View File

@@ -0,0 +1,12 @@
package top.fatweb.api.param.authentication
import io.swagger.v3.oas.annotations.media.Schema
import top.fatweb.api.param.PageSortParam
data class RoleGetParam(
@Schema(description = "查询角色名称")
val searchName: String? = null,
@Schema(description = "查询使用正则表达式")
val searchRegex: Boolean = false,
) : PageSortParam()

View File

@@ -2,17 +2,11 @@ package top.fatweb.api.param.system
import io.swagger.v3.oas.annotations.media.Schema
import org.springframework.format.annotation.DateTimeFormat
import top.fatweb.api.param.PageParam
import top.fatweb.api.param.PageSortParam
import java.time.LocalDateTime
@Schema(description = "获取系统日志请求参数")
data class SysLogGetParam(
@Schema(description = "排序字段", example = "id")
val sortField: String? = null,
@Schema(description = "排序方式", example = "desc", allowableValues = ["desc", "asc"])
val sortOrder: String? = null,
@Schema(description = "类型过滤(多个使用逗号分隔)", example = "INFO", allowableValues = ["INFO", "ERROR"])
val logType: String? = null,
@@ -36,4 +30,4 @@ data class SysLogGetParam(
@Schema(description = "查询结束时间")
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
val searchEndTime: LocalDateTime? = null
) : PageParam()
) : PageSortParam()

View File

@@ -1,7 +1,9 @@
package top.fatweb.api.service.permission
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.service.IService
import top.fatweb.api.entity.permission.Group
import top.fatweb.api.param.authentication.GroupGetParam
/**
* <p>
@@ -11,4 +13,6 @@ import top.fatweb.api.entity.permission.Group
* @author FatttSnake
* @since 2023-10-25
*/
interface IGroupService : IService<Group>
interface IGroupService : IService<Group> {
fun getPage(groupGetParam: GroupGetParam?): IPage<Group>
}

View File

@@ -1,7 +1,11 @@
package top.fatweb.api.service.permission
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.RoleGetParam
import top.fatweb.api.vo.permission.RoleVo
/**
* <p>
@@ -11,4 +15,8 @@ import top.fatweb.api.entity.permission.Role
* @author FatttSnake
* @since 2023-10-25
*/
interface IRoleService : IService<Role>
interface IRoleService : IService<Role> {
fun getPage(roleGetParam: RoleGetParam?): IPage<Role>
fun add(roleAddParam: RoleAddParam): RoleVo?
}

View File

@@ -1,10 +1,14 @@
package top.fatweb.api.service.permission.impl
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.springframework.stereotype.Service
import top.fatweb.api.entity.permission.Group
import top.fatweb.api.mapper.permission.GroupMapper
import top.fatweb.api.param.authentication.GroupGetParam
import top.fatweb.api.service.permission.IGroupService
import top.fatweb.api.util.PageUtil
/**
* <p>
@@ -15,4 +19,12 @@ import top.fatweb.api.service.permission.IGroupService
* @since 2023-10-25
*/
@Service
class GroupServiceImpl : ServiceImpl<GroupMapper, Group>(), IGroupService
class GroupServiceImpl : ServiceImpl<GroupMapper, Group>(), IGroupService {
override fun getPage(groupGetParam: GroupGetParam?): IPage<Group> {
val groupPage = Page<Group>(groupGetParam?.currentPage ?: 1, groupGetParam?.pageSize ?: 20)
PageUtil.setPageSort(groupGetParam, groupPage)
return baseMapper.selectPage(groupPage)
}
}

View File

@@ -1,10 +1,20 @@
package top.fatweb.api.service.permission.impl
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import top.fatweb.api.converter.permission.RoleConverter
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.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>
@@ -15,4 +25,35 @@ import top.fatweb.api.service.permission.IRoleService
* @since 2023-10-25
*/
@Service
class RoleServiceImpl : ServiceImpl<RoleMapper, Role>(), IRoleService
class RoleServiceImpl(
private val powerRoleService: IPowerRoleService
) : ServiceImpl<RoleMapper, Role>(), IRoleService {
override fun getPage(roleGetParam: RoleGetParam?): IPage<Role> {
val rolePage = Page<Role>(roleGetParam?.currentPage ?: 1, roleGetParam?.pageSize ?: 20)
PageUtil.setPageSort(roleGetParam, rolePage)
return baseMapper.selectPage(rolePage)
}
@Transactional
override fun add(roleAddParam: RoleAddParam): RoleVo? {
val role = RoleConverter.roleAddParamToRole(roleAddParam)
if (baseMapper.insert(role) == 1) {
if (powerRoleService.saveBatch(
roleAddParam.powerIds?.map {
PowerRole().apply {
roleId = role.id
powerId = it
}
}
)
) {
return RoleConverter.roleToRoleVo(role)
}
}
return null
}
}

View File

@@ -9,7 +9,7 @@ import top.fatweb.api.entity.system.SysLog
import top.fatweb.api.mapper.system.SysLogMapper
import top.fatweb.api.param.system.SysLogGetParam
import top.fatweb.api.service.system.ISysLogService
import top.fatweb.api.util.StrUtil
import top.fatweb.api.util.PageUtil
/**
* <p>
@@ -24,20 +24,7 @@ class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
override fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog> {
val sysLogPage = Page<SysLog>(sysLogGetParam?.currentPage ?: 1, sysLogGetParam?.pageSize ?: 20)
if (sysLogGetParam?.sortField == null && sysLogGetParam?.sortOrder == null) {
sysLogPage.addOrder(OrderItem.desc("start_time"))
} else {
sysLogPage.addOrder(
if (sysLogGetParam.sortOrder?.startsWith(
"desc", true
) == true
) OrderItem.desc(StrUtil.upperToUnderLetter(sysLogGetParam.sortField)) else OrderItem.asc(
StrUtil.upperToUnderLetter(
sysLogGetParam.sortField
)
)
)
}
PageUtil.setPageSort(sysLogGetParam, sysLogPage, OrderItem.desc("start_time"))
return baseMapper.selectPage(
sysLogPage,

View File

@@ -0,0 +1,24 @@
package top.fatweb.api.util
import com.baomidou.mybatisplus.core.metadata.OrderItem
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import top.fatweb.api.param.PageSortParam
object PageUtil {
fun <T: Page<*>> setPageSort(pageSortParam: PageSortParam?, page: T, defaultOrder: OrderItem? = null) {
if (pageSortParam?.sortField != null || pageSortParam?.sortOrder != null) {
page.addOrder(
if (pageSortParam.sortOrder?.startsWith(
"desc", true
) == true
) OrderItem.desc(StrUtil.upperToUnderLetter(pageSortParam.sortField)) else OrderItem.asc(
StrUtil.upperToUnderLetter(
pageSortParam.sortField
)
)
)
} else {
defaultOrder?.let { page.addOrder(defaultOrder) }
}
}
}

View File

@@ -0,0 +1,17 @@
package top.fatweb.api.vo.permission
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import io.swagger.v3.oas.annotations.media.Schema
@Schema(description = "角色返回参数")
data class RoleWithPowerVo(
@JsonSerialize(using = ToStringSerializer::class)
val id: Long?,
@Schema(description = "角色名", example = "Role")
val name: String?,
@Schema(description = "启用", example = "true")
val enable: Boolean?
)