Skip to content

Commit 3367fcc

Browse files
committed
feat: 무한스크롤 페이징 구현
1 parent 80ad213 commit 3367fcc

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/main/java/com/back/domain/post/post/controller/PostController.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.back.domain.post.post.dto.response.PostResponseDto;
66
import com.back.domain.post.post.service.PostService;
77
import com.back.global.rsData.RsData;
8+
import io.swagger.v3.oas.annotations.Operation;
9+
import io.swagger.v3.oas.annotations.tags.Tag;
810
import jakarta.validation.Valid;
911
import java.util.List;
1012
import lombok.RequiredArgsConstructor;
@@ -15,10 +17,12 @@
1517
import org.springframework.web.bind.annotation.PostMapping;
1618
import org.springframework.web.bind.annotation.RequestBody;
1719
import org.springframework.web.bind.annotation.RequestMapping;
20+
import org.springframework.web.bind.annotation.RequestParam;
1821
import org.springframework.web.bind.annotation.RestController;
1922

2023
@RestController
2124
@RequestMapping("/api/posts")
25+
@Tag(name = "ApiPostController", description = "API 게시글 컨트롤러")
2226
@RequiredArgsConstructor
2327
public class PostController {
2428

@@ -30,6 +34,7 @@ public class PostController {
3034
* @return 작성된 게시글 정보
3135
*/
3236
@PostMapping
37+
@Operation(summary = "게시글 작성")
3338
public RsData<PostResponseDto> createPost(
3439
@Valid @RequestBody PostCreateRequestDto reqBody
3540
) {
@@ -38,11 +43,15 @@ public RsData<PostResponseDto> createPost(
3843

3944
/**
4045
* 게시글 다건 조회 API
41-
* @return 모든 게시글 리스트
46+
* @param lastId 마지막으로 조회한 게시글 ID (페이징 처리용, optional)
47+
* @return 게시글 목록
4248
*/
4349
@GetMapping
44-
public RsData<List<PostResponseDto>> getAllPosts() {
45-
return RsData.successOf(postService.getAllPosts()); // code=200, message="success"
50+
@Operation(summary = "게시글 다건 조회")
51+
public RsData<List<PostResponseDto>> getAllPosts(
52+
@RequestParam(required = false) Long lastId
53+
) {
54+
return RsData.successOf(postService.getAllPosts(lastId)); // code=200, message="success"
4655
}
4756

4857
/**
@@ -51,6 +60,7 @@ public RsData<List<PostResponseDto>> getAllPosts() {
5160
* @return 해당 ID의 게시글 정보
5261
*/
5362
@GetMapping("/{postId}")
63+
@Operation(summary = "게시글 단건 조회")
5464
public RsData<PostResponseDto> getPost(
5565
@PathVariable Long postId
5666
) {
@@ -64,6 +74,7 @@ public RsData<PostResponseDto> getPost(
6474
* @return 수정된 게시글 정보
6575
*/
6676
@PatchMapping("/{postId}")
77+
@Operation(summary = "게시글 수정")
6778
public RsData<PostResponseDto> updatePost(
6879
@PathVariable Long postId,
6980
@Valid @RequestBody PostUpdateRequestDto reqBody
@@ -77,6 +88,7 @@ public RsData<PostResponseDto> updatePost(
7788
* @return 삭제 성공 메시지
7889
*/
7990
@DeleteMapping("/{postId}")
91+
@Operation(summary = "게시글 삭제")
8092
public RsData<Void> deletePost(
8193
@PathVariable Long postId
8294
) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.back.domain.post.post.repository;
22

33
import com.back.domain.post.post.entity.Post;
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 PostRepository extends JpaRepository<Post, Long> {
10+
// 첫 페이지 (최신순, id 기준)
11+
List<Post> findTop10ByOrderByIdDesc();
912

13+
// 이후 페이지 (lastId보다 작은 id들 중 최신순)
14+
List<Post> findTop10ByIdLessThanOrderByIdDesc(Long lastId);
1015
}

src/main/java/com/back/domain/post/post/service/PostService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,17 @@ public PostResponseDto createPost(PostCreateRequestDto reqBody) {
5454

5555
// 게시글 다건 조회 로직
5656
@Transactional(readOnly = true)
57-
public List<PostResponseDto> getAllPosts() {
58-
List<Post> posts = postRepository.findAll();
57+
public List<PostResponseDto> getAllPosts(Long lastId) {
58+
// List<Post> posts = postRepository.findAll();
59+
List<Post> posts;
60+
61+
if (lastId == null) {
62+
// 첫 페이지 요청
63+
posts = postRepository.findTop10ByOrderByIdDesc();
64+
} else {
65+
// 이후 페이지 요청
66+
posts = postRepository.findTop10ByIdLessThanOrderByIdDesc(lastId);
67+
}
5968

6069
return posts.stream()
6170
.map(PostResponseDto::new)

0 commit comments

Comments
 (0)