feat: implement authority deletion functionality and related repository methods
This commit is contained in:
@@ -4,6 +4,7 @@ import com.onixbyte.helix.domain.entity.Authority;
|
||||
import com.onixbyte.helix.domain.web.request.AddAuthorityRequest;
|
||||
import com.onixbyte.helix.domain.web.request.EditAuthorityRequest;
|
||||
import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
|
||||
import com.onixbyte.helix.domain.web.response.ActionResponse;
|
||||
import com.onixbyte.helix.service.AuthorityService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@@ -65,4 +66,10 @@ public class AuthorityController {
|
||||
public Authority editAuthority(@Validated @RequestBody EditAuthorityRequest request) {
|
||||
return authorityService.editAuthority(request);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{authorityId:\\d+}")
|
||||
public ActionResponse deleteAuthority(@PathVariable Long authorityId) {
|
||||
var name = authorityService.deleteAuthority(authorityId);
|
||||
return ActionResponse.success("Authority [%s] deleted.".formatted(name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,4 +72,12 @@ public class AuthorityManager {
|
||||
|
||||
return authority;
|
||||
}
|
||||
|
||||
public String findAuthorityNameById(Long authorityId) {
|
||||
return authorityRepository.findAuthorityNameById(authorityId);
|
||||
}
|
||||
|
||||
public void deleteById(Long authorityId) {
|
||||
authorityRepository.deleteById(authorityId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.onixbyte.helix.manager;
|
||||
|
||||
import com.onixbyte.helix.mapper.RoleAuthorityMapper;
|
||||
import com.onixbyte.helix.repository.RoleAuthorityRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -10,13 +11,21 @@ public class RoleAuthorityManager {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(RoleAuthorityManager.class);
|
||||
private final RoleAuthorityRepository roleAuthorityRepository;
|
||||
private final RoleAuthorityMapper roleAuthorityMapper;
|
||||
|
||||
public RoleAuthorityManager(RoleAuthorityRepository roleAuthorityRepository) {
|
||||
public RoleAuthorityManager(RoleAuthorityRepository roleAuthorityRepository, RoleAuthorityMapper roleAuthorityMapper) {
|
||||
this.roleAuthorityRepository = roleAuthorityRepository;
|
||||
this.roleAuthorityMapper = roleAuthorityMapper;
|
||||
}
|
||||
|
||||
public void deleteByRoleId(Long roleId) {
|
||||
var affectedRows = roleAuthorityRepository.deleteByRoleId(roleId);
|
||||
log.info("角色 {} 关联的权限绑定已全部移除(共 {} 条)", roleId, affectedRows);
|
||||
var affectedRows = roleAuthorityMapper.deleteByRoleId(roleId);
|
||||
log.info("A total of {} authorities linked to Role ID: {} have been successfully cleared.",
|
||||
affectedRows, roleId);
|
||||
}
|
||||
|
||||
public void deleteByAuthorityId(Long authorityId) {
|
||||
var affectedRows = roleAuthorityMapper.deleteByAuthorityId(authorityId);
|
||||
log.info("The binding between {} authorities and the role has been cleared.", affectedRows);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,6 @@ import org.apache.ibatis.annotations.Param;
|
||||
public interface RoleAuthorityMapper {
|
||||
|
||||
int deleteByRoleId(@Param("roleId") Long roleId);
|
||||
|
||||
int deleteByAuthorityId(@Param("authorityId") Long authorityId);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,17 @@ package com.onixbyte.helix.repository;
|
||||
|
||||
import com.onixbyte.helix.domain.entity.Authority;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AuthorityRepository extends JpaRepository<Authority, Long> {
|
||||
|
||||
@Query("""
|
||||
select a.name
|
||||
from Authority a
|
||||
where a.id = :authorityId
|
||||
""")
|
||||
String findAuthorityNameById(Long authorityId);
|
||||
}
|
||||
|
||||
@@ -8,10 +8,13 @@ import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
|
||||
import com.onixbyte.helix.enumeration.Status;
|
||||
import com.onixbyte.helix.exception.BizException;
|
||||
import com.onixbyte.helix.manager.AuthorityManager;
|
||||
import com.onixbyte.helix.manager.RoleAuthorityManager;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -19,9 +22,11 @@ import java.util.Optional;
|
||||
public class AuthorityService {
|
||||
|
||||
private final AuthorityManager authorityManager;
|
||||
private final RoleAuthorityManager roleAuthorityManager;
|
||||
|
||||
public AuthorityService(AuthorityManager authorityManager) {
|
||||
public AuthorityService(AuthorityManager authorityManager, RoleAuthorityManager roleAuthorityManager) {
|
||||
this.authorityManager = authorityManager;
|
||||
this.roleAuthorityManager = roleAuthorityManager;
|
||||
}
|
||||
|
||||
public Page<Authority> getAuthorities(Pageable pageable, QueryAuthorityRequest request) {
|
||||
@@ -50,4 +55,18 @@ public class AuthorityService {
|
||||
public Authority editAuthority(EditAuthorityRequest request) {
|
||||
return authorityManager.update(request);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
public String deleteAuthority(Long authorityId) {
|
||||
var authorityName = authorityManager.findAuthorityNameById(authorityId);
|
||||
|
||||
if (StringUtils.isBlank(authorityName)) {
|
||||
throw new BizException(HttpStatus.NOT_FOUND, "Authority with ID '%d' not found.".formatted(authorityId));
|
||||
}
|
||||
|
||||
roleAuthorityManager.deleteByAuthorityId(authorityId);
|
||||
authorityManager.deleteById(authorityId);
|
||||
|
||||
return authorityName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user