From f203347432f1a81af00176fc6204803cdda6f0b7 Mon Sep 17 00:00:00 2001 From: Sangyoo Date: Mon, 20 Feb 2023 02:29:36 +0900 Subject: [PATCH] refactor: get response type changed from page to list #63 --- .../comment/controller/CommentController.java | 13 ++++++------- .../comment/repository/CommentCustomRepository.java | 3 +++ .../repository/CommentCustomRepositoryImpl.java | 10 ++++++++++ .../domain/comment/service/CommentService.java | 5 +++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/controller/CommentController.java b/server/src/main/java/com/main36/pikcha/domain/comment/controller/CommentController.java index 9b1c4b72..c77c3002 100644 --- a/server/src/main/java/com/main36/pikcha/domain/comment/controller/CommentController.java +++ b/server/src/main/java/com/main36/pikcha/domain/comment/controller/CommentController.java @@ -79,15 +79,13 @@ public ResponseEntity> getComment(@PathVariable("comment-id") } @GetMapping("/listof/{post-id}") - public ResponseEntity> getComment(@PathVariable("post-id") @Positive long postId, + public ResponseEntity> getComment(@PathVariable("post-id") @Positive long postId, @Positive @RequestParam(required = false, defaultValue = "1") int page, @Positive @RequestParam(required = false, defaultValue = "10") int size) { Post findPost = postService.findPostNoneSetView(postId); - Page commentPage = commentService.findComments(page - 1, size, findPost); - List commentList = commentPage.getContent(); - for(Comment c : commentList) { - log.info(c.getCommentContent()); - } +// Page commentPage = commentService.findComments(page - 1, size, findPost); +// List commentList = commentPage.getContent(); + List commentList = commentService.findComments(findPost); List result = new ArrayList<>(); Map map = new HashMap<>(); @@ -111,7 +109,8 @@ public ResponseEntity> getComment(@PathVariable("post-id") @ } }); - return ResponseEntity.ok(new MultiResponseDto<>(result, commentPage)); +// return ResponseEntity.ok(new MultiResponseDto<>(result, commentPage)); + return ResponseEntity.ok(new DataResponseDto<>(result)); } @LoginUser diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepository.java b/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepository.java index 36b40af8..4a4c03ec 100644 --- a/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepository.java +++ b/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepository.java @@ -5,6 +5,9 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import java.util.List; + public interface CommentCustomRepository { Page findCommentByPost(Post post, Pageable pageable); + List findCommentByPost(Post post); } diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepositoryImpl.java b/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepositoryImpl.java index cfccae7d..126444e7 100644 --- a/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepositoryImpl.java +++ b/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepositoryImpl.java @@ -31,6 +31,16 @@ public Page findCommentByPost(Post post, Pageable pageable) { return PageableExecutionUtils.getPage(commentList, pageable, ()-> countQuery.fetchOne()); } + @Override + public List findCommentByPost(Post post) { + return jpaQueryFactory.selectFrom(comment) + .leftJoin(comment.parent) + .fetchJoin() + .where(comment.post.postId.eq(post.getPostId())) + .orderBy(comment.parent.commentId.asc().nullsFirst(), comment.createdAt.asc()) + .fetch(); + } + private JPAQuery getCount(Post post) { JPAQuery countQuery = jpaQueryFactory .select(comment.count()) diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/service/CommentService.java b/server/src/main/java/com/main36/pikcha/domain/comment/service/CommentService.java index a4c00626..c752940b 100644 --- a/server/src/main/java/com/main36/pikcha/domain/comment/service/CommentService.java +++ b/server/src/main/java/com/main36/pikcha/domain/comment/service/CommentService.java @@ -14,6 +14,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; import java.util.Optional; @Service @@ -53,6 +54,10 @@ public Page findComments(int page, int size) { public Page findComments(int page, int size, Post post){ return customRepository.findCommentByPost(post, PageRequest.of(page, size)); } + @Transactional(readOnly = true) + public List findComments(Post post){ + return customRepository.findCommentByPost(post); + } public void deleteComment(Comment comment) { commentRepository.delete(comment); }