diff --git a/src/main/java/com/ticle/server/post/controller/PostApiController.java b/src/main/java/com/ticle/server/post/controller/PostApiController.java index 7b15e27..8e5dae8 100644 --- a/src/main/java/com/ticle/server/post/controller/PostApiController.java +++ b/src/main/java/com/ticle/server/post/controller/PostApiController.java @@ -39,20 +39,16 @@ public class PostApiController { @GetMapping public ResponseEntity> findAllArticles(@RequestParam(required = false) String category, @RequestParam(defaultValue = "1") int page) { - int size = 9; // 한 페이지에 보여질 객체 수 + Page postPage = postService.findAllByCategory(category, page); - Pageable pageable = PageRequest.of(page - 1, size); - - Page postPage = postService.findAllByCategory(category, pageable); - - List postResponses = postPage.getContent().stream() - .map(PostResponse::from) - .collect(Collectors.toList()); +// List postResponses = postPage.getContent().stream() +// .map(PostResponse::from) +// .collect(Collectors.toList()); return ResponseEntity .status(HttpStatus.OK) - .body(ResponseTemplate.from(postResponses)); + .body(ResponseTemplate.from(postPage)); } //특정 아티클 조회 diff --git a/src/main/java/com/ticle/server/post/service/PostService.java b/src/main/java/com/ticle/server/post/service/PostService.java index 0a641db..cff8c70 100644 --- a/src/main/java/com/ticle/server/post/service/PostService.java +++ b/src/main/java/com/ticle/server/post/service/PostService.java @@ -2,6 +2,7 @@ import com.ticle.server.memo.domain.Memo; import com.ticle.server.mypage.repository.MemoRepository; +import com.ticle.server.post.dto.PostResponse; import com.ticle.server.scrapped.dto.ScrappedDto; import com.ticle.server.user.domain.type.Category; import com.ticle.server.post.domain.Post; @@ -12,17 +13,14 @@ import com.ticle.server.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpStatus; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import java.util.Optional; -import java.util.List; @RequiredArgsConstructor @Service @@ -34,14 +32,22 @@ public class PostService { private final MemoRepository memoRepository; // 카테고리에 맞는 글 찾기 - public Page findAllByCategory(String category, Pageable pageable) { + public Page findAllByCategory(String category, int page) { + + final int SIZE = 9; // 한 페이지에 보여질 객체 수 + + Pageable pageable = PageRequest.of(page - 1, SIZE); + Page postPage; + if (category == null || category.isEmpty()) { // 모든 글 조회 - return postRepository.findAll(pageable); + postPage = postRepository.findAll(pageable); } else { //카테고리에 맞는 글 조회 - return postRepository.findByCategory(Category.valueOf(category), pageable); + postPage = postRepository.findByCategory(Category.valueOf(category), pageable); } + return postPage.map(PostResponse::from); + } //postId로 조회한 특정 post 정보 리턴 @@ -94,7 +100,7 @@ public Object writeMemo(long id, UserDetails userDetails, String targetText, Str memo.setUser(user); memo.setTargetText(targetText); memo.setContent(content); - + return memoRepository.save(memo); } }