mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: appname and namespace duplicate check #1009
This commit is contained in:
parent
57627305fa
commit
508127426f
@ -49,6 +49,10 @@ public enum ErrorCodes {
|
||||
* 系统内部异常
|
||||
*/
|
||||
SYSTEM_UNKNOWN_ERROR("-500", "SYS_UNKNOWN_ERROR"),
|
||||
/**
|
||||
* 非法参数
|
||||
*/
|
||||
ILLEGAL_ARGS_ERROR("-501", "ILLEGAL_ARGS_ERROR"),
|
||||
|
||||
/**
|
||||
* OPENAPI 错误码号段 -10XX
|
||||
|
@ -0,0 +1,17 @@
|
||||
package tech.powerjob.common.exception;
|
||||
|
||||
|
||||
import tech.powerjob.common.enums.ErrorCodes;
|
||||
|
||||
/**
|
||||
* PowerJobExceptionLauncher
|
||||
*
|
||||
* @author tjq
|
||||
* @since 2024/11/22
|
||||
*/
|
||||
public class PowerJobExceptionLauncher {
|
||||
|
||||
public PowerJobExceptionLauncher(ErrorCodes errorCode, String message) {
|
||||
throw new PowerJobException(errorCode, message);
|
||||
}
|
||||
}
|
@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import tech.powerjob.common.enums.ErrorCodes;
|
||||
import tech.powerjob.common.exception.PowerJobExceptionLauncher;
|
||||
import tech.powerjob.common.response.ResultDTO;
|
||||
import tech.powerjob.common.serialize.JsonUtils;
|
||||
import tech.powerjob.common.utils.CommonUtils;
|
||||
@ -85,9 +87,14 @@ public class AppInfoController {
|
||||
|
||||
Long id = req.getId();
|
||||
if (id == null) {
|
||||
|
||||
// 前置校验,防止部分没加唯一索引的 DB 重复创建记录导致异常
|
||||
appInfoRepository.findByAppName(req.getAppName()).ifPresent(x -> new PowerJobExceptionLauncher(ErrorCodes.ILLEGAL_ARGS_ERROR, String.format("App[%s] already exists", req.getAppName())));
|
||||
|
||||
appInfoDO = new AppInfoDO();
|
||||
appInfoDO.setGmtCreate(new Date());
|
||||
appInfoDO.setCreator(LoginUserHolder.getUserId());
|
||||
|
||||
} else {
|
||||
appInfoDO = appInfoService.findById(id, false).orElseThrow(() -> new IllegalArgumentException("can't find appInfo by id:" + id));
|
||||
|
||||
|
@ -8,12 +8,14 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tech.powerjob.common.enums.ErrorCodes;
|
||||
import tech.powerjob.common.enums.SwitchableStatus;
|
||||
import tech.powerjob.common.exception.PowerJobException;
|
||||
import tech.powerjob.common.exception.PowerJobExceptionLauncher;
|
||||
import tech.powerjob.server.auth.LoginUserHolder;
|
||||
import tech.powerjob.server.auth.RoleScope;
|
||||
import tech.powerjob.server.auth.service.WebAuthService;
|
||||
import tech.powerjob.server.common.SJ;
|
||||
import tech.powerjob.common.enums.SwitchableStatus;
|
||||
import tech.powerjob.server.persistence.QueryConvertUtils;
|
||||
import tech.powerjob.server.persistence.remote.model.AppInfoDO;
|
||||
import tech.powerjob.server.persistence.remote.model.NamespaceDO;
|
||||
@ -57,6 +59,9 @@ public class NamespaceWebServiceImpl implements NamespaceWebService {
|
||||
boolean isCreate = id == null;
|
||||
|
||||
if (isCreate) {
|
||||
|
||||
namespaceRepository.findByCode(req.getCode()).ifPresent(x -> new PowerJobExceptionLauncher(ErrorCodes.ILLEGAL_ARGS_ERROR, String.format("namespace[%s] already exists", req.getCode())));
|
||||
|
||||
namespaceDO = new NamespaceDO();
|
||||
namespaceDO.setGmtCreate(new Date());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user