Skip to content

Commit

Permalink
style: 优化校验相关方法命名
Browse files Browse the repository at this point in the history
1.check 作为开头的方法名称,不设置返回值,对于 check 不通过的情况以异常的形式抛出(提升代码可读性,无需考虑是 check
通过了返回 true,还是 check 失败的情况下返回 true)
2.返回布尔值的方法,名称以 is 作为起始
  • Loading branch information
Charles7c committed Oct 25, 2023
1 parent 51f5528 commit f25de2d
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class LogInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
@NonNull Object handler) {
if (checkIsNeedRecord(handler, request)) {
if (this.isNeedRecord(handler, request)) {
// 记录时间
this.logCreateTime();
}
Expand Down Expand Up @@ -336,15 +336,15 @@ private String getResponseBody(HttpServletResponse response) {
}

/**
* 检查是否要记录系统日志
* 是否要记录系统日志
*
* @param handler
* 处理器
* @param request
* 请求对象
* @return true 需要记录false 不需要记录
* @return true 需要记录false 不需要记录
*/
private boolean checkIsNeedRecord(Object handler, HttpServletRequest request) {
private boolean isNeedRecord(Object handler, HttpServletRequest request) {
// 1、未启用时,不需要记录系统日志
if (!(handler instanceof HandlerMethod) || Boolean.FALSE.equals(operationLogProperties.getEnabled())) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public List<LoginUser> list(OnlineUserQuery query) {
}
// 检查是否符合查询条件
LoginUser loginUser = LoginHelper.getLoginUser(token);
if (this.checkQuery(query, loginUser)) {
if (this.isMatchQuery(query, loginUser)) {
loginUserList.add(loginUser);
}
}
Expand All @@ -95,15 +95,15 @@ public void cleanByRoleId(Long roleId) {
}

/**
* 检查是否符合查询条件
* 是否符合查询条件
*
* @param query
* 查询条件
* @param loginUser
* 登录用户信息
* @return 是否符合查询条件
*/
private boolean checkQuery(OnlineUserQuery query, LoginUser loginUser) {
private boolean isMatchQuery(OnlineUserQuery query, LoginUser loginUser) {
boolean flag1 = true;
String nickname = query.getNickname();
if (StrUtil.isNotBlank(nickname)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
@Transactional(rollbackFor = Exception.class)
public Long add(DeptRequest request) {
String name = request.getName();
boolean isExists = this.checkNameExists(name, request.getParentId(), null);
boolean isExists = this.isNameExists(name, request.getParentId(), null);
CheckUtils.throwIf(isExists, "新增失败,[{}] 已存在", name);
request.setAncestors(this.getAncestors(request.getParentId()));
request.setStatus(DisEnableStatusEnum.DISABLE);
Expand All @@ -76,7 +76,7 @@ public Long add(DeptRequest request) {
@Transactional(rollbackFor = Exception.class)
public void update(DeptRequest request, Long id) {
String name = request.getName();
boolean isExists = this.checkNameExists(name, request.getParentId(), id);
boolean isExists = this.isNameExists(name, request.getParentId(), id);
CheckUtils.throwIf(isExists, "修改失败,[{}] 已存在", name);
DeptDO oldDept = super.getById(id);
String oldName = oldDept.getName();
Expand Down Expand Up @@ -137,7 +137,7 @@ public void fillDetail(Object detailObj) {
}

/**
* 检查名称是否存在
* 名称是否存在
*
* @param name
* 名称
Expand All @@ -147,7 +147,7 @@ public void fillDetail(Object detailObj) {
* ID
* @return 是否存在
*/
private boolean checkNameExists(String name, Long parentId, Long id) {
private boolean isNameExists(String name, Long parentId, Long id) {
return baseMapper.lambdaQuery().eq(DeptDO::getName, name).eq(DeptDO::getParentId, parentId)
.ne(null != id, DeptDO::getId, id).exists();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class DictItemServiceImpl
@Transactional(rollbackFor = Exception.class)
public Long add(DictItemRequest request) {
String value = request.getValue();
CheckUtils.throwIf(this.checkValueExists(value, null, request.getDictId()), "新增失败,字典值 [{}] 已存在", value);
CheckUtils.throwIf(this.isValueExists(value, null, request.getDictId()), "新增失败,字典值 [{}] 已存在", value);
return super.add(request);
}

Expand All @@ -65,7 +65,7 @@ public Long add(DictItemRequest request) {
@Transactional(rollbackFor = Exception.class)
public void update(DictItemRequest request, Long id) {
String value = request.getValue();
CheckUtils.throwIf(this.checkValueExists(value, id, request.getDictId()), "修改失败,字典值 [{}] 已存在", value);
CheckUtils.throwIf(this.isValueExists(value, id, request.getDictId()), "修改失败,字典值 [{}] 已存在", value);
super.update(request, id);
}

Expand All @@ -92,7 +92,7 @@ public void deleteByDictIds(List<Long> dictIds) {
}

/**
* 检查字典值是否存在
* 字典值是否存在
*
* @param value
* 字典值
Expand All @@ -102,7 +102,7 @@ public void deleteByDictIds(List<Long> dictIds) {
* 字典 ID
* @return 是否存在
*/
private boolean checkValueExists(String value, Long id, Long dictId) {
private boolean isValueExists(String value, Long id, Long dictId) {
return baseMapper.lambdaQuery().eq(DictItemDO::getValue, value).eq(DictItemDO::getDictId, dictId)
.ne(null != id, DictItemDO::getId, id).exists();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ public class DictServiceImpl extends BaseServiceImpl<DictMapper, DictDO, DictVO,
@Transactional(rollbackFor = Exception.class)
public Long add(DictRequest request) {
String name = request.getName();
CheckUtils.throwIf(this.checkNameExists(name, null), "新增失败,[{}] 已存在", name);
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,[{}] 已存在", name);
String code = request.getCode();
CheckUtils.throwIf(this.checkCodeExists(code, null), "新增失败,[{}] 已存在", code);
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
return super.add(request);
}

@Override
@Transactional(rollbackFor = Exception.class)
public void update(DictRequest request, Long id) {
String name = request.getName();
CheckUtils.throwIf(this.checkNameExists(name, id), "修改失败,[{}] 已存在", name);
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);
String code = request.getCode();
CheckUtils.throwIf(this.checkCodeExists(code, id), "修改失败,[{}] 已存在", code);
CheckUtils.throwIf(this.isCodeExists(code, id), "修改失败,[{}] 已存在", code);
DictDO oldDict = super.getById(id);
if (oldDict.getIsSystem()) {
CheckUtils.throwIfNotEqual(request.getCode(), oldDict.getCode(), "[{}] 是系统内置字典,不允许修改字典编码",
Expand Down Expand Up @@ -105,28 +105,28 @@ public void export(DictQuery query, SortQuery sortQuery, HttpServletResponse res
}

/**
* 检查名称是否存在
* 名称是否存在
*
* @param name
* 名称
* @param id
* ID
* @return 是否存在
*/
private boolean checkNameExists(String name, Long id) {
private boolean isNameExists(String name, Long id) {
return baseMapper.lambdaQuery().eq(DictDO::getName, name).ne(null != id, DictDO::getId, id).exists();
}

/**
* 检查编码是否存在
* 编码是否存在
*
* @param code
* 编码
* @param id
* ID
* @return 是否存在
*/
private boolean checkCodeExists(String code, Long id) {
private boolean isCodeExists(String code, Long id) {
return baseMapper.lambdaQuery().eq(DictDO::getCode, code).ne(null != id, DictDO::getId, id).exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuVO,
@Transactional(rollbackFor = Exception.class)
public Long add(MenuRequest request) {
String title = request.getTitle();
boolean isExists = this.checkNameExists(title, request.getParentId(), null);
CheckUtils.throwIf(isExists, "新增失败,[{}] 已存在", title);
CheckUtils.throwIf(this.isNameExists(title, request.getParentId(), null), "新增失败,[{}] 已存在", title);
request.setStatus(DisEnableStatusEnum.ENABLE);
return super.add(request);
}
Expand All @@ -67,8 +66,7 @@ public Long add(MenuRequest request) {
@Transactional(rollbackFor = Exception.class)
public void update(MenuRequest request, Long id) {
String title = request.getTitle();
boolean isExists = this.checkNameExists(title, request.getParentId(), id);
CheckUtils.throwIf(isExists, "修改失败,[{}] 已存在", title);
CheckUtils.throwIf(this.isNameExists(title, request.getParentId(), id), "修改失败,[{}] 已存在", title);
super.update(request, id);
}

Expand Down Expand Up @@ -103,7 +101,7 @@ public List<MenuVO> list() {
}

/**
* 检查名称是否存在
* 名称是否存在
*
* @param name
* 名称
Expand All @@ -113,7 +111,7 @@ public List<MenuVO> list() {
* ID
* @return 是否存在
*/
private boolean checkNameExists(String name, Long parentId, Long id) {
private boolean isNameExists(String name, Long parentId, Long id) {
return baseMapper.lambdaQuery().eq(MenuDO::getTitle, name).eq(MenuDO::getParentId, parentId)
.ne(null != id, MenuDO::getId, id).exists();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
@Transactional(rollbackFor = Exception.class)
public Long add(RoleRequest request) {
String name = request.getName();
CheckUtils.throwIf(this.checkNameExists(name, null), "新增失败,[{}] 已存在", name);
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,[{}] 已存在", name);
String code = request.getCode();
CheckUtils.throwIf(this.checkCodeExists(code, null), "新增失败,[{}] 已存在", code);
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
// 新增信息
request.setStatus(DisEnableStatusEnum.ENABLE);
Long roleId = super.add(request);
Expand All @@ -86,9 +86,9 @@ public Long add(RoleRequest request) {
@Transactional(rollbackFor = Exception.class)
public void update(RoleRequest request, Long id) {
String name = request.getName();
CheckUtils.throwIf(this.checkNameExists(name, id), "修改失败,[{}] 已存在", name);
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);
String code = request.getCode();
CheckUtils.throwIf(this.checkCodeExists(code, id), "修改失败,[{}] 已存在", code);
CheckUtils.throwIf(this.isCodeExists(code, id), "修改失败,[{}] 已存在", code);
RoleDO oldRole = super.getById(id);
DataScopeEnum oldDataScope = oldRole.getDataScope();
String oldCode = oldRole.getCode();
Expand Down Expand Up @@ -184,28 +184,28 @@ public RoleDO getByCode(String code) {
}

/**
* 检查名称是否存在
* 名称是否存在
*
* @param name
* 名称
* @param id
* ID
* @return 是否存在
*/
private boolean checkNameExists(String name, Long id) {
private boolean isNameExists(String name, Long id) {
return baseMapper.lambdaQuery().eq(RoleDO::getName, name).ne(null != id, RoleDO::getId, id).exists();
}

/**
* 检查编码是否存在
* 编码是否存在
*
* @param code
* 编码
* @param id
* ID
* @return 是否存在
*/
private boolean checkCodeExists(String code, Long id) {
private boolean isCodeExists(String code, Long id) {
return baseMapper.lambdaQuery().eq(RoleDO::getCode, code).ne(null != id, RoleDO::getId, id).exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public Long save(UserDO user) {
@Transactional(rollbackFor = Exception.class)
public Long add(UserRequest request) {
String username = request.getUsername();
CheckUtils.throwIf(this.checkNameExists(username, null), "新增失败,[{}] 已存在", username);
CheckUtils.throwIf(this.isNameExists(username, null), "新增失败,[{}] 已存在", username);
String email = request.getEmail();
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.checkEmailExists(email, null), "新增失败,[{}] 已存在", email);
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), "新增失败,[{}] 已存在", email);
String phone = request.getPhone();
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.checkPhoneExists(phone, null), "新增失败,[{}] 已存在", phone);
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), "新增失败,[{}] 已存在", phone);
// 新增信息
request.setStatus(DisEnableStatusEnum.ENABLE);
Long userId = super.add(request);
Expand All @@ -111,11 +111,11 @@ public Long add(UserRequest request) {
@Transactional(rollbackFor = Exception.class)
public void update(UserRequest request, Long id) {
String username = request.getUsername();
CheckUtils.throwIf(this.checkNameExists(username, id), "修改失败,[{}] 已存在", username);
CheckUtils.throwIf(this.isNameExists(username, id), "修改失败,[{}] 已存在", username);
String email = request.getEmail();
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.checkEmailExists(email, id), "修改失败,[{}] 已存在", email);
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, id), "修改失败,[{}] 已存在", email);
String phone = request.getPhone();
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.checkPhoneExists(phone, id), "修改失败,[{}] 已存在", phone);
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, id), "修改失败,[{}] 已存在", phone);
DisEnableStatusEnum newStatus = request.getStatus();
CheckUtils.throwIf(
DisEnableStatusEnum.DISABLE.equals(newStatus) && ObjectUtil.equal(id, LoginHelper.getUserId()),
Expand Down Expand Up @@ -261,41 +261,41 @@ public String getNicknameById(Long id) {
}

/**
* 检查名称是否存在
* 名称是否存在
*
* @param name
* 名称
* @param id
* ID
* @return 是否存在
*/
private boolean checkNameExists(String name, Long id) {
private boolean isNameExists(String name, Long id) {
return baseMapper.lambdaQuery().eq(UserDO::getUsername, name).ne(null != id, UserDO::getId, id).exists();
}

/**
* 检查邮箱是否存在
* 邮箱是否存在
*
* @param email
* 邮箱
* @param id
* ID
* @return 是否存在
*/
private boolean checkEmailExists(String email, Long id) {
private boolean isEmailExists(String email, Long id) {
return baseMapper.lambdaQuery().eq(UserDO::getEmail, email).ne(null != id, UserDO::getId, id).exists();
}

/**
* 检查手机号码是否存在
* 手机号码是否存在
*
* @param phone
* 手机号码
* @param id
* ID
* @return 是否存在
*/
private boolean checkPhoneExists(String phone, Long id) {
private boolean isPhoneExists(String phone, Long id) {
return baseMapper.lambdaQuery().eq(UserDO::getPhone, phone).ne(null != id, UserDO::getId, id).exists();
}
}

0 comments on commit f25de2d

Please sign in to comment.