Skip to content

Commit e8a08db

Browse files
authored
[fix] 삭제된 댓글이 조회되는 오류 해결 #230 (#231)
* Revert "chore: initData용 이미지 추가" This reverts commit ef30eef. * . * chore: 수정 사항 없음 * fix: 삭제되지 않은 댓글만 조회하는 기능 추가 * fix: Soft Delete 정책에 따른 댓글 조회 로직 수정 - 댓글 조회 시 논리적으로 삭제된 댓글(`CommentStatus.DELETED`)을 제외하도록 Service 로직을 수정
1 parent 3a23325 commit e8a08db

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package com.back.domain.post.comment.repository;
22

33
import com.back.domain.post.comment.entity.Comment;
4-
import java.util.List;
4+
import com.back.domain.post.comment.enums.CommentStatus;
55
import org.springframework.data.jpa.repository.JpaRepository;
66
import org.springframework.stereotype.Repository;
77

8+
import java.util.List;
9+
810
@Repository
911
public interface CommentRepository extends JpaRepository<Comment, Long> {
1012

1113
List<Comment> findTop10ByPostIdOrderByIdDesc(Long postId);
1214

1315
List<Comment> findTop10ByPostIdAndIdLessThanOrderByIdDesc(Long postId, Long lastId);
16+
17+
// DELETED가 아닌 댓글만 조회하는 메서드 추가
18+
List<Comment> findTop10ByPostIdAndStatusNotOrderByIdDesc(Long postId, CommentStatus status);
19+
20+
List<Comment> findTop10ByPostIdAndStatusNotAndIdLessThanOrderByIdDesc(Long postId, CommentStatus status, Long lastId);
1421
}

src/main/java/com/back/domain/post/comment/service/CommentService.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ public CommentResponseDto createComment(Long postId, CommentCreateRequestDto req
6868
@Transactional(readOnly = true)
6969
public List<CommentResponseDto> getComments(Long postId, Long lastId) {
7070
if (lastId == null) {
71-
return commentRepository.findTop10ByPostIdOrderByIdDesc(postId)
72-
.stream()
73-
.map(CommentResponseDto::new)
74-
.toList();
71+
return commentRepository.findTop10ByPostIdAndStatusNotOrderByIdDesc(postId, CommentStatus.DELETED)
72+
.stream()
73+
.map(CommentResponseDto::new)
74+
.toList();
7575
} else {
76-
return commentRepository.findTop10ByPostIdAndIdLessThanOrderByIdDesc(postId, lastId)
77-
.stream()
78-
.map(CommentResponseDto::new)
79-
.toList();
76+
return commentRepository.findTop10ByPostIdAndStatusNotAndIdLessThanOrderByIdDesc(postId, CommentStatus.DELETED, lastId)
77+
.stream()
78+
.map(CommentResponseDto::new)
79+
.toList();
8080
}
8181
}
8282

@@ -85,6 +85,11 @@ public List<CommentResponseDto> getComments(Long postId, Long lastId) {
8585
public CommentResponseDto getComment(Long postId, Long commentId) {
8686
Comment comment = findCommentWithValidation(postId, commentId);
8787

88+
// 삭제된 댓글 조회 방지
89+
if (comment.getStatus() == CommentStatus.DELETED) {
90+
throw new IllegalArgumentException("삭제된 댓글입니다.");
91+
}
92+
8893
return new CommentResponseDto(comment);
8994
}
9095

@@ -131,12 +136,17 @@ public void deleteComment(Long postId, Long commentId) {
131136
// 댓글과 게시글의 연관관계 검증
132137
private Comment findCommentWithValidation(Long postId, Long commentId) {
133138
Comment comment = commentRepository.findById(commentId)
134-
.orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + commentId));
139+
.orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + commentId));
135140

136141
if (!comment.getPost().getId().equals(postId)) {
137142
throw new IllegalStateException("댓글이 해당 게시글에 속하지 않습니다.");
138143
}
139144

145+
// 삭제된 댓글 접근 방지 (수정/삭제 시)
146+
if (comment.getStatus() == CommentStatus.DELETED) {
147+
throw new IllegalStateException("삭제된 댓글은 수정하거나 삭제할 수 없습니다.");
148+
}
149+
140150
return comment;
141151
}
142152
}

0 commit comments

Comments
 (0)