Skip to content

Commit

Permalink
#256 Feat: 댓글 페이징 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-haeseung committed Nov 10, 2024
1 parent 6d9fd9c commit c93c7c2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -69,10 +71,11 @@ public ApiResponse<CommentResponseDTO.CommentDTO> updateComment(
@GetMapping("/posts/{post_id}/comments")
public ApiResponse<List<CommentResponseDTO.CommentDTO>> getComments(
@PathVariable("post_id") Long postId,
@AuthenticationPrincipal CustomMemberDetails memberDetails
@AuthenticationPrincipal CustomMemberDetails memberDetails,
@PageableDefault(size = 10) Pageable pageable
) {
Long memberId = memberDetails.getId();
List<Comment> comments = commentService.getComments(postId, memberId);
List<Comment> comments = commentService.getComments(postId, memberId, pageable);
return ApiResponse.onSuccess(SuccessStatus.COMMENT_OK, CommentConverter.toCommentResponseListDto(comments));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.codiary.backend.domain.comment.repository;

import com.codiary.backend.domain.comment.entity.Comment;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface CommentRepositoryCustom {

List<Comment> findByPostWithMemberInfoOrderByCreatedAtAsc(Long postId);
List<Comment> findByPostWithMemberInfoOrderByCreatedAtAsc(Long postId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;

@RequiredArgsConstructor
public class CommentRepositoryImpl implements CommentRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<Comment> findByPostWithMemberInfoOrderByCreatedAtAsc(Long postId) {
public List<Comment> findByPostWithMemberInfoOrderByCreatedAtAsc(Long postId, Pageable pageable) {
List<Comment> comments = queryFactory
.selectFrom(comment)
.leftJoin(comment.member, member)
.leftJoin(comment.post, post)
.where(comment.post.postId.eq(postId))
// .offset(pageable.getOffset())
// .limit(pageable.getPageSize())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(comment.createdAt.asc())
.fetch();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.codiary.backend.global.apiPayload.exception.handler.PostHandler;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -95,7 +96,7 @@ public Comment updateComment(Long commentId, Long memberId, CommentRequestDTO.Co
}

@Transactional(readOnly = true)
public List<Comment> getComments(Long postId, Long memberId) {
public List<Comment> getComments(Long postId, Long memberId, Pageable pageable) {
// validation: 사용자, post 유무 확인
Member requester = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
Expand All @@ -112,7 +113,7 @@ public List<Comment> getComments(Long postId, Long memberId) {
}

// business logic: 댓글 조회
List<Comment> comments = commentRepository.findByPostWithMemberInfoOrderByCreatedAtAsc(postId);
List<Comment> comments = commentRepository.findByPostWithMemberInfoOrderByCreatedAtAsc(postId, pageable);

// response: comment list 반환
return comments;
Expand Down

0 comments on commit c93c7c2

Please sign in to comment.