Skip to content

Commit

Permalink
feat: 댓글 조회 Ver.2 (Paging)
Browse files Browse the repository at this point in the history
  • Loading branch information
jang-namu committed Apr 14, 2024
1 parent 800096d commit 7449984
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ public ResponseEntity<List<CommentResponse>> getCommentsByUrl(@RequestParam Stri
public List<CommentResponse> getCommentsByPostId(@PathVariable Long postId) {
return commentService.getCommentsByPostId(postId);
}

@Operation(summary = "댓글 조회(postId) Ver.2", description = "포스트에 달린 댓글 조회(Paging)")
@GetMapping("/v2/comments/{postId}")
public List<CommentResponse> getCommentsByPostId(@PathVariable Long postId,
@RequestParam Integer offset,
@RequestParam Integer limit) {
return commentService.getCommentsByPostIdWithPaging(postId, offset, limit);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.bugflix.weblog.comment.repository;

import com.bugflix.weblog.comment.domain.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment,Long> {
List<Comment> findAllByPostPageUrl(String pageUrl);
List<Comment> findAllByPostPostId(Long postId);
Page<Comment> findAllByPostPostId(Long postId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.bugflix.weblog.security.domain.CustomUserDetails;
import com.bugflix.weblog.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -113,4 +115,17 @@ public List<CommentResponse> getCommentsByPostId(Long postId) {

return commentResponses;
}

public List<CommentResponse> getCommentsByPostIdWithPaging(Long postId, Integer offset, Integer limit) {
List<CommentResponse> commentResponses = new ArrayList<>();
Page<Comment> comments = commentRepository.findAllByPostPostId(postId,
PageRequest.of(offset, limit));

for (Comment comment : comments) {
User user = comment.getUser();
commentResponses.add(CommentResponse.of(comment, user, user.getProfile()));
}

return commentResponses;
}
}

0 comments on commit 7449984

Please sign in to comment.