feat: implement authority deletion functionality and related repository methods

This commit is contained in:
siujamo
2026-03-23 15:39:44 +08:00
parent aebb693ee7
commit 69e3f84bec
9 changed files with 66 additions and 6 deletions
@@ -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.AddAuthorityRequest;
import com.onixbyte.helix.domain.web.request.EditAuthorityRequest; import com.onixbyte.helix.domain.web.request.EditAuthorityRequest;
import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest; import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
import com.onixbyte.helix.domain.web.response.ActionResponse;
import com.onixbyte.helix.service.AuthorityService; import com.onixbyte.helix.service.AuthorityService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@@ -65,4 +66,10 @@ public class AuthorityController {
public Authority editAuthority(@Validated @RequestBody EditAuthorityRequest request) { public Authority editAuthority(@Validated @RequestBody EditAuthorityRequest request) {
return authorityService.editAuthority(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; 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; package com.onixbyte.helix.manager;
import com.onixbyte.helix.mapper.RoleAuthorityMapper;
import com.onixbyte.helix.repository.RoleAuthorityRepository; import com.onixbyte.helix.repository.RoleAuthorityRepository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -10,13 +11,21 @@ public class RoleAuthorityManager {
private static final Logger log = LoggerFactory.getLogger(RoleAuthorityManager.class); private static final Logger log = LoggerFactory.getLogger(RoleAuthorityManager.class);
private final RoleAuthorityRepository roleAuthorityRepository; private final RoleAuthorityRepository roleAuthorityRepository;
private final RoleAuthorityMapper roleAuthorityMapper;
public RoleAuthorityManager(RoleAuthorityRepository roleAuthorityRepository) { public RoleAuthorityManager(RoleAuthorityRepository roleAuthorityRepository, RoleAuthorityMapper roleAuthorityMapper) {
this.roleAuthorityRepository = roleAuthorityRepository; this.roleAuthorityRepository = roleAuthorityRepository;
this.roleAuthorityMapper = roleAuthorityMapper;
} }
public void deleteByRoleId(Long roleId) { public void deleteByRoleId(Long roleId) {
var affectedRows = roleAuthorityRepository.deleteByRoleId(roleId); var affectedRows = roleAuthorityMapper.deleteByRoleId(roleId);
log.info("角色 {} 关联的权限绑定已全部移除(共 {} 条)", roleId, affectedRows); 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 { public interface RoleAuthorityMapper {
int deleteByRoleId(@Param("roleId") Long roleId); 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 com.onixbyte.helix.domain.entity.Authority;
import org.springframework.data.jpa.repository.JpaRepository; 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; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface AuthorityRepository extends JpaRepository<Authority, Long> { 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.enumeration.Status;
import com.onixbyte.helix.exception.BizException; import com.onixbyte.helix.exception.BizException;
import com.onixbyte.helix.manager.AuthorityManager; 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.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional; import java.util.Optional;
@@ -19,9 +22,11 @@ import java.util.Optional;
public class AuthorityService { public class AuthorityService {
private final AuthorityManager authorityManager; private final AuthorityManager authorityManager;
private final RoleAuthorityManager roleAuthorityManager;
public AuthorityService(AuthorityManager authorityManager) { public AuthorityService(AuthorityManager authorityManager, RoleAuthorityManager roleAuthorityManager) {
this.authorityManager = authorityManager; this.authorityManager = authorityManager;
this.roleAuthorityManager = roleAuthorityManager;
} }
public Page<Authority> getAuthorities(Pageable pageable, QueryAuthorityRequest request) { public Page<Authority> getAuthorities(Pageable pageable, QueryAuthorityRequest request) {
@@ -50,4 +55,18 @@ public class AuthorityService {
public Authority editAuthority(EditAuthorityRequest request) { public Authority editAuthority(EditAuthorityRequest request) {
return authorityManager.update(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;
}
} }
@@ -2,7 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.onixbyte.helix.mapper.AuthorityMapper"> <mapper namespace="com.onixbyte.helix.mapper.AuthorityMapper">
<select id="selectByUserId" parameterType="long"> <select id="selectByUserId" parameterType="long" resultType="com.onixbyte.helix.domain.entity.Authority">
SELECT DISTINCT a.id, a.code, a.name, a.description, a.status, a.created_at, a.updated_at SELECT DISTINCT a.id, a.code, a.name, a.description, a.status, a.created_at, a.updated_at
FROM authority a FROM authority a
JOIN role_authority ra ON a.id = ra.authority_id JOIN role_authority ra ON a.id = ra.authority_id
@@ -7,4 +7,10 @@
FROM role_authority FROM role_authority
WHERE role_id = #{roleId} WHERE role_id = #{roleId}
</delete> </delete>
<delete id="deleteByAuthorityId" parameterType="long">
DELETE
FROM role_authority
WHERE authority_id = #{authorityId}
</delete>
</mapper> </mapper>
+1 -1
View File
@@ -52,7 +52,7 @@
</if> </if>
</select> </select>
<select id="count" parameterType="com.onixbyte.helix.domain.database.query.wrapper.QueryRoleWrapper"> <select id="count" parameterType="com.onixbyte.helix.domain.database.query.wrapper.QueryRoleWrapper" resultType="int">
SELECT COUNT(*) SELECT COUNT(*)
FROM role FROM role
<where> <where>