Skip to content

Commit 16d7dd9

Browse files
committed
feat: 댓글 다건 조회 기능 구현
1 parent 13e0204 commit 16d7dd9

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/main/java/com/back/domain/post/comment/controller/CommentController.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import io.swagger.v3.oas.annotations.Operation;
88
import io.swagger.v3.oas.annotations.tags.Tag;
99
import jakarta.validation.Valid;
10+
import java.util.List;
1011
import lombok.RequiredArgsConstructor;
12+
import org.springframework.web.bind.annotation.GetMapping;
1113
import org.springframework.web.bind.annotation.PathVariable;
1214
import org.springframework.web.bind.annotation.PostMapping;
1315
import org.springframework.web.bind.annotation.RequestBody;
1416
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RequestParam;
1518
import org.springframework.web.bind.annotation.RestController;
1619

1720
@RestController
@@ -37,4 +40,12 @@ public RsData<CommentResponseDto> createComment(
3740
return RsData.successOf(commentService.createComment(postId, reqBody)); // code=200, message="success"
3841
}
3942

43+
@GetMapping
44+
@Operation(summary = "댓글 다건 조회")
45+
public RsData<List<CommentResponseDto>> getComments(
46+
@PathVariable Long postId,
47+
@RequestParam(required = false) Long lastId
48+
) {
49+
return RsData.successOf(commentService.getComments(postId, lastId)); // code=200, message="success"
50+
}
4051
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.back.domain.post.comment.repository;
22

33
import com.back.domain.post.comment.entity.Comment;
4+
import java.util.List;
45
import org.springframework.data.jpa.repository.JpaRepository;
56
import org.springframework.stereotype.Repository;
67

78
@Repository
89
public interface CommentRepository extends JpaRepository<Comment, Long> {
910

11+
List<Comment> findTop10ByPostIdOrderByIdDesc(Long postId);
12+
13+
List<Comment> findTop10ByPostIdAndIdLessThanOrderByIdDesc(Long postId, Long lastId);
1014
}

src/main/java/com/back/domain/post/comment/service/CommentService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.back.domain.post.post.repository.PostRepository;
99
import com.back.domain.user.entity.User;
1010
import com.back.global.rq.Rq;
11+
import java.util.List;
1112
import lombok.RequiredArgsConstructor;
1213
import org.springframework.stereotype.Service;
1314
import org.springframework.transaction.annotation.Transactional;
@@ -36,4 +37,20 @@ public CommentResponseDto createComment(Long postId, CommentCreateRequestDto req
3637

3738
return new CommentResponseDto(commentRepository.save(comment));
3839
}
40+
41+
// 댓글 다건 조회 로직 (무한스크롤)
42+
@Transactional(readOnly = true)
43+
public List<CommentResponseDto> getComments(Long postId, Long lastId) {
44+
if (lastId == null) {
45+
return commentRepository.findTop10ByPostIdOrderByIdDesc(postId)
46+
.stream()
47+
.map(CommentResponseDto::new)
48+
.toList();
49+
} else {
50+
return commentRepository.findTop10ByPostIdAndIdLessThanOrderByIdDesc(postId, lastId)
51+
.stream()
52+
.map(CommentResponseDto::new)
53+
.toList();
54+
}
55+
}
3956
}

0 commit comments

Comments
 (0)