feature/admin #7
|
|
@ -23,7 +23,7 @@ public class Role extends BaseEntity {
|
||||||
@Column(name = "description")
|
@Column(name = "description")
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "role", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "role", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
private Set<RoleMenu> roleMenus = new HashSet<>();
|
private Set<RoleMenu> roleMenus = new HashSet<>();
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
|
|
@ -43,4 +43,8 @@ public class Role extends BaseEntity {
|
||||||
public void addRoleMenu(RoleMenu roleMenu) {
|
public void addRoleMenu(RoleMenu roleMenu) {
|
||||||
this.roleMenus.add(roleMenu);
|
this.roleMenus.add(roleMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteRoleMenus() {
|
||||||
|
this.roleMenus.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,11 +15,11 @@ public class RoleMenu extends BaseEntity {
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "role_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
@JoinColumn(name = "role_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||||
private Role role;
|
private Role role;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "menu_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
@JoinColumn(name = "menu_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||||
private Menu menu;
|
private Menu menu;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,11 @@ public class AdministratorManagementService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(@NotNull Long id) {
|
public void delete(@NotNull Long id) {
|
||||||
|
Optional<Administrator> findAdministrator = repository.findById(id);
|
||||||
|
if (findAdministrator.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Not found administrator");
|
||||||
|
}
|
||||||
|
|
||||||
repository.deleteById(id);
|
repository.deleteById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,12 +77,15 @@ public class RoleRestController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 권한 내 메뉴 추가
|
* 권한 내 메뉴 추가
|
||||||
|
* DELETE -> INSERT
|
||||||
*/
|
*/
|
||||||
@PostMapping("/add/menu")
|
@PostMapping("/put/menu")
|
||||||
public ResponseEntity<?> addMenu(
|
public ResponseEntity<?> addMenu(
|
||||||
@RequestBody @Valid List<RoleAddMenu.Request> request,
|
@RequestBody @Valid List<RoleAddMenu.Request> request,
|
||||||
BindingResult bindingResult
|
BindingResult bindingResult
|
||||||
) {
|
) {
|
||||||
|
roleService.deleteRoleMenu(request.get(0).getRoleId());
|
||||||
|
|
||||||
Set<RoleAddMenuCommand> commands = request.stream()
|
Set<RoleAddMenuCommand> commands = request.stream()
|
||||||
.map(c -> RoleAddMenuCommand.builder()
|
.map(c -> RoleAddMenuCommand.builder()
|
||||||
.roleId(c.getRoleId())
|
.roleId(c.getRoleId())
|
||||||
|
|
@ -91,7 +94,7 @@ public class RoleRestController {
|
||||||
)
|
)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
roleService.addMenu(commands);
|
roleService.addRoleMenu(commands);
|
||||||
|
|
||||||
return ResponseEntity.ok(RoleAddMenu.Response.success());
|
return ResponseEntity.ok(RoleAddMenu.Response.success());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,10 +60,11 @@ public class RoleService {
|
||||||
throw new IllegalArgumentException("Role Not Found");
|
throw new IllegalArgumentException("Role Not Found");
|
||||||
}
|
}
|
||||||
|
|
||||||
roleRepository.deleteById(id);
|
Role role = findRole.get();
|
||||||
|
roleRepository.deleteById(role.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMenu(
|
public void addRoleMenu(
|
||||||
@NotNull @Valid Set<RoleAddMenuCommand> commands
|
@NotNull @Valid Set<RoleAddMenuCommand> commands
|
||||||
) {
|
) {
|
||||||
RoleAddMenuCommand command = commands.stream().findFirst().orElseThrow();
|
RoleAddMenuCommand command = commands.stream().findFirst().orElseThrow();
|
||||||
|
|
@ -87,4 +88,14 @@ public class RoleService {
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteRoleMenu(@NotNull Long roleId) {
|
||||||
|
Optional<Role> findRole = roleRepository.findById(roleId);
|
||||||
|
if (findRole.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Role Not Found");
|
||||||
|
}
|
||||||
|
|
||||||
|
Role role = findRole.get();
|
||||||
|
role.deleteRoleMenus();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ 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.MenuGroupRepository;
|
||||||
import com.bpgroup.poc.admin.domain.entity.menu.MenuRepository;
|
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.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.RoleMenuRepository;
|
||||||
import com.bpgroup.poc.admin.domain.entity.role.RoleRepository;
|
import com.bpgroup.poc.admin.domain.entity.role.RoleRepository;
|
||||||
import com.bpgroup.poc.admin.env.MariaDBTestEnv;
|
import com.bpgroup.poc.admin.env.MariaDBTestEnv;
|
||||||
|
|
@ -116,7 +117,7 @@ class RoleServiceTest extends MariaDBTestEnv {
|
||||||
flushAndClear();
|
flushAndClear();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
roleService.addMenu(
|
roleService.addRoleMenu(
|
||||||
Set.of(
|
Set.of(
|
||||||
RoleAddMenuCommand.builder()
|
RoleAddMenuCommand.builder()
|
||||||
.roleId(saveRole.getId())
|
.roleId(saveRole.getId())
|
||||||
|
|
@ -134,6 +135,79 @@ class RoleServiceTest extends MariaDBTestEnv {
|
||||||
assertThat(roleMenuRepository.findById(saveRole.getId())).isNotEmpty();
|
assertThat(roleMenuRepository.findById(saveRole.getId())).isNotEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Role Menu 삭제 테스트")
|
||||||
|
void deleteRoleMenuTest() {
|
||||||
|
// given
|
||||||
|
Role saveRole = createRole();
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
MenuGroup menuGroup = createMenuGroup();
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
Menu saveMenu1 = createMenu(menuGroup);
|
||||||
|
Menu saveMenu2 = createMenu(menuGroup);
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
roleMenuRepository.save(
|
||||||
|
RoleMenu.builder()
|
||||||
|
.role(saveRole)
|
||||||
|
.menu(saveMenu1)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
|
roleMenuRepository.save(
|
||||||
|
RoleMenu.builder()
|
||||||
|
.role(saveRole)
|
||||||
|
.menu(saveMenu2)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
// when
|
||||||
|
roleService.deleteRoleMenu(saveRole.getId());
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(roleMenuRepository.findAll()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Role 삭제 시 Role Menu 동시 삭제 테스트")
|
||||||
|
void deleteRoleAndRoleMenuTest() {
|
||||||
|
// given
|
||||||
|
Role saveRole = createRole();
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
MenuGroup menuGroup = createMenuGroup();
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
Menu saveMenu1 = createMenu(menuGroup);
|
||||||
|
Menu saveMenu2 = createMenu(menuGroup);
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
roleMenuRepository.save(
|
||||||
|
RoleMenu.builder()
|
||||||
|
.role(saveRole)
|
||||||
|
.menu(saveMenu1)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
|
roleMenuRepository.save(
|
||||||
|
RoleMenu.builder()
|
||||||
|
.role(saveRole)
|
||||||
|
.menu(saveMenu2)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
|
// when
|
||||||
|
roleService.delete(saveRole.getId());
|
||||||
|
flushAndClear();
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(roleMenuRepository.findAll()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
private Role createRole() {
|
private Role createRole() {
|
||||||
return roleRepository.save(
|
return roleRepository.save(
|
||||||
Role.builder()
|
Role.builder()
|
||||||
|
|
@ -144,7 +218,7 @@ class RoleServiceTest extends MariaDBTestEnv {
|
||||||
}
|
}
|
||||||
|
|
||||||
private MenuGroup createMenuGroup() {
|
private MenuGroup createMenuGroup() {
|
||||||
return menuGroupRepository.save(
|
return menuGroupRepository.save(
|
||||||
MenuGroup.builder()
|
MenuGroup.builder()
|
||||||
.uri("TEST")
|
.uri("TEST")
|
||||||
.name("TEST")
|
.name("TEST")
|
||||||
|
|
@ -160,7 +234,7 @@ class RoleServiceTest extends MariaDBTestEnv {
|
||||||
.sortOrder(1)
|
.sortOrder(1)
|
||||||
.build();
|
.build();
|
||||||
menu.setMenuGroup(menuGroup);
|
menu.setMenuGroup(menuGroup);
|
||||||
return menuRepository.save(menu);
|
return menuRepository.save(menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -179,8 +253,11 @@ class RoleServiceTest extends MariaDBTestEnv {
|
||||||
assertThatThrownBy(() -> roleService.delete(null)).isInstanceOf(ConstraintViolationException.class);
|
assertThatThrownBy(() -> roleService.delete(null)).isInstanceOf(ConstraintViolationException.class);
|
||||||
|
|
||||||
// add Role Menu
|
// add Role Menu
|
||||||
assertThatThrownBy(() -> roleService.addMenu(Set.of(RoleAddMenuCommand.builder().roleId(null).menuId(null).build()))).isInstanceOf(ConstraintViolationException.class);
|
assertThatThrownBy(() -> roleService.addRoleMenu(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);
|
assertThatThrownBy(() -> roleService.addRoleMenu(Set.of(RoleAddMenuCommand.builder().roleId(1L).menuId(null).build()))).isInstanceOf(ConstraintViolationException.class);
|
||||||
|
|
||||||
|
// delete Role Menu
|
||||||
|
assertThatThrownBy(() -> roleService.deleteRoleMenu(null)).isInstanceOf(ConstraintViolationException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue