Skip to content

Commit 7d850b0

Browse files
committed
fix: 차단 기록 복구 로직 추가 (Unique Constraint 문제 해결)
- 이전에 차단했다가 해제한 사용자를 다시 차단할 때 기존 기록 복구 - BlockRepository.findByBlockerIdAndBlockedIdIncludingDeleted 추가 - BaseEntity.restore() 메서드 추가 (soft delete 복구)
1 parent b3059f4 commit 7d850b0

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

src/main/java/com/taba/block/repository/BlockRepository.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ public interface BlockRepository extends JpaRepository<Block, String> {
2121
List<Block> findByBlockerId(@Param("blockerId") String blockerId);
2222

2323
/**
24-
* 특정 차단 관계 조회
24+
* 특정 차단 관계 조회 (활성 상태만)
2525
*/
2626
@Query("SELECT b FROM Block b WHERE b.blocker.id = :blockerId AND b.blocked.id = :blockedId AND b.deletedAt IS NULL")
2727
Optional<Block> findByBlockerIdAndBlockedId(@Param("blockerId") String blockerId, @Param("blockedId") String blockedId);
2828

29+
/**
30+
* 특정 차단 관계 조회 (삭제된 것 포함)
31+
* - 이전에 차단했다가 해제한 기록도 포함
32+
*/
33+
@Query("SELECT b FROM Block b WHERE b.blocker.id = :blockerId AND b.blocked.id = :blockedId")
34+
Optional<Block> findByBlockerIdAndBlockedIdIncludingDeleted(@Param("blockerId") String blockerId, @Param("blockedId") String blockedId);
35+
2936
/**
3037
* 차단 관계 존재 여부 확인
3138
*/

src/main/java/com/taba/block/service/BlockService.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class BlockService {
3131

3232
/**
3333
* 사용자 차단
34-
* - 차단 관계 생성
34+
* - 차단 관계 생성 또는 복구
3535
* - 친구 관계가 있으면 삭제 (양방향)
3636
*/
3737
@Transactional
@@ -54,17 +54,27 @@ public void blockUser(String blockedUserId) {
5454
User blockedUser = userRepository.findActiveUserById(blockedUserId)
5555
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
5656

57-
// 이미 차단한 경우 확인
57+
// 이미 차단한 경우 확인 (활성 상태)
5858
if (blockRepository.existsByBlockerIdAndBlockedId(currentUserId, blockedUserId)) {
5959
throw new BusinessException(ErrorCode.INVALID_REQUEST);
6060
}
6161

62-
// 차단 관계 생성
63-
Block block = Block.builder()
64-
.blocker(currentUser)
65-
.blocked(blockedUser)
66-
.build();
67-
blockRepository.save(block);
62+
// 이전에 차단했다가 해제한 기록이 있는지 확인 (soft deleted)
63+
Block existingBlock = blockRepository.findByBlockerIdAndBlockedIdIncludingDeleted(currentUserId, blockedUserId)
64+
.orElse(null);
65+
66+
if (existingBlock != null) {
67+
// 기존 차단 기록 복구 (deletedAt을 null로 설정)
68+
existingBlock.restore();
69+
blockRepository.save(existingBlock);
70+
} else {
71+
// 새 차단 관계 생성
72+
Block block = Block.builder()
73+
.blocker(currentUser)
74+
.blocked(blockedUser)
75+
.build();
76+
blockRepository.save(block);
77+
}
6878

6979
// 친구 관계가 있으면 삭제 (양방향)
7080
List<Friendship> friendships = friendshipRepository.findByUserIdsList(currentUserId, blockedUserId);

src/main/java/com/taba/common/entity/BaseEntity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,12 @@ public void softDelete() {
3131
public boolean isDeleted() {
3232
return this.deletedAt != null;
3333
}
34+
35+
/**
36+
* Soft delete된 엔티티를 복구합니다.
37+
*/
38+
public void restore() {
39+
this.deletedAt = null;
40+
}
3441
}
3542

0 commit comments

Comments
 (0)