From d2d6433617a6eba8bd1d59317ce8e47dc016f830 Mon Sep 17 00:00:00 2001 From: Jungsub Ryoo Date: Fri, 6 Jun 2025 20:24:17 +0900 Subject: [PATCH] Revert "Feat/178 about ordering" --- .../service/impl/NodeGroupServiceImpl.java | 73 ++-------------- .../cms/service/impl/NodeServiceImpl.java | 75 ++-------------- .../cms/service/impl/SectionServiceImpl.java | 87 ++----------------- 3 files changed, 19 insertions(+), 216 deletions(-) diff --git a/src/main/java/com/handongapp/cms/service/impl/NodeGroupServiceImpl.java b/src/main/java/com/handongapp/cms/service/impl/NodeGroupServiceImpl.java index f86261cd..4c3f0247 100644 --- a/src/main/java/com/handongapp/cms/service/impl/NodeGroupServiceImpl.java +++ b/src/main/java/com/handongapp/cms/service/impl/NodeGroupServiceImpl.java @@ -15,11 +15,9 @@ import org.springframework.transaction.annotation.Transactional; import java.time.Duration; -import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import org.springframework.lang.Nullable; import com.handongapp.cms.mapper.NodeGroupMapper; import com.fasterxml.jackson.databind.ObjectMapper; @@ -49,9 +47,9 @@ public class NodeGroupServiceImpl implements NodeGroupService { @Override @Transactional public NodeGroupDto.Response create(NodeGroupDto.CreateRequest req) { - TbNodeGroup newNodeGroup = req.toEntity(); // Assumes toEntity sets sectionId and order from req - TbNodeGroup persistedNodeGroup = reorderAndPersistNodeGroups(newNodeGroup.getSectionId(), newNodeGroup, newNodeGroup.getOrder()); - return NodeGroupDto.Response.from(persistedNodeGroup); + TbNodeGroup entity = req.toEntity(); + TbNodeGroup savedNodeGroup = nodeGroupRepository.save(entity); + return NodeGroupDto.Response.from(savedNodeGroup); } @Override @@ -74,17 +72,10 @@ public List listBySection(String sectionId) { @Override @Transactional public NodeGroupDto.Response update(String id, NodeGroupDto.UpdateRequest req) { - TbNodeGroup entityToUpdate = nodeGroupRepository.findByIdAndDeleted(id, "N") + TbNodeGroup entity = nodeGroupRepository.findByIdAndDeleted(id, "N") .orElseThrow(() -> new EntityNotFoundException("NodeGroup not found with id: " + id)); - - String sectionId = entityToUpdate.getSectionId(); - - // Apply changes from DTO. Assumes req.applyTo updates entityToUpdate.order if req.getOrder() is not null. - req.applyTo(entityToUpdate); - - // entityToUpdate.getOrder() will be the requested new order if specified in DTO, or original order if not. - TbNodeGroup updatedEntity = reorderAndPersistNodeGroups(sectionId, entityToUpdate, entityToUpdate.getOrder()); - return NodeGroupDto.Response.from(updatedEntity); + req.applyTo(entity); + return NodeGroupDto.Response.from(entity); } @Override @@ -92,59 +83,7 @@ public NodeGroupDto.Response update(String id, NodeGroupDto.UpdateRequest req) { public void deleteSoft(String id) { TbNodeGroup entity = nodeGroupRepository.findByIdAndDeleted(id, "N") .orElseThrow(() -> new EntityNotFoundException("NodeGroup not found with id: " + id)); - - String sectionId = entity.getSectionId(); entity.setDeleted("Y"); - entity.setOrder(null); // Mark order as irrelevant for soft-deleted items - nodeGroupRepository.save(entity); // Persist the soft deletion - - // Reorder remaining active node groups - reorderAndPersistNodeGroups(sectionId, null, null); - } - - private TbNodeGroup reorderAndPersistNodeGroups(String sectionId, @Nullable TbNodeGroup targetNodeGroup, @Nullable Integer requestedOrderForTarget) { - List currentNodeGroupsInDb = nodeGroupRepository.findBySectionIdAndDeletedOrderByOrderAsc(sectionId, "N"); - - List nodeGroupsToProcess = new ArrayList<>(); - boolean isTargetNew = (targetNodeGroup != null && targetNodeGroup.getId() == null); - - for (TbNodeGroup ng : currentNodeGroupsInDb) { - if (targetNodeGroup != null && ng.getId() != null && ng.getId().equals(targetNodeGroup.getId()) && !isTargetNew) { - continue; - } - nodeGroupsToProcess.add(ng); - } - - TbNodeGroup nodeGroupToReturn = targetNodeGroup; - - if (targetNodeGroup != null) { - int insertionIndex; - Integer effectiveOrder = requestedOrderForTarget; - - if (effectiveOrder == null) { - if (!isTargetNew) { - effectiveOrder = targetNodeGroup.getOrder(); - } - } - - if (effectiveOrder == null) { - insertionIndex = nodeGroupsToProcess.size(); - } else { - insertionIndex = Math.max(0, Math.min(effectiveOrder, nodeGroupsToProcess.size())); - } - nodeGroupsToProcess.add(insertionIndex, targetNodeGroup); - } - - for (int i = 0; i < nodeGroupsToProcess.size(); i++) { - TbNodeGroup nodeGroup = nodeGroupsToProcess.get(i); - nodeGroup.setOrder(i); - } - - if (!nodeGroupsToProcess.isEmpty()) { - nodeGroupRepository.saveAll(nodeGroupsToProcess); - } - - return nodeGroupToReturn; } @Override diff --git a/src/main/java/com/handongapp/cms/service/impl/NodeServiceImpl.java b/src/main/java/com/handongapp/cms/service/impl/NodeServiceImpl.java index 563b371b..2274d0c4 100644 --- a/src/main/java/com/handongapp/cms/service/impl/NodeServiceImpl.java +++ b/src/main/java/com/handongapp/cms/service/impl/NodeServiceImpl.java @@ -23,11 +23,9 @@ import jakarta.persistence.EntityNotFoundException; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.springframework.lang.Nullable; @Slf4j @@ -45,9 +43,9 @@ public class NodeServiceImpl implements NodeService { @Transactional public NodeDto.Response create(NodeDto.CreateRequest req) { // NodeDataValidator.validate(req.getType(), req.getData()); - TbNode newNode = req.toEntity(); // Assumes toEntity sets nodeGroupId and order from req - TbNode persistedNode = reorderAndPersistNodes(newNode.getNodeGroupId(), newNode, newNode.getOrder()); - return NodeDto.Response.from(persistedNode); + TbNode entity = req.toEntity(); + TbNode savedNode = nodeRepository.save(entity); + return NodeDto.Response.from(savedNode); } @Override @@ -70,20 +68,13 @@ public List listByGroup(String nodeGroupId) { @Override @Transactional public NodeDto.Response update(String nodeId, NodeDto.UpdateRequest req) { - TbNode entityToUpdate = nodeRepository.findByIdAndDeleted(nodeId, "N") + TbNode entity = nodeRepository.findByIdAndDeleted(nodeId, "N") .orElseThrow(() -> new EntityNotFoundException("Node not found with id: " + nodeId)); - if (req.getData() != null) { -// NodeDataValidator.validate(entityToUpdate.getType(), req.getData()); +// NodeDataValidator.validate(entity.getType(), req.getData()); } - String nodeGroupId = entityToUpdate.getNodeGroupId(); - - // Apply changes from DTO. Assumes req.applyTo updates entityToUpdate.order if req.getOrder() is not null. - req.applyTo(entityToUpdate); - - // entityToUpdate.getOrder() will be the requested new order if specified in DTO, or original order if not. - TbNode updatedEntity = reorderAndPersistNodes(nodeGroupId, entityToUpdate, entityToUpdate.getOrder()); - return NodeDto.Response.from(updatedEntity); + req.applyTo(entity); + return NodeDto.Response.from(entity); } @Override @@ -91,59 +82,7 @@ public NodeDto.Response update(String nodeId, NodeDto.UpdateRequest req) { public void deleteSoft(String nodeId) { TbNode entity = nodeRepository.findByIdAndDeleted(nodeId, "N") .orElseThrow(() -> new EntityNotFoundException("Node not found with id: " + nodeId)); - - String nodeGroupId = entity.getNodeGroupId(); entity.setDeleted("Y"); - entity.setOrder(null); // Mark order as irrelevant for soft-deleted items - nodeRepository.save(entity); // Persist the soft deletion - - // Reorder remaining active nodes - reorderAndPersistNodes(nodeGroupId, null, null); - } - - private TbNode reorderAndPersistNodes(String nodeGroupId, @Nullable TbNode targetNode, @Nullable Integer requestedOrderForTarget) { - List currentNodesInDb = nodeRepository.findByNodeGroupIdAndDeletedOrderByOrderAsc(nodeGroupId, "N"); - - List nodesToProcess = new ArrayList<>(); - boolean isTargetNew = (targetNode != null && targetNode.getId() == null); - - for (TbNode n : currentNodesInDb) { - if (targetNode != null && n.getId() != null && n.getId().equals(targetNode.getId()) && !isTargetNew) { - continue; - } - nodesToProcess.add(n); - } - - TbNode nodeToReturn = targetNode; - - if (targetNode != null) { - int insertionIndex; - Integer effectiveOrder = requestedOrderForTarget; - - if (effectiveOrder == null) { - if (!isTargetNew) { - effectiveOrder = targetNode.getOrder(); - } - } - - if (effectiveOrder == null) { - insertionIndex = nodesToProcess.size(); - } else { - insertionIndex = Math.max(0, Math.min(effectiveOrder, nodesToProcess.size())); - } - nodesToProcess.add(insertionIndex, targetNode); - } - - for (int i = 0; i < nodesToProcess.size(); i++) { - TbNode node = nodesToProcess.get(i); - node.setOrder(i); - } - - if (!nodesToProcess.isEmpty()) { - nodeRepository.saveAll(nodesToProcess); - } - - return nodeToReturn; } /** diff --git a/src/main/java/com/handongapp/cms/service/impl/SectionServiceImpl.java b/src/main/java/com/handongapp/cms/service/impl/SectionServiceImpl.java index b5efc32d..d9009817 100644 --- a/src/main/java/com/handongapp/cms/service/impl/SectionServiceImpl.java +++ b/src/main/java/com/handongapp/cms/service/impl/SectionServiceImpl.java @@ -9,10 +9,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -import org.springframework.lang.Nullable; @Service @RequiredArgsConstructor @@ -23,9 +21,9 @@ public class SectionServiceImpl implements SectionService { @Override @Transactional public SectionDto.Response create(String courseId, SectionDto.CreateRequest req) { - TbSection newSection = req.toEntity(courseId); // Assumes toEntity sets order from req - TbSection persistedSection = reorderAndPersist(courseId, newSection, newSection.getOrder()); - return SectionDto.Response.from(persistedSection); + TbSection entity = req.toEntity(courseId); + TbSection savedSection = sectionRepository.save(entity); + return SectionDto.Response.from(savedSection); } @Override @@ -48,17 +46,10 @@ public List listByCourse(String courseId) { @Override @Transactional public SectionDto.Response update(String id, SectionDto.UpdateRequest req) { - TbSection entityToUpdate = sectionRepository.findByIdAndDeleted(id, "N") + TbSection entity = sectionRepository.findByIdAndDeleted(id, "N") .orElseThrow(() -> new EntityNotFoundException("Section not found with id: " + id)); - - String courseId = entityToUpdate.getCourseId(); - - // Apply changes from DTO. Assumes req.applyTo updates entityToUpdate.order if req.getOrder() is not null. - req.applyTo(entityToUpdate); - - // entityToUpdate.getOrder() will be the requested new order if specified in DTO, or original order if not. - TbSection updatedEntity = reorderAndPersist(courseId, entityToUpdate, entityToUpdate.getOrder()); - return SectionDto.Response.from(updatedEntity); + req.applyTo(entity); + return SectionDto.Response.from(entity); } @Override @@ -66,72 +57,6 @@ public SectionDto.Response update(String id, SectionDto.UpdateRequest req) { public void deleteSoft(String id) { TbSection entity = sectionRepository.findByIdAndDeleted(id, "N") .orElseThrow(() -> new EntityNotFoundException("Section not found with id: " + id)); - - String courseId = entity.getCourseId(); entity.setDeleted("Y"); - entity.setOrder(null); // Mark order as irrelevant for soft-deleted items - sectionRepository.save(entity); // Persist the soft deletion - - // Reorder remaining active sections - reorderAndPersist(courseId, null, null); - } - - private TbSection reorderAndPersist(String courseId, @Nullable TbSection targetSection, @Nullable Integer requestedOrderForTarget) { - List currentSectionsInDb = sectionRepository.findByCourseIdAndDeletedOrderByOrderAsc(courseId, "N"); - - List sectionsToProcess = new ArrayList<>(); - boolean isTargetNew = (targetSection != null && targetSection.getId() == null); - - // Populate sectionsToProcess with existing sections, excluding the targetSection if it's being updated - for (TbSection s : currentSectionsInDb) { - if (targetSection != null && s.getId() != null && s.getId().equals(targetSection.getId()) && !isTargetNew) { - // Skip the old version of targetSection if it's an update of an existing entity - continue; - } - sectionsToProcess.add(s); - } - - // If targetSection is provided (create or update), add it to the list at the correct position - // Keep a reference to return, as the instance in targetSection variable might be the one from DB - // or a new one. The one added to sectionsToProcess is what gets its ID populated if new. - TbSection sectionToReturn = targetSection; - - if (targetSection != null) { - int insertionIndex; - Integer effectiveOrder = requestedOrderForTarget; - - // If no order is specified in the request for an existing item, - // use its current order to maintain its relative position unless other items shift it. - if (effectiveOrder == null) { - if (!isTargetNew) { // Existing item, order not specified in update DTO - effectiveOrder = targetSection.getOrder(); // Use its current order for placement logic - } - // If still null (e.g., new item and DTO order was null), it will be appended. - } - - if (effectiveOrder == null) { - insertionIndex = sectionsToProcess.size(); // Append to the end - } else { - // Ensure insertionIndex is within the bounds of [0, sectionsToProcess.size()] - insertionIndex = Math.max(0, Math.min(effectiveOrder, sectionsToProcess.size())); - } - sectionsToProcess.add(insertionIndex, targetSection); - } - - // Re-assign sequential order values from 0 to the items in sectionsToProcess - for (int i = 0; i < sectionsToProcess.size(); i++) { - TbSection section = sectionsToProcess.get(i); - section.setOrder(i); - } - - if (!sectionsToProcess.isEmpty()) { - sectionRepository.saveAll(sectionsToProcess); // Use sectionsToProcess directly - } - - // If targetSection was new, its ID is populated by saveAll. - // The 'sectionToReturn' (which is the 'targetSection' object passed in or created) - // is the instance that was added to sectionsToProcess and subsequently saved. - // So it should have the ID if it was new. - return sectionToReturn; } }