Skip to content

Commit

Permalink
fix : 댓글 목록 페이지네이션 버그 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
moonyaeyoon committed Jul 25, 2024
1 parent 3e0c88e commit 0fe13ed
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static CommentDetail from(Comment comment) {
.content(comment.getContent())
.name(comment.getAccount().getName())
.isAnonymous(comment.getIsAnonymous())
.imageUrl(comment.getCommentImage() != null ? comment.getCommentImage().getImageKey() : null)
.imageUrl(comment.getCommentImage() != null ? comment.getCommentImage().getImageUrl() : null)
.updatedAt(comment.getUpdatedAt())
.build();
}
Expand Down
185 changes: 115 additions & 70 deletions src/main/java/meltingpot/server/comment/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -34,104 +35,121 @@ public class CommentService {
private FileService fileService;



/* 댓글 작성 */
public ResponseCode createComment(CommentCreateRequest commentCreateRequest , Account account, Long postId) {
Post post = findPostById(postId);
String commentImgUrl = null;
if (commentCreateRequest.getImageKey() != null){
commentImgUrl = getCdnUrl(commentCreateRequest.getImageKey());
}
Comment comment = commentCreateRequest.toEntity(post, account, null);
if (commentCreateRequest.getImageKey() == null || commentCreateRequest.getImageKey().isEmpty()) {
return null;
}

if (commentImgUrl != null) {
CommentImage commentImage = CommentImage.builder()
.imageUrl(commentImgUrl)
.comment(comment)
.account(account)
.build();
comment.setCommentImage(commentImage);
if (commentCreateRequest.getImageKey() != null && !commentCreateRequest.getImageKey().isEmpty()) {
String commentImgUrl = getCdnUrl(commentCreateRequest.getImageKey());
createCommentImage(comment, account, commentImgUrl);
}

commentRepository.save(comment);
return ResponseCode.CREATE_COMMENT_SUCCESS;
}


public ResponseCode createChildComment(CommentCreateRequest commentCreateRequest, Account account, Long commentId) {
Comment parentComment = findCommentById(commentId);
Post post = findPostById(parentComment.getPost().getId());
String commentImgUrl = null;
if (commentCreateRequest.getImageKey() != null) {
commentImgUrl = getCdnUrl(commentCreateRequest.getImageKey());
}
/* 대댓글 작성 */
public ResponseCode createChildComment(CommentCreateRequest commentCreateRequest, Account account, Long parentCommentId) {
Comment parentComment = findCommentById(parentCommentId);
Post post = parentComment.getPost();
Comment childComment = commentCreateRequest.toEntity(post, account, parentComment);

if (commentImgUrl != null) {
CommentImage commentImage = CommentImage.builder()
.imageUrl(commentImgUrl)
.comment(childComment)
.account(account)
.build();
childComment.setCommentImage(commentImage);
if (commentCreateRequest.getImageKey() != null && !commentCreateRequest.getImageKey().isEmpty()) {
String commentImgUrl = getCdnUrl(commentCreateRequest.getImageKey());
createCommentImage(childComment, account, commentImgUrl);
}

commentRepository.save(childComment);
return ResponseCode.CREATE_CHILD_COMMENT_SUCCESS;
}


/* 댓글 수정 */
public ResponseCode updateComment(CommentCreateRequest updateCommentDTO, Account account, Long commentId) {
// 댓글을 ID로 찾기
Comment comment = findCommentById(commentId);

// 댓글 내용 업데이트
comment.setContent(updateCommentDTO.getContent());

// 새로운 이미지 키와 기존 댓글 이미지 가져오기
String newImageKey = updateCommentDTO.getImageKey();
CommentImage oldCommentImage = comment.getCommentImage();
updateCommentImage(comment, account, newImageKey);

if (newImageKey == null || newImageKey.isEmpty()) {
// 새로운 이미지 키가 없을 경우 기존 이미지 삭제
if (oldCommentImage != null) {
commentImageRepository.delete(oldCommentImage);
comment.setCommentImage(null);
}
} else {
// 새로운 이미지 키가 있을 경우 이미지 업데이트 또는 생성
String newImageUrl = getCdnUrl(newImageKey);
if (oldCommentImage != null) {
oldCommentImage.setImageUrl(newImageKey);
commentImageRepository.save(oldCommentImage);
} else {
CommentImage newCommentImage = CommentImage.builder()
.imageUrl(newImageUrl)
.comment(comment)
.account(account)
.build();
comment.setCommentImage(newCommentImage);
commentImageRepository.save(newCommentImage);
}
}
// 댓글 저장
commentRepository.save(comment);
return ResponseCode.UPDATE_COMMENT_SUCCESS;
}

@Transactional(readOnly = true)


/* 댓글 목록 불러오기 */
// @Transactional(readOnly = true)
// public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) {
// List<CommentsListResponse.CommentDetail> commentDetailDTOs = new ArrayList<>();
// int count = 0;
// Long parentCursor = null;
//
// // Cursor가 자식 댓글에 해당하는 경우 처리
// if (cursor != null) {
// // Cursor가 부모 댓글이 아닌 자식 댓글을 나타내는 경우
// Comment childComment = commentRepository.findById(cursor).orElse(null);
// if (childComment != null && childComment.getParent() != null) {
// Comment parentComment = childComment.getParent();
// List<Comment> remainingChildren = commentRepository.findChildrenCommentsByParentId(parentComment.getId(), cursor);
// for (Comment child : remainingChildren) {
// if (count >= pageSize) break;
// commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(child));
// count++;
// }
// parentCursor = parentComment.getId();
//
// // 만약 자식 댓글을 모두 가져왔고, 페이지가 꽉 차지 않았다면 다음 부모 댓글로 넘어감
// if (count < pageSize && remainingChildren.size() < pageSize) {
// parentCursor = parentComment.getId();
// }
// } else {
// parentCursor = cursor; // cursor가 부모 댓글인 경우
// }
// }
//
// // 부모 댓글과 자식 댓글을 가져오는 처리
// if (count < pageSize) {
// Pageable pageable = PageRequest.of(0, pageSize - count);
// List<Comment> parentComments = commentRepository.findParentCommentsByPostId(postId, parentCursor, pageable);
// for (Comment parent : parentComments) {
// if (count >= pageSize) break;
// commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(parent));
// count++;
//
// List<Comment> children = commentRepository.findChildrenCommentsByParentId(parent.getId(), null);
// for (Comment child : children) {
// if (count >= pageSize) break;
// commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(child));
// count++;
// }
// }
// }
//
// Long nextCursor = (count < pageSize) ? null : commentDetailDTOs.get(commentDetailDTOs.size() - 1).getCommentId();
// boolean isLast = (count < pageSize);
//
// return CommentsListResponse.from(commentDetailDTOs,nextCursor,isLast);
// }

public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) {
List<CommentsListResponse.CommentDetail> commentDetailDTOs = new ArrayList<>();
int count = 0;
Long parentCursor = null;

// Cursor가 자식 댓글에 해당하는 경우 처리
if (cursor != null) {
// Cursor가 부모 댓글이 아닌 자식 댓글을 나타내는 경우
Comment childComment = commentRepository.findById(cursor).orElse(null);
if (childComment != null && childComment.getParent() != null) {
Comment parentComment = childComment.getParent();
Comment cursorComment = commentRepository.findById(cursor).orElse(null);
if (cursorComment != null) {
Comment parentComment;
if (cursorComment.getParent() != null) {
// Cursor가 자식 댓글을 나타내는 경우
parentComment = cursorComment.getParent();
} else {
// Cursor가 부모 댓글을 나타내는 경우
parentComment = cursorComment;
}

List<Comment> remainingChildren = commentRepository.findChildrenCommentsByParentId(parentComment.getId(), cursor);
for (Comment child : remainingChildren) {
if (count >= pageSize) break;
Expand Down Expand Up @@ -170,16 +188,11 @@ public CommentsListResponse getCommentsList(Account account, Long postId, Long c
Long nextCursor = (count < pageSize) ? null : commentDetailDTOs.get(commentDetailDTOs.size() - 1).getCommentId();
boolean isLast = (count < pageSize);

return CommentsListResponse.from(commentDetailDTOs,nextCursor,isLast);
return CommentsListResponse.from(commentDetailDTOs, nextCursor, isLast);
}



private String getCdnUrl(String imageKey) {
String prefix = "comment"; // 적절한 prefix 값을 설정
return fileService.getCdnUrl(prefix, imageKey);
}


private Comment findCommentById(Long commentId) {
return commentRepository.findById(commentId)
Expand All @@ -197,6 +210,38 @@ private Post findPostById(Long postId) {
.orElseThrow(() -> new RuntimeException("게시물을 찾을 수 없습니다."));
}

private String getCdnUrl(String imageKey) {
String prefix = "comment"; // 적절한 prefix 값을 설정
return fileService.getCdnUrl(prefix, imageKey);
}

private void createCommentImage(Comment comment, Account account, String imageUrl) {
CommentImage commentImage = CommentImage.builder()
.imageUrl(imageUrl)
.comment(comment)
.account(account)
.build();
comment.setCommentImage(commentImage);
}

private void updateCommentImage(Comment comment, Account account, String newImageKey) {
CommentImage oldCommentImage = comment.getCommentImage();
if (newImageKey == null || newImageKey.isEmpty()) {
if (oldCommentImage != null) {
commentImageRepository.delete(oldCommentImage);
comment.setCommentImage(null);
}
} else {
String newImageUrl = getCdnUrl(newImageKey);
if (oldCommentImage != null) {
oldCommentImage.setImageUrl(newImageUrl);
commentImageRepository.save(oldCommentImage);
} else {
createCommentImage(comment, account, newImageUrl);
}
}
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {



@Query("SELECT c FROM Comment c WHERE c.post.id = :postId AND c.parent IS NULL AND (:cursor IS NULL OR c.id > :cursor) ORDER BY c.id ASC")
List<Comment> findParentCommentsByPostId(@Param("postId") Long postId, @Param("cursor") Long cursor, Pageable pageable);
@Query("SELECT c FROM Comment c WHERE c.post.id = :postId AND c.parent IS NULL AND (:parentCursor IS NULL OR c.id > :parentCursor) ORDER BY c.id ASC")
List<Comment> findParentCommentsByPostId(@Param("postId") Long postId, @Param("parentCursor") Long parentCursor, Pageable pageable);

@Query("SELECT c FROM Comment c WHERE c.parent.id = :parentId AND (:cursor IS NULL OR c.id > :cursor) ORDER BY c.id ASC")
List<Comment> findChildrenCommentsByParentId(@Param("parentId") Long parentId, @Param("cursor") Long cursor);

Slice<Comment> findAllByAccountAndDeletedAtIsNullOrderByIdDesc(Account account, Pageable pageable);




}
Loading

0 comments on commit 0fe13ed

Please sign in to comment.