[ Server ] [ Server ] - 角色部分CRUD加测试代码

This commit is contained in:
zeaslity
2023-10-08 16:51:16 +08:00
parent 2594514a87
commit 8b6da2f9e8
10 changed files with 394 additions and 34 deletions

View File

@@ -7,7 +7,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.DataAccessException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
@@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.*;
/**
@@ -119,23 +118,28 @@ public class GlobalExceptionHandler {
* @param exception 数据库异常
* @return
*/
@ExceptionHandler(value = {SQLException.class, DuplicateKeyException.class})
@ExceptionHandler(value = {SQLException.class})
public R<String> sqlExceptionHandler(SQLException exception) {
R<String> vo = new R<>();
vo.setCode(ResultStat.PARAM_ERROR.getCode());
vo.setMsg(getLocaleMsg(ResultStat.PARAM_ERROR.getDescription()));
if (exception instanceof SQLIntegrityConstraintViolationException) {
vo.setData("Data already exsit ! 操作失败!");
} else {
vo.setData("数据库异常,操作失败!");
}
log.debug(
exception.getMessage(),
exception
);
vo.setData("数据库异常,操作失败!");
vo.setMsg(getLocaleMsg(ResultStat.PARAM_ERROR.getDescription()));
return vo;
}
@ExceptionHandler(value = {DataAccessException.class})
public R<String> sqlDataExceptionHandler(DataAccessException exception) {
R<String> vo = new R<>();
vo.setCode(ResultStat.PARAM_ERROR.getCode());
vo.setData("数据库操作错误!");
vo.setMsg(exception.getMessage());
return vo;
}

View File

@@ -0,0 +1,21 @@
package io.wdd.common.utils;
import net.datafaker.Faker;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Locale;
@Component
public class FakeDataUtils {
@Bean(name = "fakerInstance")
public Faker NewFakerInstance() {
Faker faker = new Faker(new Locale(
"zh",
"CN"
));
return faker;
}
}

View File

@@ -6,6 +6,7 @@ import io.wdd.server.beans.vo.ServerRoleVO;
import io.wdd.server.coreService.CoreRoleService;
import io.wdd.server.service.ServerRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -18,9 +19,6 @@ public class CoreRoleServiceImpl implements CoreRoleService {
@Resource
ServerRoleService serverRoleService;
@Resource
ServerRoleDTOMapper serverRoleDTOMapper;
@Override
public List<ServerRolePO> roleGetAll() {
return serverRoleService.list();
@@ -29,12 +27,29 @@ public class CoreRoleServiceImpl implements CoreRoleService {
@Override
public boolean roleCreate(ServerRoleVO serverRoleVO) {
ServerRolePO serverRolePO = serverRoleDTOMapper.voToPo(serverRoleVO);
if (!checkDataValid(serverRoleVO)) {
return false;
}
ServerRolePO serverRolePO = ServerRoleDTOMapper.INSTANCE.voToPo(serverRoleVO);
return serverRoleService.save(serverRolePO);
}
private boolean checkDataValid(ServerRoleVO serverRoleVO) {
if (StringUtils.isBlank(serverRoleVO.getRoleName())) {
return false;
}
return serverRoleVO.getRoleNum() < 128 && serverRoleVO.getRoleNum() >= 0;
}
@Override
public boolean roleUpdate(ServerRolePO serverRolePO) {
if (!checkDataValid(ServerRoleDTOMapper.INSTANCE.poToVo(serverRolePO))) {
return false;
}
return serverRoleService.saveOrUpdate(serverRolePO);
}