admin: 권한 내 메뉴 추가 기능 및 테스트 코드 추가
This commit is contained in:
parent
49c2757e65
commit
aaf1537f06
|
|
@ -39,4 +39,8 @@ public class Role extends BaseEntity {
|
|||
this.name = updateRole.name;
|
||||
this.description = updateRole.description;
|
||||
}
|
||||
|
||||
public void addRoleMenu(RoleMenu roleMenu) {
|
||||
this.roleMenus.add(roleMenu);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.bpgroup.poc.admin.domain.entity.role;
|
|||
import com.bpgroup.poc.admin.domain.entity.BaseEntity;
|
||||
import com.bpgroup.poc.admin.domain.entity.menu.Menu;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
|
|
@ -22,4 +23,11 @@ public class RoleMenu extends BaseEntity {
|
|||
@JoinColumn(name = "menu_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
private Menu menu;
|
||||
|
||||
@Builder
|
||||
public static RoleMenu of(Role role, Menu menu) {
|
||||
RoleMenu roleMenu = new RoleMenu();
|
||||
roleMenu.role = role;
|
||||
roleMenu.menu = menu;
|
||||
return roleMenu;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
package com.bpgroup.poc.admin.domain.entity.role;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface RoleMenuRepository extends JpaRepository<RoleMenu, Long> {
|
||||
}
|
||||
|
|
@ -1,12 +1,10 @@
|
|||
package com.bpgroup.poc.admin.web.main.admin.role;
|
||||
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.reqres.RoleAddMenu;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.reqres.RoleCreate;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.reqres.RoleDelete;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.reqres.RoleUpdate;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.RoleCreateCommand;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.RoleCreateResult;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.RoleService;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.RoleUpdateCommand;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.*;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
|
@ -16,6 +14,10 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/admin/role")
|
||||
|
|
@ -73,4 +75,25 @@ public class RoleRestController {
|
|||
return ResponseEntity.ok(RoleDelete.Response.success());
|
||||
}
|
||||
|
||||
/**
|
||||
* 권한 내 메뉴 추가
|
||||
*/
|
||||
@PostMapping("/add/menu")
|
||||
public ResponseEntity<?> addMenu(
|
||||
@RequestBody @Valid List<RoleAddMenu.Request> request,
|
||||
BindingResult bindingResult
|
||||
) {
|
||||
Set<RoleAddMenuCommand> commands = request.stream()
|
||||
.map(c -> RoleAddMenuCommand.builder()
|
||||
.roleId(c.getRoleId())
|
||||
.menuId(c.getMenuId())
|
||||
.build()
|
||||
)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
roleService.addMenu(commands);
|
||||
|
||||
return ResponseEntity.ok(RoleAddMenu.Response.success());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
package com.bpgroup.poc.admin.web.main.admin.role.reqres;
|
||||
|
||||
import com.bpgroup.poc.admin.web.common.CommonResponse;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
public class RoleAddMenu {
|
||||
|
||||
@Data
|
||||
public static class Request {
|
||||
@NotNull
|
||||
private Long roleId;
|
||||
@NotNull
|
||||
private Long menuId;
|
||||
|
||||
}
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public static class Response extends CommonResponse {
|
||||
@Builder
|
||||
public static Response success() {
|
||||
Response response = new Response();
|
||||
response.resultCode = "0000";
|
||||
response.resultMessage = "Success";
|
||||
return response;
|
||||
}
|
||||
|
||||
@Builder
|
||||
public static Response fail(String resultMessage) {
|
||||
Response response = new Response();
|
||||
response.resultCode = "9999";
|
||||
response.resultMessage = resultMessage;
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.bpgroup.poc.admin.web.main.admin.role.service;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class RoleAddMenuCommand {
|
||||
@NotNull
|
||||
private Long roleId;
|
||||
@NotNull
|
||||
private Long menuId;
|
||||
|
||||
@Builder
|
||||
public static RoleAddMenuCommand of(Long roleId, Long menuId) {
|
||||
RoleAddMenuCommand command = new RoleAddMenuCommand();
|
||||
command.roleId = roleId;
|
||||
command.menuId = menuId;
|
||||
return command;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
package com.bpgroup.poc.admin.web.main.admin.role.service;
|
||||
|
||||
import com.bpgroup.poc.admin.domain.entity.menu.Menu;
|
||||
import com.bpgroup.poc.admin.domain.entity.menu.MenuRepository;
|
||||
import com.bpgroup.poc.admin.domain.entity.role.Role;
|
||||
import com.bpgroup.poc.admin.domain.entity.role.RoleMenu;
|
||||
import com.bpgroup.poc.admin.domain.entity.role.RoleMenuRepository;
|
||||
import com.bpgroup.poc.admin.domain.entity.role.RoleRepository;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.RoleQueryRepository;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
|
@ -11,6 +14,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
|
|
@ -18,8 +22,9 @@ import java.util.Optional;
|
|||
@Transactional
|
||||
public class RoleService {
|
||||
|
||||
private final RoleRepository repository;
|
||||
private final RoleQueryRepository queryRepository;
|
||||
private final RoleRepository roleRepository;
|
||||
private final MenuRepository menuRepository;
|
||||
private final RoleMenuRepository roleMenuRepository;
|
||||
|
||||
/**
|
||||
* ROLE 생성
|
||||
|
|
@ -27,7 +32,7 @@ public class RoleService {
|
|||
public RoleCreateResult create(
|
||||
@NotNull @Valid RoleCreateCommand command
|
||||
) {
|
||||
Role role = repository.save(command.toEntity());
|
||||
Role role = roleRepository.save(command.toEntity());
|
||||
|
||||
return RoleCreateResult.builder()
|
||||
.id(role.getId())
|
||||
|
|
@ -40,7 +45,7 @@ public class RoleService {
|
|||
public void update(
|
||||
@NotNull @Valid RoleUpdateCommand command
|
||||
) {
|
||||
Optional<Role> findRole = repository.findById(command.getId());
|
||||
Optional<Role> findRole = roleRepository.findById(command.getId());
|
||||
if (findRole.isEmpty()) {
|
||||
throw new IllegalArgumentException("Role Not Found");
|
||||
}
|
||||
|
|
@ -50,11 +55,36 @@ public class RoleService {
|
|||
}
|
||||
|
||||
public void delete(@NotNull Long id) {
|
||||
Optional<Role> findRole = repository.findById(id);
|
||||
Optional<Role> findRole = roleRepository.findById(id);
|
||||
if (findRole.isEmpty()) {
|
||||
throw new IllegalArgumentException("Role Not Found");
|
||||
}
|
||||
|
||||
repository.deleteById(id);
|
||||
roleRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public void addMenu(
|
||||
@NotNull @Valid Set<RoleAddMenuCommand> commands
|
||||
) {
|
||||
RoleAddMenuCommand command = commands.stream().findFirst().orElseThrow();
|
||||
Optional<Role> findRole = roleRepository.findById(command.getRoleId());
|
||||
if (findRole.isEmpty()) {
|
||||
throw new IllegalArgumentException("Role Not Found");
|
||||
}
|
||||
|
||||
Role role = findRole.get();
|
||||
commands.forEach(c -> {
|
||||
Optional<Menu> findMenu = menuRepository.findById(c.getMenuId());
|
||||
if (findMenu.isEmpty()) {
|
||||
throw new IllegalArgumentException("Menu Not Found");
|
||||
}
|
||||
|
||||
Menu menu = findMenu.get();
|
||||
RoleMenu roleMenu = RoleMenu.of(role, menu);
|
||||
role.addRoleMenu(roleMenu);
|
||||
|
||||
roleMenuRepository.save(roleMenu);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
package com.bpgroup.poc.admin.web.main.admin.role;
|
||||
|
||||
import com.bpgroup.poc.admin.domain.entity.menu.Menu;
|
||||
import com.bpgroup.poc.admin.domain.entity.menu.MenuGroup;
|
||||
import com.bpgroup.poc.admin.domain.entity.menu.MenuGroupRepository;
|
||||
import com.bpgroup.poc.admin.domain.entity.menu.MenuRepository;
|
||||
import com.bpgroup.poc.admin.domain.entity.role.Role;
|
||||
import com.bpgroup.poc.admin.domain.entity.role.RoleMenuRepository;
|
||||
import com.bpgroup.poc.admin.domain.entity.role.RoleRepository;
|
||||
import com.bpgroup.poc.admin.env.MariaDBTestEnv;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.RoleCreateCommand;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.RoleCreateResult;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.RoleService;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.RoleUpdateCommand;
|
||||
import com.bpgroup.poc.admin.web.main.admin.role.service.*;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
|
|
@ -14,6 +16,8 @@ import org.junit.jupiter.api.Test;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
|
|
@ -27,9 +31,21 @@ class RoleServiceTest extends MariaDBTestEnv {
|
|||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
@Autowired
|
||||
private MenuGroupRepository menuGroupRepository;
|
||||
|
||||
@Autowired
|
||||
private MenuRepository menuRepository;
|
||||
|
||||
@Autowired
|
||||
private RoleMenuRepository roleMenuRepository;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
roleRepository.deleteAll();
|
||||
menuRepository.deleteAll();
|
||||
menuGroupRepository.deleteAll();
|
||||
roleMenuRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -52,16 +68,10 @@ class RoleServiceTest extends MariaDBTestEnv {
|
|||
@DisplayName("Role 수정 테스트")
|
||||
void updateRoleTest() {
|
||||
// given
|
||||
RoleCreateCommand roleCreateCommand = RoleCreateCommand.builder()
|
||||
.name("TEST")
|
||||
.description("TEST")
|
||||
.build();
|
||||
|
||||
RoleCreateResult createResult = roleService.create(roleCreateCommand);
|
||||
flushAndClear();
|
||||
Long roleId = createRole();
|
||||
|
||||
RoleUpdateCommand roleUpdateCommand = RoleUpdateCommand.builder()
|
||||
.id(createResult.getId())
|
||||
.id(roleId)
|
||||
.name("TEST2")
|
||||
.description("TEST2")
|
||||
.build();
|
||||
|
|
@ -71,7 +81,7 @@ class RoleServiceTest extends MariaDBTestEnv {
|
|||
flushAndClear();
|
||||
|
||||
// then
|
||||
Role role = roleRepository.findById(createResult.getId()).orElseThrow();
|
||||
Role role = roleRepository.findById(roleId).orElseThrow();
|
||||
assertThat(role.getName()).isEqualTo("TEST2");
|
||||
assertThat(role.getDescription()).isEqualTo("TEST2");
|
||||
}
|
||||
|
|
@ -80,20 +90,79 @@ class RoleServiceTest extends MariaDBTestEnv {
|
|||
@DisplayName("Role 삭제 테스트")
|
||||
void deleteRoleTest() {
|
||||
// given
|
||||
RoleCreateCommand roleCreateCommand = RoleCreateCommand.builder()
|
||||
.name("TEST")
|
||||
.description("TEST")
|
||||
.build();
|
||||
|
||||
RoleCreateResult createResult = roleService.create(roleCreateCommand);
|
||||
flushAndClear();
|
||||
Long roleId = createRole();
|
||||
|
||||
// when
|
||||
roleService.delete(createResult.getId());
|
||||
roleService.delete(roleId);
|
||||
flushAndClear();
|
||||
|
||||
// then
|
||||
assertThat(roleRepository.findById(createResult.getId())).isEmpty();
|
||||
assertThat(roleRepository.findById(roleId)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Role Menu 추가 테스트")
|
||||
void addMenuTest() {
|
||||
// given
|
||||
Long roleId = createRole();
|
||||
MenuGroup menuGroup = createMenus();
|
||||
Menu menu1 = createMenu(menuGroup);
|
||||
Menu menu2 = createMenu(menuGroup);
|
||||
|
||||
Set<RoleAddMenuCommand> roleAddMenuCommands = Set.of(
|
||||
RoleAddMenuCommand.builder()
|
||||
.roleId(roleId)
|
||||
.menuId(menu1.getId())
|
||||
.build(),
|
||||
RoleAddMenuCommand.builder()
|
||||
.roleId(roleId)
|
||||
.menuId(menu2.getId())
|
||||
.build()
|
||||
);
|
||||
|
||||
// when
|
||||
roleService.addMenu(roleAddMenuCommands);
|
||||
flushAndClear();
|
||||
|
||||
// then
|
||||
assertThat(roleMenuRepository.findAll()).hasSize(2);
|
||||
}
|
||||
|
||||
private Long createRole() {
|
||||
Role role = roleRepository.save(
|
||||
Role.builder()
|
||||
.name("TEST")
|
||||
.description("TEST")
|
||||
.build()
|
||||
);
|
||||
flushAndClear();
|
||||
return role.getId();
|
||||
}
|
||||
|
||||
private MenuGroup createMenus() {
|
||||
MenuGroup menuGroup = menuGroupRepository.save(
|
||||
MenuGroup.builder()
|
||||
.uri("TEST")
|
||||
.name("TEST")
|
||||
.sortOrder(1)
|
||||
.build()
|
||||
);
|
||||
|
||||
flushAndClear();
|
||||
return menuGroup;
|
||||
}
|
||||
|
||||
private Menu createMenu(MenuGroup menuGroup) {
|
||||
Menu menu = Menu.builder()
|
||||
.uri("TEST")
|
||||
.name("TEST")
|
||||
.sortOrder(1)
|
||||
.build();
|
||||
menu.setMenuGroup(menuGroup);
|
||||
Menu saveMenu = menuRepository.save(menu);
|
||||
|
||||
flushAndClear();
|
||||
return saveMenu;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -110,6 +179,10 @@ class RoleServiceTest extends MariaDBTestEnv {
|
|||
|
||||
// delete
|
||||
assertThatThrownBy(() -> roleService.delete(null)).isInstanceOf(ConstraintViolationException.class);
|
||||
|
||||
// add Role Menu
|
||||
assertThatThrownBy(() -> roleService.addMenu(Set.of(RoleAddMenuCommand.builder().roleId(null).menuId(null).build()))).isInstanceOf(ConstraintViolationException.class);
|
||||
assertThatThrownBy(() -> roleService.addMenu(Set.of(RoleAddMenuCommand.builder().roleId(1L).menuId(null).build()))).isInstanceOf(ConstraintViolationException.class);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue