diff --git a/src/main/java/com/example/Pick_Read_Me/Controller/PostController.java b/src/main/java/com/example/Pick_Read_Me/Controller/PostController.java index d5928a4..5465952 100644 --- a/src/main/java/com/example/Pick_Read_Me/Controller/PostController.java +++ b/src/main/java/com/example/Pick_Read_Me/Controller/PostController.java @@ -1,5 +1,6 @@ package com.example.Pick_Read_Me.Controller; +import com.example.Pick_Read_Me.Domain.Dto.CustomSlice.CustomSliceResponseDto; import com.example.Pick_Read_Me.Domain.Dto.PostDto.GetPostDto; import com.example.Pick_Read_Me.Domain.Dto.PostDto.PostsDTO; import com.example.Pick_Read_Me.Domain.Dto.PostDto.SelectAllPost; @@ -13,8 +14,6 @@ import org.apache.batik.transcoder.TranscoderException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Slice; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; @@ -51,7 +50,7 @@ public String getReadme(Authentication authentication, return html; } - @Operation(summary = "자기글 무한 스크롤(AccessToken 필수)", + @Operation(summary = "자기글 모두 조회하는 API(AccessToken 필수)", description = "자기글을 모두 조회합니다.") @GetMapping("/get/searchMyPosts") public List getMyPosts(Authentication authentication) { @@ -91,10 +90,6 @@ public List selectAllPost() { return selectAllPosts; } - - - - @Operation(summary = "사용자의 글 한 개를 조회하는 API", description = "예시) /api/get/posts?post_id=5를 URL로 호출하면 5번째 글을 조회합니다.") @GetMapping("/get/posts") public ResponseEntity selectPost(@RequestParam("post_id") Long post_id) { @@ -128,9 +123,10 @@ public ResponseEntity updatePost(@RequestParam Long post_id, } @Operation(summary = "게시글 무한스크롤 API", description = "예시)" + - "/api/get/infinity/posts?page_number=1이면 마지막글부터 마지막글-10번까지의 글이 조회됩니다.") + "/api/get/infinity/posts?page_number=1이면 첫번째 페이지가 조회됩니다.\n"+ + "nowPage = 현재 페이지, totalPage = 전체 피이지 countContent = 현재 조회한 글 갯수입니당~\n") @GetMapping("/get/infinity/posts") - public Slice getPosts(@RequestParam("page_number") Long page_number) { + public CustomSliceResponseDto getPosts(@RequestParam("page_number") Long page_number) { return postService.searchByPost(page_number, PageRequest.ofSize(10)); } diff --git a/src/main/java/com/example/Pick_Read_Me/Domain/Dto/CustomSlice/CustomSliceImpl.java b/src/main/java/com/example/Pick_Read_Me/Domain/Dto/CustomSlice/CustomSliceImpl.java new file mode 100644 index 0000000..ec64503 --- /dev/null +++ b/src/main/java/com/example/Pick_Read_Me/Domain/Dto/CustomSlice/CustomSliceImpl.java @@ -0,0 +1,16 @@ +package com.example.Pick_Read_Me.Domain.Dto.CustomSlice; + +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.SliceImpl; + +import java.util.List; + +public class CustomSliceImpl extends SliceImpl { + + private int customField; + + public CustomSliceImpl(List content) { + super(content); + } + +} diff --git a/src/main/java/com/example/Pick_Read_Me/Domain/Dto/CustomSlice/CustomSliceResponseDto.java b/src/main/java/com/example/Pick_Read_Me/Domain/Dto/CustomSlice/CustomSliceResponseDto.java new file mode 100644 index 0000000..76b450a --- /dev/null +++ b/src/main/java/com/example/Pick_Read_Me/Domain/Dto/CustomSlice/CustomSliceResponseDto.java @@ -0,0 +1,24 @@ +package com.example.Pick_Read_Me.Domain.Dto.CustomSlice; + +import com.example.Pick_Read_Me.Domain.Dto.PostDto.GetPostDto; +import lombok.Getter; + +import java.util.List; + +@Getter +public class CustomSliceResponseDto { + + private List content; + private Long nowPage; + private Long totalPage; + private int countContent; + + public CustomSliceResponseDto(List content, Long nowPage, Long totalPage, + int countContent) { + this.content = content; + this.nowPage = nowPage; + this.totalPage = totalPage; + this.countContent = countContent; + } + +} diff --git a/src/main/java/com/example/Pick_Read_Me/Service/PostService.java b/src/main/java/com/example/Pick_Read_Me/Service/PostService.java index 92cfbeb..6a91a5a 100644 --- a/src/main/java/com/example/Pick_Read_Me/Service/PostService.java +++ b/src/main/java/com/example/Pick_Read_Me/Service/PostService.java @@ -1,8 +1,10 @@ package com.example.Pick_Read_Me.Service; +import com.example.Pick_Read_Me.Domain.Dto.CustomSlice.CustomSliceResponseDto; import com.example.Pick_Read_Me.Domain.Dto.PostDto.GetPostDto; import com.example.Pick_Read_Me.Domain.Dto.PostDto.PostsDTO; import com.example.Pick_Read_Me.Domain.Dto.PostDto.SelectAllPost; + import com.example.Pick_Read_Me.Domain.Entity.Member; import com.example.Pick_Read_Me.Domain.Entity.Post; import com.example.Pick_Read_Me.Exception.MemberNotFoundException; @@ -10,7 +12,6 @@ import com.example.Pick_Read_Me.Repository.MemberRepository; import com.example.Pick_Read_Me.Repository.PostRepository; import com.querydsl.core.types.dsl.BooleanExpression; -import com.querydsl.core.types.dsl.Expressions; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.extern.slf4j.Slf4j; import org.apache.batik.transcoder.TranscoderException; @@ -20,9 +21,7 @@ import org.jsoup.select.Elements; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; -import org.springframework.data.domain.SliceImpl; +import org.springframework.data.domain.*; import org.springframework.http.*; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; @@ -241,9 +240,13 @@ public ResponseEntity updatePost(Long post_id, PostsDTO postsDTO) { } - public Slice searchByPost(Long page_number, Pageable pageable) { + public CustomSliceResponseDto searchByPost(Long page_number, Pageable pageable) { Long last_post_id = Long.valueOf(postRepository.findAll().size()); + Long totalPage = last_post_id/10; + log.info(String.valueOf(last_post_id)); last_post_id -= page_number*10; + + List results = query.selectFrom(post) .where( ltPostId(last_post_id) @@ -255,7 +258,15 @@ public Slice searchByPost(Long page_number, Pageable pageable) { .map(this::mapToGetPostDto) // Post 엔티티를 GetPostDto로 매핑 .collect(Collectors.toList()); - return checkLastPage(pageable, results); + List content = checkLastPage(pageable, results).getContent(); + CustomSliceResponseDto custom = new CustomSliceResponseDto( + content, + page_number, + totalPage, + content.size() + ); + return custom; + } // no-offset 방식 처리하는 메서드