Skip to content

Commit

Permalink
[FIX] 삭제 시 시퀀스 조정 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
seokbeom00 committed Jan 14, 2025
1 parent 8363f1c commit 8dd3946
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ default Product findProductByIdOrThrow(Long id) {

@Query("SELECT COALESCE(MAX(p.sequence), 0) FROM Product p WHERE p.isTrial = :isTrial")
int findMaxSequenceByIsTrial(@Param("isTrial") boolean isTrial);

@Modifying
@Query("UPDATE Product p SET p.sequence = p.sequence - 1 " +
"WHERE p.isDeleted = false AND p.isTrial = :isTrial AND p.sequence > :deletedSequence")
void decrementSequenceAfterDeletion(@Param("isTrial") boolean isTrial, @Param("deletedSequence") int deletedSequence);
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,18 @@ public void delete(List<Long> productIds) {
if (products.isEmpty()) {
throw new CustomException(ErrorType.NOT_FOUND_PRODUCT_ERROR);
}
products.forEach(Product::deleteProduct);

products.forEach(product -> {
int deletedSequence = product.getSequence();
boolean isTrial = product.isTrial();
product.deleteProduct();
productRepository.decrementSequenceAfterDeletion(isTrial, deletedSequence);
});
}

@Transactional
public void updateProductSequence(Long productId, int currentSequence, int newSequence) {
Product product = productRepository.findById(productId)
.orElseThrow(() -> new IllegalArgumentException("Product not found with id: " + productId));
Product product = productRepository.findProductByIdOrThrow(productId);

if (currentSequence < newSequence) {
productRepository.decrementSequenceRange(currentSequence, newSequence);
Expand Down

0 comments on commit 8dd3946

Please sign in to comment.