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
@@ -1,7 +1,6 @@
package com.back.domain.post.comment.repository;

import com.back.domain.post.comment.entity.Comment;
import com.back.domain.post.comment.enums.CommentStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -13,9 +12,4 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findTop10ByPostIdOrderByIdDesc(Long postId);

List<Comment> findTop10ByPostIdAndIdLessThanOrderByIdDesc(Long postId, Long lastId);

// DELETED가 아닌 댓글만 조회하는 메서드 추가
List<Comment> findTop10ByPostIdAndStatusNotOrderByIdDesc(Long postId, CommentStatus status);

List<Comment> findTop10ByPostIdAndStatusNotAndIdLessThanOrderByIdDesc(Long postId, CommentStatus status, Long lastId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public CommentResponseDto createComment(Long postId, CommentCreateRequestDto req
@Transactional(readOnly = true)
public List<CommentResponseDto> getComments(Long postId, Long lastId) {
if (lastId == null) {
return commentRepository.findTop10ByPostIdAndStatusNotOrderByIdDesc(postId, CommentStatus.DELETED)
.stream()
.map(CommentResponseDto::new)
.toList();
return commentRepository.findTop10ByPostIdOrderByIdDesc(postId)
.stream()
.map(CommentResponseDto::new)
.toList();
} else {
return commentRepository.findTop10ByPostIdAndStatusNotAndIdLessThanOrderByIdDesc(postId, CommentStatus.DELETED, lastId)
.stream()
.map(CommentResponseDto::new)
.toList();
return commentRepository.findTop10ByPostIdAndIdLessThanOrderByIdDesc(postId, lastId)
.stream()
.map(CommentResponseDto::new)
.toList();
}
}

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

// 삭제된 댓글 조회 방지
if (comment.getStatus() == CommentStatus.DELETED) {
throw new IllegalArgumentException("삭제된 댓글입니다.");
}

return new CommentResponseDto(comment);
}

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

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

// 삭제된 댓글 접근 방지 (수정/삭제 시)
if (comment.getStatus() == CommentStatus.DELETED) {
throw new IllegalStateException("삭제된 댓글은 수정하거나 삭제할 수 없습니다.");
}

return comment;
}
}