fix: 统一使用BusinessException替换RuntimeException
- RoleService 改用 BusinessException - PermissionService 改用 BusinessException - ProjectService 改用 BusinessException - SpaceNodeService 改用 BusinessException - LoginService 添加 @Transactional - 扩展错误码:PERMISSION_002, SPACE_001, SPACE_002
This commit is contained in:
parent
53381e2670
commit
f022d298e6
|
|
@ -7,6 +7,7 @@ import com.ether.pms.common.BusinessException;
|
||||||
import com.ether.pms.common.ErrorCode;
|
import com.ether.pms.common.ErrorCode;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -20,6 +21,7 @@ public class LoginService {
|
||||||
private final PasswordService passwordService;
|
private final PasswordService passwordService;
|
||||||
private final LoginAttemptService loginAttemptService;
|
private final LoginAttemptService loginAttemptService;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public Map<String, Object> login(String username, String password, String ip) {
|
public Map<String, Object> login(String username, String password, String ip) {
|
||||||
if (loginAttemptService.isLockedOut(username)) {
|
if (loginAttemptService.isLockedOut(username)) {
|
||||||
throw new BusinessException(ErrorCode.AUTH_002);
|
throw new BusinessException(ErrorCode.AUTH_002);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package com.ether.pms.auth.service;
|
||||||
|
|
||||||
import com.ether.pms.auth.entity.Permission;
|
import com.ether.pms.auth.entity.Permission;
|
||||||
import com.ether.pms.auth.repository.PermissionRepository;
|
import com.ether.pms.auth.repository.PermissionRepository;
|
||||||
|
import com.ether.pms.common.BusinessException;
|
||||||
|
import com.ether.pms.common.ErrorCode;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
@ -21,7 +23,7 @@ public class PermissionService {
|
||||||
|
|
||||||
public Permission findById(UUID id) {
|
public Permission findById(UUID id) {
|
||||||
return permissionRepository.findById(id)
|
return permissionRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("权限不存在"));
|
.orElseThrow(() -> new BusinessException(ErrorCode.PERMISSION_002));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Permission> findByType(String type) {
|
public List<Permission> findByType(String type) {
|
||||||
|
|
@ -39,7 +41,7 @@ public class PermissionService {
|
||||||
@Transactional
|
@Transactional
|
||||||
public Permission create(Permission permission) {
|
public Permission create(Permission permission) {
|
||||||
if (permissionRepository.existsByCode(permission.getCode())) {
|
if (permissionRepository.existsByCode(permission.getCode())) {
|
||||||
throw new RuntimeException("权限编码已存在");
|
throw new BusinessException(ErrorCode.PERMISSION_001);
|
||||||
}
|
}
|
||||||
return permissionRepository.save(permission);
|
return permissionRepository.save(permission);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import com.ether.pms.auth.entity.Role;
|
||||||
import com.ether.pms.auth.entity.Permission;
|
import com.ether.pms.auth.entity.Permission;
|
||||||
import com.ether.pms.auth.repository.RoleRepository;
|
import com.ether.pms.auth.repository.RoleRepository;
|
||||||
import com.ether.pms.auth.repository.PermissionRepository;
|
import com.ether.pms.auth.repository.PermissionRepository;
|
||||||
|
import com.ether.pms.common.BusinessException;
|
||||||
|
import com.ether.pms.common.ErrorCode;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
@ -24,12 +26,12 @@ public class RoleService {
|
||||||
|
|
||||||
public Role findById(UUID id) {
|
public Role findById(UUID id) {
|
||||||
return roleRepository.findById(id)
|
return roleRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("角色不存在"));
|
.orElseThrow(() -> new BusinessException(ErrorCode.ROLE_002));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Role findByCode(String code) {
|
public Role findByCode(String code) {
|
||||||
return roleRepository.findByCode(code)
|
return roleRepository.findByCode(code)
|
||||||
.orElseThrow(() -> new RuntimeException("角色不存在"));
|
.orElseThrow(() -> new BusinessException(ErrorCode.ROLE_002));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Role> findByProjectId(String projectId) {
|
public List<Role> findByProjectId(String projectId) {
|
||||||
|
|
@ -39,7 +41,7 @@ public class RoleService {
|
||||||
@Transactional
|
@Transactional
|
||||||
public Role create(Role role) {
|
public Role create(Role role) {
|
||||||
if (roleRepository.existsByCode(role.getCode())) {
|
if (roleRepository.existsByCode(role.getCode())) {
|
||||||
throw new RuntimeException("角色编码已存在");
|
throw new BusinessException(ErrorCode.ROLE_001);
|
||||||
}
|
}
|
||||||
return roleRepository.save(role);
|
return roleRepository.save(role);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,14 @@ public enum ErrorCode {
|
||||||
ROLE_002(3002, "角色不存在"),
|
ROLE_002(3002, "角色不存在"),
|
||||||
|
|
||||||
PERMISSION_001(4001, "权限编码已存在"),
|
PERMISSION_001(4001, "权限编码已存在"),
|
||||||
|
PERMISSION_002(4002, "权限不存在"),
|
||||||
|
|
||||||
PROJECT_001(5001, "项目编码已存在"),
|
PROJECT_001(5001, "项目编码已存在"),
|
||||||
PROJECT_002(5002, "项目不存在"),
|
PROJECT_002(5002, "项目不存在"),
|
||||||
|
|
||||||
|
SPACE_001(6001, "空间节点编码已存在"),
|
||||||
|
SPACE_002(6002, "空间节点不存在"),
|
||||||
|
|
||||||
SYSTEM_ERROR(9999, "系统错误");
|
SYSTEM_ERROR(9999, "系统错误");
|
||||||
|
|
||||||
private final int code;
|
private final int code;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package com.ether.pms.mdm.service;
|
||||||
|
|
||||||
import com.ether.pms.mdm.entity.Project;
|
import com.ether.pms.mdm.entity.Project;
|
||||||
import com.ether.pms.mdm.repository.ProjectRepository;
|
import com.ether.pms.mdm.repository.ProjectRepository;
|
||||||
|
import com.ether.pms.common.BusinessException;
|
||||||
|
import com.ether.pms.common.ErrorCode;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
@ -21,18 +23,18 @@ public class ProjectService {
|
||||||
|
|
||||||
public Project findById(UUID id) {
|
public Project findById(UUID id) {
|
||||||
return projectRepository.findById(id)
|
return projectRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("项目不存在"));
|
.orElseThrow(() -> new BusinessException(ErrorCode.PROJECT_002));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Project findByCode(String code) {
|
public Project findByCode(String code) {
|
||||||
return projectRepository.findByCode(code)
|
return projectRepository.findByCode(code)
|
||||||
.orElseThrow(() -> new RuntimeException("项目不存在"));
|
.orElseThrow(() -> new BusinessException(ErrorCode.PROJECT_002));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Project create(Project project) {
|
public Project create(Project project) {
|
||||||
if (projectRepository.existsByCode(project.getCode())) {
|
if (projectRepository.existsByCode(project.getCode())) {
|
||||||
throw new RuntimeException("项目编码已存在");
|
throw new BusinessException(ErrorCode.PROJECT_001);
|
||||||
}
|
}
|
||||||
return projectRepository.save(project);
|
return projectRepository.save(project);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package com.ether.pms.mdm.service;
|
||||||
|
|
||||||
import com.ether.pms.mdm.entity.SpaceNode;
|
import com.ether.pms.mdm.entity.SpaceNode;
|
||||||
import com.ether.pms.mdm.repository.SpaceNodeRepository;
|
import com.ether.pms.mdm.repository.SpaceNodeRepository;
|
||||||
|
import com.ether.pms.common.BusinessException;
|
||||||
|
import com.ether.pms.common.ErrorCode;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
@ -21,7 +23,7 @@ public class SpaceNodeService {
|
||||||
|
|
||||||
public SpaceNode findById(UUID id) {
|
public SpaceNode findById(UUID id) {
|
||||||
return spaceNodeRepository.findById(id)
|
return spaceNodeRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("空间节点不存在"));
|
.orElseThrow(() -> new BusinessException(ErrorCode.SPACE_002));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SpaceNode> findByProject(String projectCode) {
|
public List<SpaceNode> findByProject(String projectCode) {
|
||||||
|
|
@ -39,7 +41,7 @@ public class SpaceNodeService {
|
||||||
@Transactional
|
@Transactional
|
||||||
public SpaceNode create(SpaceNode spaceNode) {
|
public SpaceNode create(SpaceNode spaceNode) {
|
||||||
if (spaceNodeRepository.existsByCode(spaceNode.getCode())) {
|
if (spaceNodeRepository.existsByCode(spaceNode.getCode())) {
|
||||||
throw new RuntimeException("空间节点编码已存在");
|
throw new BusinessException(ErrorCode.SPACE_001);
|
||||||
}
|
}
|
||||||
return spaceNodeRepository.save(spaceNode);
|
return spaceNodeRepository.save(spaceNode);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue