Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.back.domain.post.post.dto.response.PostResponseDto;
import com.back.domain.post.post.service.PostService;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,10 +17,12 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

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

/**
* 게시글 다건 조회 API
* @return 모든 게시글 리스트
* @param lastId 마지막으로 조회한 게시글 ID (페이징 처리용, optional)
* @return 게시글 목록
*/
@GetMapping
public RsData<List<PostResponseDto>> getAllPosts() {
return RsData.successOf(postService.getAllPosts()); // code=200, message="success"
@Operation(summary = "게시글 다건 조회")
public RsData<List<PostResponseDto>> getAllPosts(
@RequestParam(required = false) Long lastId
) {
return RsData.successOf(postService.getAllPosts(lastId)); // code=200, message="success"
}

/**
Expand All @@ -51,6 +60,7 @@ public RsData<List<PostResponseDto>> getAllPosts() {
* @return 해당 ID의 게시글 정보
*/
@GetMapping("/{postId}")
@Operation(summary = "게시글 단건 조회")
public RsData<PostResponseDto> getPost(
@PathVariable Long postId
) {
Expand All @@ -64,6 +74,7 @@ public RsData<PostResponseDto> getPost(
* @return 수정된 게시글 정보
*/
@PatchMapping("/{postId}")
@Operation(summary = "게시글 수정")
public RsData<PostResponseDto> updatePost(
@PathVariable Long postId,
@Valid @RequestBody PostUpdateRequestDto reqBody
Expand All @@ -77,6 +88,7 @@ public RsData<PostResponseDto> updatePost(
* @return 삭제 성공 메시지
*/
@DeleteMapping("/{postId}")
@Operation(summary = "게시글 삭제")
public RsData<Void> deletePost(
@PathVariable Long postId
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.back.domain.post.post.repository;

import com.back.domain.post.post.entity.Post;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
// 첫 페이지 (최신순, id 기준)
List<Post> findTop10ByOrderByIdDesc();

// 이후 페이지 (lastId보다 작은 id들 중 최신순)
List<Post> findTop10ByIdLessThanOrderByIdDesc(Long lastId);
}
13 changes: 11 additions & 2 deletions src/main/java/com/back/domain/post/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ public PostResponseDto createPost(PostCreateRequestDto reqBody) {

// 게시글 다건 조회 로직
@Transactional(readOnly = true)
public List<PostResponseDto> getAllPosts() {
List<Post> posts = postRepository.findAll();
public List<PostResponseDto> getAllPosts(Long lastId) {
// List<Post> posts = postRepository.findAll();
List<Post> posts;

if (lastId == null) {
// 첫 페이지 요청
posts = postRepository.findTop10ByOrderByIdDesc();
} else {
// 이후 페이지 요청
posts = postRepository.findTop10ByIdLessThanOrderByIdDesc(lastId);
}

return posts.stream()
.map(PostResponseDto::new)
Expand Down