Skip to content

Commit

Permalink
refactor: get response type changed from page to list #63
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangyoo committed Feb 19, 2023
1 parent 1a96f29 commit f203347
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,13 @@ public ResponseEntity<DataResponseDto<?>> getComment(@PathVariable("comment-id")
}

@GetMapping("/listof/{post-id}")
public ResponseEntity<MultiResponseDto<?>> getComment(@PathVariable("post-id") @Positive long postId,
public ResponseEntity<DataResponseDto<?>> 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<Comment> commentPage = commentService.findComments(page - 1, size, findPost);
List<Comment> commentList = commentPage.getContent();
for(Comment c : commentList) {
log.info(c.getCommentContent());
}
// Page<Comment> commentPage = commentService.findComments(page - 1, size, findPost);
// List<Comment> commentList = commentPage.getContent();
List<Comment> commentList = commentService.findComments(findPost);
List<CommentDetailResponseDto> result = new ArrayList<>();
Map<Long, CommentDetailResponseDto> map = new HashMap<>();

Expand All @@ -111,7 +109,8 @@ public ResponseEntity<MultiResponseDto<?>> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface CommentCustomRepository {
Page<Comment> findCommentByPost(Post post, Pageable pageable);
List<Comment> findCommentByPost(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public Page<Comment> findCommentByPost(Post post, Pageable pageable) {
return PageableExecutionUtils.getPage(commentList, pageable, ()-> countQuery.fetchOne());
}

@Override
public List<Comment> 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<Long> getCount(Post post) {
JPAQuery<Long> countQuery = jpaQueryFactory
.select(comment.count())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
Expand Down Expand Up @@ -53,6 +54,10 @@ public Page<Comment> findComments(int page, int size) {
public Page<Comment> findComments(int page, int size, Post post){
return customRepository.findCommentByPost(post, PageRequest.of(page, size));
}
@Transactional(readOnly = true)
public List<Comment> findComments(Post post){
return customRepository.findCommentByPost(post);
}
public void deleteComment(Comment comment) {
commentRepository.delete(comment);
}
Expand Down

0 comments on commit f203347

Please sign in to comment.