Skip to content

Commit

Permalink
CustomSliceResponseDto & HttpMediaTypeNotAcceptTableException Error 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeg00 committed Jul 15, 2023
1 parent 5265b10 commit 99d7b8c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -51,7 +50,7 @@ public String getReadme(Authentication authentication,
return html;
}

@Operation(summary = "자기글 무한 스크롤(AccessToken 필수)",
@Operation(summary = "자기글 모두 조회하는 API(AccessToken 필수)",
description = "자기글을 모두 조회합니다.")
@GetMapping("/get/searchMyPosts")
public List<GetPostDto> getMyPosts(Authentication authentication) {
Expand Down Expand Up @@ -91,10 +90,6 @@ public List<SelectAllPost> selectAllPost() {
return selectAllPosts;
}





@Operation(summary = "사용자의 글 한 개를 조회하는 API", description = "예시) /api/get/posts?post_id=5를 URL로 호출하면 5번째 글을 조회합니다.")
@GetMapping("/get/posts")
public ResponseEntity<GetPostDto> selectPost(@RequestParam("post_id") Long post_id) {
Expand Down Expand Up @@ -128,9 +123,10 @@ public ResponseEntity<PostsDTO> 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<GetPostDto> getPosts(@RequestParam("page_number") Long page_number) {
public CustomSliceResponseDto getPosts(@RequestParam("page_number") Long page_number) {
return postService.searchByPost(page_number, PageRequest.ofSize(10));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> extends SliceImpl<T> {

private int customField;

public CustomSliceImpl(List<T> content) {
super(content);
}

}
Original file line number Diff line number Diff line change
@@ -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<T> {

private List<GetPostDto> content;
private Long nowPage;
private Long totalPage;
private int countContent;

public CustomSliceResponseDto(List<GetPostDto> content, Long nowPage, Long totalPage,
int countContent) {
this.content = content;
this.nowPage = nowPage;
this.totalPage = totalPage;
this.countContent = countContent;
}

}
23 changes: 17 additions & 6 deletions src/main/java/com/example/Pick_Read_Me/Service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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;
import com.example.Pick_Read_Me.Jwt.JwtProvider;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -241,9 +240,13 @@ public ResponseEntity<PostsDTO> updatePost(Long post_id, PostsDTO postsDTO) {
}


public Slice<GetPostDto> 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<GetPostDto> results = query.selectFrom(post)
.where(
ltPostId(last_post_id)
Expand All @@ -255,7 +258,15 @@ public Slice<GetPostDto> searchByPost(Long page_number, Pageable pageable) {
.map(this::mapToGetPostDto) // Post 엔티티를 GetPostDto로 매핑
.collect(Collectors.toList());

return checkLastPage(pageable, results);
List<GetPostDto> content = checkLastPage(pageable, results).getContent();
CustomSliceResponseDto custom = new CustomSliceResponseDto(
content,
page_number,
totalPage,
content.size()
);
return custom;

}

// no-offset 방식 처리하는 메서드
Expand Down

0 comments on commit 99d7b8c

Please sign in to comment.