bugfix menu tree dnd update
This commit is contained in:
@@ -66,7 +66,7 @@ public class MenuApiController {
|
||||
*/
|
||||
@GetMapping("/api/v1/menus/{menuId}")
|
||||
public MenuResponseDto findById(@PathVariable Long menuId) {
|
||||
return menuService.findById(menuId);
|
||||
return menuService.findMenuResponseDtoById(menuId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.egovframe.cloud.portalservice.api.menu.dto;
|
||||
|
||||
import java.util.Comparator;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -52,6 +53,9 @@ public class MenuTreeResponseDto {
|
||||
this.level = entity.getLevel();
|
||||
this.children = entity.getChildren().stream()
|
||||
.map(children -> new MenuTreeResponseDto(children))
|
||||
.sorted(Comparator.comparing(MenuTreeResponseDto::getSortSeq))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -127,20 +127,9 @@ public class Menu extends BaseEntity {
|
||||
public Menu updateDnD(Menu parent, Integer sortSeq, Integer level) {
|
||||
this.sortSeq = sortSeq;
|
||||
this.level = level;
|
||||
|
||||
if (parent == null) {
|
||||
Menu oldParent = this.getParent();
|
||||
|
||||
if (oldParent == null) {
|
||||
return this;
|
||||
}
|
||||
|
||||
Menu old = oldParent.getChildren().stream().filter(item -> item.getId().equals(this.id)).findAny().orElse(null);
|
||||
if (old != null) {
|
||||
oldParent.getChildren().remove(old);
|
||||
}
|
||||
this.parent = null;
|
||||
|
||||
return this;
|
||||
return updateOldParent();
|
||||
}
|
||||
|
||||
if (parent.equals(this.parent)) {
|
||||
@@ -149,7 +138,21 @@ public class Menu extends BaseEntity {
|
||||
|
||||
this.parent = parent;
|
||||
parent.getChildren().add(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
private Menu updateOldParent() {
|
||||
Menu oldParent = this.getParent();
|
||||
|
||||
if (oldParent == null) {
|
||||
return this;
|
||||
}
|
||||
|
||||
Menu old = oldParent.getChildren().stream().filter(item -> item.getId().equals(this.id)).findAny().orElse(null);
|
||||
if (old != null) {
|
||||
oldParent.getChildren().remove(old);
|
||||
}
|
||||
this.parent = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ import java.util.List;
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
@Transactional
|
||||
@Service
|
||||
public class MenuService extends AbstractService {
|
||||
|
||||
@@ -48,6 +48,7 @@ public class MenuService extends AbstractService {
|
||||
* @param siteId
|
||||
* @return
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<MenuTreeResponseDto> findTreeBySiteId(Long siteId) {
|
||||
return menuRepository.findTreeBySiteId(siteId);
|
||||
}
|
||||
@@ -58,7 +59,8 @@ public class MenuService extends AbstractService {
|
||||
* @param menuId
|
||||
* @return
|
||||
*/
|
||||
public MenuResponseDto findById(Long menuId) {
|
||||
@Transactional(readOnly = true)
|
||||
public MenuResponseDto findMenuResponseDtoById(Long menuId) {
|
||||
return menuRepository.findByIdWithConnectName(menuId);
|
||||
}
|
||||
|
||||
@@ -68,7 +70,6 @@ public class MenuService extends AbstractService {
|
||||
* @param menuTreeRequestDto
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public MenuTreeResponseDto save(MenuTreeRequestDto menuTreeRequestDto) {
|
||||
Site site = siteRepository.findById(menuTreeRequestDto.getSiteId())
|
||||
.orElseThrow(() ->
|
||||
@@ -77,9 +78,7 @@ public class MenuService extends AbstractService {
|
||||
Menu parent = null;
|
||||
|
||||
if (menuTreeRequestDto.getParentId() != null) {
|
||||
parent = menuRepository.findById(menuTreeRequestDto.getParentId())
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= " + menuTreeRequestDto.getParentId()));
|
||||
parent = findById(menuTreeRequestDto.getParentId());
|
||||
}
|
||||
|
||||
Menu menu = menuRepository.save(Menu.builder()
|
||||
@@ -102,11 +101,8 @@ public class MenuService extends AbstractService {
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public MenuTreeResponseDto updateName(Long menuId, String name) throws EntityNotFoundException {
|
||||
Menu menu = menuRepository.findById(menuId)
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= " + menuId));
|
||||
Menu menu = findById(menuId);
|
||||
|
||||
menu.updateName(name);
|
||||
|
||||
@@ -121,11 +117,8 @@ public class MenuService extends AbstractService {
|
||||
* @param updateRequestDto
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public MenuResponseDto update(Long menuId, MenuUpdateRequestDto updateRequestDto) throws EntityNotFoundException, BusinessMessageException {
|
||||
Menu menu = menuRepository.findById(menuId)
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= " + menuId));
|
||||
Menu menu = findById(menuId);
|
||||
|
||||
//컨텐츠 or 게시판인 경우 connectId 필수
|
||||
if ("contents".equals(updateRequestDto.getMenuType()) || "board".equals(updateRequestDto.getMenuType())) {
|
||||
@@ -153,11 +146,8 @@ public class MenuService extends AbstractService {
|
||||
*
|
||||
* @param menuId
|
||||
*/
|
||||
@Transactional
|
||||
public void delete(Long menuId) {
|
||||
Menu menu = menuRepository.findById(menuId)
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= " + menuId));
|
||||
Menu menu = findById(menuId);
|
||||
menuRepository.delete(menu);
|
||||
}
|
||||
|
||||
@@ -170,10 +160,10 @@ public class MenuService extends AbstractService {
|
||||
* @param level
|
||||
*/
|
||||
private void recursive(MenuDnDRequestDto dto, Menu parent, Integer sortSeq, Integer level) {
|
||||
Menu menu = menuRepository.findById(dto.getMenuId())
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= " + dto.getMenuId()));
|
||||
Menu menu = findById(dto.getMenuId());
|
||||
|
||||
menu.updateDnD(parent, sortSeq, level);
|
||||
|
||||
if (dto.getChildren() == null || dto.getChildren().size() <= 0) {
|
||||
return;
|
||||
}
|
||||
@@ -191,11 +181,21 @@ public class MenuService extends AbstractService {
|
||||
* @param menuDnDRequestDtoList
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public Long updateDnD(Long siteId, List<MenuDnDRequestDto> menuDnDRequestDtoList) {
|
||||
for (int i = 0; i < menuDnDRequestDtoList.size(); i++) {
|
||||
recursive(menuDnDRequestDtoList.get(i), null, i+1, 1);
|
||||
MenuDnDRequestDto requestDto = menuDnDRequestDtoList.get(i);
|
||||
Menu parent = null;
|
||||
if (requestDto.getParentId() != null) {
|
||||
parent = findById(requestDto.getParentId());
|
||||
}
|
||||
recursive(requestDto, parent, requestDto.getSortSeq(), requestDto.getLevel());
|
||||
}
|
||||
return siteId;
|
||||
}
|
||||
|
||||
private Menu findById(Long id) {
|
||||
return menuRepository.findById(id)
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= " + id));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user