Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 增加角色code检测,防止新增已存在的code时直接返回数据库异常 #253

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@ default PageResult<SysRolePageVO> queryPage(PageParam pageParam, SysRoleQO qo) {
*/
List<SelectData<Void>> listSelectData();

/**
* 是否存在角色code
* @param roleCode 角色code
* @return boolean 是否存在
*/
default boolean existsRoleCode(String roleCode) {
LambdaQueryWrapperX<SysRole> wrapperX = new LambdaQueryWrapperX<>();
wrapperX.eq(SysRole::getCode, roleCode);
return this.selectCount(wrapperX) > 0L;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public interface SysRoleService extends ExtendService<SysRole> {
*/
List<SelectData<Void>> listSelectData();

/**
* 是否存在角色code
* @param roleCode 角色code
* @return boolean 是否存在
*/
boolean existsRoleCode(String roleCode);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.hccake.ballcat.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hccake.ballcat.common.core.exception.BusinessException;
import com.hccake.ballcat.common.model.result.BaseResultCode;
import com.hccake.ballcat.system.mapper.SysRoleMapper;
import com.hccake.ballcat.system.model.entity.SysRole;
import com.hccake.ballcat.system.model.qo.SysRoleQO;
Expand Down Expand Up @@ -65,4 +68,27 @@ public List<SelectData<Void>> listSelectData() {
return baseMapper.listSelectData();
}

/**
* 是否存在角色code
* @param roleCode 角色code
* @return boolean 是否存在
*/
@Override
public boolean existsRoleCode(String roleCode) {
return baseMapper.existsRoleCode(roleCode);
}

/**
* 新增角色
* @param sysRole 角色对象
* @return boolean 是否新增成功
*/
@Override
public boolean save(SysRole sysRole) {
if (existsRoleCode(sysRole.getCode())) {
throw new BusinessException(BaseResultCode.LOGIC_CHECK_ERROR, "角色标识已存在!");
}
return SqlHelper.retBool(getBaseMapper().insert(sysRole));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public R<SysRole> getById(@PathVariable("id") Integer id) {
@PreAuthorize("@per.hasPermission('system:role:add')")
@Operation(summary = "新增系统角色", description = "新增系统角色")
public R<Boolean> save(@Valid @RequestBody SysRole sysRole) {
return R.ok(sysRoleService.save(sysRole));
return sysRoleService.save(sysRole) ? R.ok() : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "新建角色失败");
}

/**
Expand Down