Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Role 도메인 삭제, enum으로 변경 #146

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@
import com.pgms.coredomain.domain.event.repository.EventSeatRepository;
import com.pgms.coredomain.domain.event.repository.EventTimeRepository;
import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.enums.Provider;
import com.pgms.coredomain.domain.member.repository.MemberRepository;
import com.pgms.coredomain.domain.member.repository.RoleRepository;

@SpringBootTest
@Import(TestConfig.class)
Expand Down Expand Up @@ -86,20 +84,15 @@ class BookingServiceTest {
@Autowired
private MemberRepository memberRepository;

@Autowired
private RoleRepository roleRepository;

private Member member;

@BeforeEach
void setup() {
Role role = roleRepository.save(new Role("ROLE_USER"));
member = memberRepository.save(Member.builder()
.email("[email protected]")
.password("test1234!")
.name("홍길동")
.provider(Provider.KAKAO)
.role(role)
.phoneNumber("010-123-456")
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.net.URI;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -20,16 +19,8 @@
import com.pgms.apimember.dto.request.AdminCreateRequest;
import com.pgms.apimember.dto.request.AdminUpdateRequest;
import com.pgms.apimember.dto.request.PageCondition;
import com.pgms.apimember.dto.request.PermissionCreateRequest;
import com.pgms.apimember.dto.request.PermissionUpdateRequest;
import com.pgms.apimember.dto.request.RoleCreateRequest;
import com.pgms.apimember.dto.request.RoleUpdateRequest;
import com.pgms.apimember.dto.response.AdminGetResponse;
import com.pgms.apimember.dto.response.PermissionGetResponse;
import com.pgms.apimember.dto.response.RoleGetResponse;
import com.pgms.apimember.service.AdminService;
import com.pgms.apimember.service.PermissionService;
import com.pgms.apimember.service.RoleService;
import com.pgms.coredomain.response.ApiResponse;

import jakarta.validation.Valid;
Expand All @@ -42,8 +33,6 @@
public class AdminManagementController { // 슈퍼관리자 전용 컨트롤러

private final AdminService adminService;
private final RoleService roleService;
private final PermissionService permissionService;

/**
* 관리자 CRUD
Expand Down Expand Up @@ -77,84 +66,4 @@ public ResponseEntity<ApiResponse<Void>> deleteAdmins(@RequestParam List<Long> a
adminService.deleteAdmins(adminIds);
return ResponseEntity.noContent().build();
}

/**
* 역할 CRUD
*/

@PostMapping("/roles")
public ResponseEntity<ApiResponse<Long>> createRole(@RequestBody @Valid RoleCreateRequest request) {
final Long roleId = roleService.createRole(request);
final URI uri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(roleId)
.toUri();
return ResponseEntity.created(uri).body(ApiResponse.created(roleId));
}

@GetMapping("/roles")
public ResponseEntity<ApiResponse<List<RoleGetResponse>>> getAllRoles() {
List<RoleGetResponse> roles = roleService.getAllRoles();
return ResponseEntity.ok(ApiResponse.ok(roles));
}

@PatchMapping("/roles/{id}")
public ResponseEntity<Void> updateRole(@PathVariable Long id, @RequestBody @Valid RoleUpdateRequest request) {
roleService.updateRole(id, request);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/roles/{id}")
public ResponseEntity<Void> deleteRole(@PathVariable Long id) {
roleService.deleteRole(id);
return ResponseEntity.noContent().build();
}

@PostMapping("/roles/{roleId}/permissions")
public ResponseEntity<ApiResponse<Long>> addPermissionToRole(@PathVariable Long roleId,
@RequestParam Long permissionId) {
roleService.addPermissionToRole(roleId, permissionId);
return new ResponseEntity<>(ApiResponse.created(roleId), HttpStatus.CREATED);
}

@DeleteMapping("/roles/{roleId}/permissions")
public ResponseEntity<Void> removePermissionsFromRole(@PathVariable Long roleId, @RequestParam Long permissionId) {
roleService.removePermissionFromRole(roleId, permissionId);
return ResponseEntity.noContent().build();
}

/**
* 권한 CRUD
*/

@PostMapping("/permissions")
public ResponseEntity<ApiResponse<Long>> createPermission(@RequestBody @Valid PermissionCreateRequest request) {
final Long permissionId = permissionService.createPermission(request);
final URI uri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(permissionId)
.toUri();
return ResponseEntity.created(uri).body(ApiResponse.created(permissionId));
}

@GetMapping("/permissions")
public ResponseEntity<ApiResponse<List<PermissionGetResponse>>> getAllPermissions() {
List<PermissionGetResponse> permissions = permissionService.getAllPermissions();
return ResponseEntity.ok(ApiResponse.ok(permissions));
}

@PatchMapping("/permissions/{id}")
public ResponseEntity<Void> updatePermission(@PathVariable Long id,
@RequestBody @Valid PermissionUpdateRequest request) {
permissionService.updatePermission(id, request);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/permissions/{id}")
public ResponseEntity<Void> deletePermission(@PathVariable Long id) {
permissionService.deletePermission(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pgms.apimember.dto.request;

import com.pgms.coredomain.domain.member.Admin;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.enums.Role;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -29,7 +29,7 @@ public record AdminCreateRequest(
String email,

@NotBlank(message = "역할은 필수 항목입니다.")
String roleName
Role role
) {
public static Admin toEntity(AdminCreateRequest requestDto, String encodedPassword, Role role) {
return Admin.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pgms.apimember.dto.request;

import com.pgms.coredomain.domain.member.enums.AccountStatus;
import com.pgms.coredomain.domain.member.enums.Role;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
Expand All @@ -26,6 +27,6 @@ public record AdminUpdateRequest(
AccountStatus status,

@NotBlank(message = "역할을 입력해주세요.")
String roleName
Role role
) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.pgms.apimember.dto.request;

import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.enums.Gender;

import jakarta.validation.constraints.Email;
Expand Down Expand Up @@ -42,7 +41,7 @@ public record MemberSignUpRequest(
String zipCode
) {

public Member toEntity(String encodedPassword, Role role) {
public Member toEntity(String encodedPassword) {
return Member.builder()
.email(email)
.password(encodedPassword)
Expand All @@ -53,7 +52,6 @@ public Member toEntity(String encodedPassword, Role role) {
.streetAddress(streetAddress)
.detailAddress(detailAddress)
.zipCode(zipCode)
.role(role)
.build();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.time.LocalDateTime;

import com.pgms.coredomain.domain.member.Admin;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.enums.AccountStatus;
import com.pgms.coredomain.domain.member.enums.Role;

public record AdminGetResponse(
Long id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import java.time.LocalDateTime;

import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.enums.AccountStatus;
import com.pgms.coredomain.domain.member.enums.Gender;
import com.pgms.coredomain.domain.member.enums.Provider;
import com.pgms.coredomain.domain.member.enums.Role;

public record MemberDetailGetResponse(
Long id,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import com.pgms.apimember.dto.response.MemberSummaryGetResponse;
import com.pgms.apimember.exception.AdminException;
import com.pgms.coredomain.domain.member.Admin;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.repository.AdminRepository;
import com.pgms.coredomain.domain.member.repository.MemberRepository;
import com.pgms.coredomain.domain.member.repository.RoleRepository;

import lombok.RequiredArgsConstructor;

Expand All @@ -32,7 +30,6 @@ public class AdminService {

private final AdminRepository adminRepository;
private final MemberRepository memberRepository;
private final RoleRepository roleRepository;
private final PasswordEncoder passwordEncoder;

// 슈퍼 관리자 기능
Expand All @@ -45,7 +42,7 @@ public Long createAdmin(AdminCreateRequest requestDto) {
final Admin admin = AdminCreateRequest.toEntity(
requestDto,
passwordEncoder.encode(requestDto.password()),
getRole(requestDto.roleName())
requestDto.role()
);
return adminRepository.save(admin).getId();
}
Expand All @@ -59,15 +56,14 @@ public List<AdminGetResponse> getAdmins(PageCondition pageCondition) {
public void updateAdmin(Long adminId, AdminUpdateRequest requestDto) {
validatePasswordAndConfirm(requestDto.password(), requestDto.passwordConfirm());

final Admin admin = adminRepository.findByIdWithRole(adminId)
final Admin admin = adminRepository.findById(adminId)
.orElseThrow(() -> new AdminException(ADMIN_NOT_FOUND));

final Role role = getRole(requestDto.roleName());
admin.update(
requestDto.name(),
passwordEncoder.encode(requestDto.password()),
requestDto.phoneNumber(),
requestDto.status(), role
requestDto.status(), requestDto.role()
);
}

Expand All @@ -85,7 +81,7 @@ public List<MemberDetailGetResponse> getMemberDetails(List<Long> memberIds) {

@Transactional(readOnly = true)
public AdminGetResponse getAdmin(Long adminId) {
final Admin admin = adminRepository.findByIdWithRole(adminId)
final Admin admin = adminRepository.findById(adminId)
.orElseThrow(() -> new AdminException(ADMIN_NOT_FOUND));
return AdminGetResponse.from(admin);
}
Expand All @@ -108,10 +104,4 @@ private void validatePasswordAndConfirm(String password, String passwordConfirm)
throw new AdminException(PASSWORD_CONFIRM_NOT_MATCHED);
}
}

private Role getRole(String roleName) {
return roleRepository.findByName(roleName)
.orElseThrow(() -> new AdminException(ADMIN_ROLE_NOT_FOUND));
}

}
Loading