Skip to content

[REFACTOR] #176: 버블 삭제(soft,hard)시 관련 백링크엔티티도 삭제(soft,hard) #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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 @@ -26,4 +26,5 @@ public class BubbleBacklink {

@Column(name = "is_trashed", nullable = false) //휴지통에 있는 지(soft_delete)
private boolean isTrashed = false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;

public interface BubbleBacklinkRepository extends JpaRepository<BubbleBacklink, Long> {
List<BubbleBacklink> findByBubble_BubbleId(Long bubbleId);

List<BubbleBacklink> findByBacklinkBubble_BubbleId(Long bubbleId);

List<BubbleBacklink> findByBubble_BubbleId(Long bubbleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,88 @@ private Bubble updateExistingBubble(BubbleRequestDto.SyncDto request, Member mem
Bubble bubble = bubbleRepository.findByMemberAndLocalIdx(member, request.getLocalIdx())
.orElseThrow(() -> new GeneralException(ErrorStatus.BUBBLE_NOT_FOUND));

// 버블 삭제할 경우 해당 버블을 백링크로 가지고 버블의 백링크 삭제
if(request.isTrashed()){
List<BubbleBacklink> backlinksDelete = bubbleBacklinkRepository.findByBubble_BubbleId(bubble.getBubbleId());
List<BubbleBacklink> backlinksDeleteByBacklink = bubbleBacklinkRepository.findByBacklinkBubble_BubbleId(bubble.getBubbleId());
// 관련된 백링크들 가져오기
List<BubbleBacklink> backlinksFromBubble = bubbleBacklinkRepository.findByBubble_BubbleId(bubble.getBubbleId());
//이 버블을 백링크로 걸은 백링크목록
List<BubbleBacklink> backlinksToBubble = bubbleBacklinkRepository.findByBacklinkBubble_BubbleId(bubble.getBubbleId());

System.out.println("▶▶▶ 내가 다른 버블을 가리키는 백링크 목록 (A → B):");
for (BubbleBacklink backlink : backlinksFromBubble) {
System.out.println("A → B: 내 ID = " + backlink.getBubble().getBubbleId()
+ " → 대상 ID = " + backlink.getBacklinkBubble().getBubbleId());
}

System.out.println("▶▶▶ 나를 가리키는 백링크 목록 (B → A):");
for (BubbleBacklink backlink : backlinksToBubble) {
System.out.println("B → A: 상대 ID = " + backlink.getBubble().getBubbleId()
+ " → 나 ID = " + backlink.getBacklinkBubble().getBubbleId());
}

Set<BubbleBacklink> allBacklinks = new HashSet<>();
allBacklinks.addAll(backlinksFromBubble);
allBacklinks.addAll(backlinksToBubble);

Set<BubbleBacklink> allBacklinksToDelete = new HashSet<>();
allBacklinksToDelete.addAll(backlinksDelete);
allBacklinksToDelete.addAll(backlinksDeleteByBacklink);
// 하드 딜리트 처리
if (request.isTrashed() && request.isDeleted()) {
bubbleBacklinkRepository.deleteAll(allBacklinks);
// 버블도 실제 삭제할 거면 여기서 bubbleRepository.delete(bubble)도 가능
return bubble; // 더 이상 업데이트 필요 없음
}

bubbleBacklinkRepository.deleteAll(allBacklinksToDelete);
for (BubbleBacklink link : backlinksFromBubble) {
Bubble target = link.getBacklinkBubble();
if(target.isDeleted()||request.isDeleted()) {
bubbleBacklinkRepository.delete(link);
}
else{
boolean shouldBeTrashed = request.isTrashed() || target.isTrashed();
System.out.println(link.getId());
link.setTrashed(shouldBeTrashed);
}
}
for (BubbleBacklink link : backlinksToBubble) {
Bubble target = link.getBacklinkBubble();
if(request.isDeleted()||target.isDeleted()) {
bubbleBacklinkRepository.delete(link);
}
else {
boolean shouldBeTrashed = request.isTrashed() || link.getBubble().isTrashed() || target.isTrashed();
System.out.println(link.getId());
System.out.println(target.getLocalIdx());
System.out.println(request.isTrashed());
if(!request.isTrashed() && !link.getBubble().isTrashed()){
shouldBeTrashed = false;
}
System.out.println(shouldBeTrashed);
link.setTrashed(shouldBeTrashed);
}
}
bubbleBacklinkRepository.saveAll(backlinksFromBubble);
bubbleBacklinkRepository.saveAll(backlinksToBubble);


Set<BubbleLabel> bubbleLabels = labels.stream()
.map(label -> BubbleLabel.builder().bubble(bubble).label(label).build())
.collect(Collectors.toSet());
bubble.update(request.getTitle(), request.getContent(), request.getMainImageUrl(), bubbleLabels);

bubble.getBacklinks().clear();
Set<BubbleBacklink> newbacklinks = backlinks.stream()
.map(backlink -> BubbleBacklink.builder()


// 새로운 backlink만 추가 (중복 방지)
Set<Bubble> existingBacklinks = bubble.getBacklinks().stream()
.map(b -> b.getBacklinkBubble()) // 중복 체크용
.collect(Collectors.toSet());

for (Bubble backlink : backlinks) {
if (!existingBacklinks.contains(backlink)) {
BubbleBacklink newBacklink = BubbleBacklink.builder()
.bubble(bubble)
.backlinkBubble(backlink)
.build())
.collect(Collectors.toSet());
bubble.getBacklinks().addAll(newbacklinks);
.isTrashed(request.isTrashed())
.build();
bubble.getBacklinks().add(newBacklink);
}
}

bubble.setTrashed(request.isTrashed());
bubble.setUpdatedAt(request.getUpdatedAt());
Expand Down