diff --git a/JWT/src/main/java/JWTLogIn/JWT/post/controller/PostController.java b/JWT/src/main/java/JWTLogIn/JWT/post/controller/PostController.java index 5a726a2..6c6de2f 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/post/controller/PostController.java +++ b/JWT/src/main/java/JWTLogIn/JWT/post/controller/PostController.java @@ -6,9 +6,13 @@ import JWTLogIn.JWT.post.service.PostService; import JWTLogIn.JWT.user.entity.UserEntity; import JWTLogIn.JWT.user.repository.UserRepository; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; @@ -119,6 +123,15 @@ public ResponseEntity delete(@PathVariable("id") Long id) } - - + @CrossOrigin + @GetMapping("/notice") + @Transactional + public ResponseEntity getPostsInPage(@RequestParam int page, + @RequestParam(required = false, defaultValue = "15") int size) { + System.out.println("-- Get Posts in Page --"); + // 첫 번째 페이지 page = 0이므로, page-1로 전달 -> 1부터 요청할 수 있도록 + Page postsInPage = postService.findPostsInPage(page-1, size); + + return ResponseEntity.ok(postsInPage); + } } diff --git a/JWT/src/main/java/JWTLogIn/JWT/post/entity/PostEntity.java b/JWT/src/main/java/JWTLogIn/JWT/post/entity/PostEntity.java index b0dcfa2..0ad4afa 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/post/entity/PostEntity.java +++ b/JWT/src/main/java/JWTLogIn/JWT/post/entity/PostEntity.java @@ -2,6 +2,7 @@ import JWTLogIn.JWT.post.dto.PostDto; import JWTLogIn.JWT.user.entity.UserEntity; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.*; import org.hibernate.annotations.UpdateTimestamp; diff --git a/JWT/src/main/java/JWTLogIn/JWT/post/repository/PostRepository.java b/JWT/src/main/java/JWTLogIn/JWT/post/repository/PostRepository.java index fce9fe8..78943b9 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/post/repository/PostRepository.java +++ b/JWT/src/main/java/JWTLogIn/JWT/post/repository/PostRepository.java @@ -2,10 +2,11 @@ import JWTLogIn.JWT.post.dto.PostDto; import JWTLogIn.JWT.post.entity.PostEntity; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.transaction.annotation.Transactional; -import java.awt.print.Pageable; import java.util.List; import java.util.Optional; @@ -16,6 +17,7 @@ public interface PostRepository extends JpaRepository { @Override S save(S entity); - List findByTitleContains(String search); - +// List findByTitleContains(String search); + List findByDescriptionContains(String search); + Page findAllByOrderByIdDesc(Pageable pageable); } diff --git a/JWT/src/main/java/JWTLogIn/JWT/post/service/PostService.java b/JWT/src/main/java/JWTLogIn/JWT/post/service/PostService.java index 9b0e81b..a629991 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/post/service/PostService.java +++ b/JWT/src/main/java/JWTLogIn/JWT/post/service/PostService.java @@ -6,6 +6,8 @@ import JWTLogIn.JWT.user.entity.UserEntity; import JWTLogIn.JWT.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -17,6 +19,7 @@ import static JWTLogIn.JWT.post.entity.PostEntity.toDto; @Service +@Transactional @RequiredArgsConstructor public class PostService { private final PostRepository postRepository; @@ -60,7 +63,7 @@ public List searchPosts(String search) { List postDtos = new ArrayList<>(); - List SearchedEntity = postRepository.findByTitleContains(search); + List SearchedEntity = postRepository.findByDescriptionContains(search); for (PostEntity postEntity : SearchedEntity) { System.out.println("postEntity = " + postEntity); postDtos.add(toDto(postEntity)); @@ -115,4 +118,24 @@ public void deletePost(Long postId) // 게시글 삭제하기 { postRepository.deleteById(postId); } + + public Page findPostsInPage(int page, int size) { + // Post DB에서 Page 단위로 가져오기 + PageRequest pageRequest = PageRequest.of(page, size); + Page postPage = postRepository.findAllByOrderByIdDesc(pageRequest); + + // Post 반환 + DTO로 변환 + List posts = postPage.getContent(); + List postDtos = new ArrayList<>(); + for (PostEntity postEntity : posts) { + PostDto dto = toDto(postEntity); + postDtos.add(dto); + postDtos.toString(); + } + + Page postDtoPage = postPage.map(PostEntity::toDto); + + return postDtoPage; + } + }