Skip to content

Commit

Permalink
refactor: validation 추가 및 메소드명 수정 (#65)
Browse files Browse the repository at this point in the history
* refactor: 수정 메소드 이름 변경

* refactor: 역할 사용 여부 validation 추가

* refactor: 역할 목록 전체 조회로 변경
  • Loading branch information
eunbc authored Dec 28, 2023
1 parent 7ce45ec commit afb6dde
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public ResponseEntity<ApiResponse<Long>> createRole(@RequestBody @Valid RoleCrea
}

@GetMapping
public ResponseEntity<ApiResponse<List<RoleGetResponse>>> getRolesByIds(@RequestParam List<Long> ids) {
List<RoleGetResponse> roles = roleService.getRolesByIds(ids);
public ResponseEntity<ApiResponse<List<RoleGetResponse>>> getAllPermissions() {
List<RoleGetResponse> roles = roleService.getAllRoles();
return ResponseEntity.ok(ApiResponse.ok(roles));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum CustomErrorCode {
NOT_AUTHORIZED("NOT AUTHORIZED", HttpStatus.UNAUTHORIZED, "접근 권한이 없습니다."),
ADMIN_ROLE_NOT_FOUND("ADMIN ROLE NOT FOUND", HttpStatus.NOT_FOUND, "존재하지 않는 관리자 역할입니다."),
DUPLICATED_ROLE("DUPLICATED ROLE", HttpStatus.BAD_REQUEST, "이미 존재하는 관리자 역할입니다."),
ROLE_IN_USE("ROLE IN USE", HttpStatus.BAD_REQUEST, "사용중인 역할입니다."),
ADMIN_PERMISSION_NOT_FOUND("ADMIN PERMISSION NOT FOUND", HttpStatus.NOT_FOUND, "존재하지 않는 관리자 권한입니다."),
DUPLICATED_PERMISSION("DUPLICATED PERMISSION", HttpStatus.BAD_REQUEST, "이미 존재하는 권한입니다"),
PERMISSION_ASSIGNED("PERMISSION ASSIGNED", HttpStatus.BAD_REQUEST, "이미 할당된 권한은 삭제할 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.pgms.coredomain.domain.member.Permission;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.RolePermission;
import com.pgms.coredomain.domain.member.repository.AdminRepository;
import com.pgms.coredomain.domain.member.repository.MemberRepository;
import com.pgms.coredomain.domain.member.repository.PermissionRepository;
import com.pgms.coredomain.domain.member.repository.RoleRepository;

Expand All @@ -26,6 +28,8 @@ public class RoleService {

private final RoleRepository roleRepository;
private final PermissionRepository permissionRepository;
private final AdminRepository adminRepository;
private final MemberRepository memberRepository;

public Long createRole(RoleCreateRequest request) {
validateRoleNameUnique(request.name());
Expand All @@ -34,8 +38,8 @@ public Long createRole(RoleCreateRequest request) {
}

@Transactional(readOnly = true)
public List<RoleGetResponse> getRolesByIds(List<Long> ids) {
return roleRepository.findAllById(ids).stream()
public List<RoleGetResponse> getAllRoles() {
return roleRepository.findAll().stream()
.map(RoleGetResponse::from)
.toList();
}
Expand All @@ -44,15 +48,24 @@ public void updateRole(Long id, RoleUpdateRequest request) {
Role role = roleRepository.findById(id)
.orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND));
validateRoleNameUnique(request.name(), role.getId());
role.changeName(request.name());
role.updateName(request.name());
}

public void deleteRole(Long id) {
Role role = roleRepository.findById(id)
.orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND));

if (isRoleInUse(id)) {
throw new AdminException(ROLE_IN_USE);
}

roleRepository.delete(role);
}

private boolean isRoleInUse(Long roleId) {
return adminRepository.existsByRoleId(roleId) || memberRepository.existsByRoleId(roleId);
}

private void validateRoleNameUnique(String name) {
roleRepository.findByName(name).ifPresent(r -> {
throw new AdminException(DUPLICATED_ROLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Role(String name) {
this.name = name;
}

public void changeName(String name) {
public void updateName(String name) {
this.name = name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface AdminRepository extends JpaRepository<Admin, Long> {
boolean existsByEmail(String email);

Slice<Admin> findSliceBy(Pageable pageable);

boolean existsByRoleId(Long roleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

public interface MemberRepository extends JpaRepository<Member, Long> {
Slice<Member> findSliceBy(Pageable pageable);

boolean existsByRoleId(Long roleId);
}

0 comments on commit afb6dde

Please sign in to comment.