Skip to content
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 @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -74,77 +72,18 @@ public List<NodeGroupDto.Response> 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);
}
Comment on lines 74 to 79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

엔티티 변경사항이 저장되지 않습니다.

req.applyTo(entity)로 엔티티를 수정한 후 저장하지 않고 있습니다.

다음과 같이 수정하세요:

 public NodeGroupDto.Response update(String id, NodeGroupDto.UpdateRequest req) {
     TbNodeGroup entity = nodeGroupRepository.findByIdAndDeleted(id, "N")
             .orElseThrow(() -> new EntityNotFoundException("NodeGroup not found with id: " + id));
     req.applyTo(entity);
+    nodeGroupRepository.save(entity);
     return NodeGroupDto.Response.from(entity);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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);
}
public NodeGroupDto.Response update(String id, NodeGroupDto.UpdateRequest req) {
TbNodeGroup entity = nodeGroupRepository.findByIdAndDeleted(id, "N")
.orElseThrow(() -> new EntityNotFoundException("NodeGroup not found with id: " + id));
req.applyTo(entity);
nodeGroupRepository.save(entity);
return NodeGroupDto.Response.from(entity);
}
🤖 Prompt for AI Agents
In src/main/java/com/handongapp/cms/service/impl/NodeGroupServiceImpl.java
around lines 74 to 79, after modifying the entity with req.applyTo(entity), the
changes are not saved to the database. Fix this by calling
nodeGroupRepository.save(entity) after applying the updates to persist the
changes before returning the response.


@Override
@Transactional
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<TbNodeGroup> currentNodeGroupsInDb = nodeGroupRepository.findBySectionIdAndDeletedOrderByOrderAsc(sectionId, "N");

List<TbNodeGroup> 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;
}
Comment on lines 83 to 87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

소프트 삭제가 저장되지 않습니다.

엔티티의 deleted 플래그를 설정한 후 저장하지 않고 있습니다.

다음과 같이 수정하세요:

 public void deleteSoft(String id) {
     TbNodeGroup entity = nodeGroupRepository.findByIdAndDeleted(id, "N")
             .orElseThrow(() -> new EntityNotFoundException("NodeGroup not found with id: " + id));
     entity.setDeleted("Y");
+    nodeGroupRepository.save(entity);
 }
🤖 Prompt for AI Agents
In src/main/java/com/handongapp/cms/service/impl/NodeGroupServiceImpl.java
around lines 83 to 87, the soft delete method sets the deleted flag on the
entity but does not save the updated entity back to the repository. Fix this by
calling the save method on nodeGroupRepository with the modified entity after
setting the deleted flag to "Y" to persist the change.


@Override
Expand Down
75 changes: 7 additions & 68 deletions src/main/java/com/handongapp/cms/service/impl/NodeServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -70,80 +68,21 @@ public List<NodeDto.Response> 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
@Transactional
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<TbNode> currentNodesInDb = nodeRepository.findByNodeGroupIdAndDeletedOrderByOrderAsc(nodeGroupId, "N");

List<TbNode> 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;
}
Comment on lines 82 to 86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

소프트 삭제가 저장되지 않습니다.

엔티티의 deleted 플래그를 설정한 후 저장하지 않고 있습니다.

다음과 같이 수정하세요:

 public void deleteSoft(String nodeId) {
     TbNode entity = nodeRepository.findByIdAndDeleted(nodeId, "N")
             .orElseThrow(() -> new EntityNotFoundException("Node not found with id: " + nodeId));
     entity.setDeleted("Y");
+    nodeRepository.save(entity);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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<TbNode> currentNodesInDb = nodeRepository.findByNodeGroupIdAndDeletedOrderByOrderAsc(nodeGroupId, "N");
List<TbNode> 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;
}
public void deleteSoft(String nodeId) {
TbNode entity = nodeRepository.findByIdAndDeleted(nodeId, "N")
.orElseThrow(() -> new EntityNotFoundException("Node not found with id: " + nodeId));
entity.setDeleted("Y");
nodeRepository.save(entity);
}
🤖 Prompt for AI Agents
In src/main/java/com/handongapp/cms/service/impl/NodeServiceImpl.java around
lines 82 to 86, the soft delete method sets the deleted flag on the entity but
does not persist this change. To fix this, after setting the deleted flag to
"Y", call the save method on nodeRepository to save the updated entity.


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -48,90 +46,17 @@ public List<SectionDto.Response> 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);
}
Comment on lines 48 to 53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

엔티티 변경사항이 저장되지 않습니다.

req.applyTo(entity)로 엔티티를 수정한 후 저장하지 않고 있습니다.

다음과 같이 수정하세요:

 public SectionDto.Response update(String id, SectionDto.UpdateRequest req) {
     TbSection entity = sectionRepository.findByIdAndDeleted(id, "N")
             .orElseThrow(() -> new EntityNotFoundException("Section not found with id: " + id));
     req.applyTo(entity);
+    sectionRepository.save(entity);
     return SectionDto.Response.from(entity);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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);
}
public SectionDto.Response update(String id, SectionDto.UpdateRequest req) {
TbSection entity = sectionRepository.findByIdAndDeleted(id, "N")
.orElseThrow(() -> new EntityNotFoundException("Section not found with id: " + id));
req.applyTo(entity);
sectionRepository.save(entity);
return SectionDto.Response.from(entity);
}
🤖 Prompt for AI Agents
In src/main/java/com/handongapp/cms/service/impl/SectionServiceImpl.java around
lines 48 to 53, after modifying the entity with req.applyTo(entity), the changes
are not saved to the database. Fix this by calling
sectionRepository.save(entity) after applying the updates to persist the changes
before returning the response.


@Override
@Transactional
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<TbSection> currentSectionsInDb = sectionRepository.findByCourseIdAndDeletedOrderByOrderAsc(courseId, "N");

List<TbSection> 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;
}
Comment on lines 57 to 61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

소프트 삭제가 저장되지 않습니다.

엔티티의 deleted 플래그를 설정한 후 저장하지 않고 있습니다.

다음과 같이 수정하세요:

 public void deleteSoft(String id) {
     TbSection entity = sectionRepository.findByIdAndDeleted(id, "N")
             .orElseThrow(() -> new EntityNotFoundException("Section not found with id: " + id));
     entity.setDeleted("Y");
+    sectionRepository.save(entity);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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<TbSection> currentSectionsInDb = sectionRepository.findByCourseIdAndDeletedOrderByOrderAsc(courseId, "N");
List<TbSection> 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;
}
public void deleteSoft(String id) {
TbSection entity = sectionRepository.findByIdAndDeleted(id, "N")
.orElseThrow(() -> new EntityNotFoundException("Section not found with id: " + id));
entity.setDeleted("Y");
sectionRepository.save(entity);
}
🤖 Prompt for AI Agents
In src/main/java/com/handongapp/cms/service/impl/SectionServiceImpl.java around
lines 57 to 61, the method sets the deleted flag on the entity but does not save
the updated entity to the repository. To fix this, after setting
entity.setDeleted("Y"), call sectionRepository.save(entity) to persist the
change.

}