diff --git a/src/main/java/com/ewhatever/qna/post/repository/PostRepository.java b/src/main/java/com/ewhatever/qna/post/repository/PostRepository.java index 3056a69..58d4b8a 100644 --- a/src/main/java/com/ewhatever/qna/post/repository/PostRepository.java +++ b/src/main/java/com/ewhatever/qna/post/repository/PostRepository.java @@ -7,13 +7,11 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; - @Repository public interface PostRepository extends JpaRepository { Page findAllByIsJuicy(Pageable pageable, Boolean isJuicy); Boolean existsByCategory(Category category); - Page findByCategory(Category category, Pageable pageable); + Page findAllByCategoryAndIsJuicy(Category category, Pageable pageable); Long countByQuestioner_UserIdxAndStatusEquals(Long userIdx, String status); Page findByQuestioner_UserIdxAndIsJuicyAndStatusEquals(Long userIdx, Boolean isJuicy, String status, Pageable pageable); } diff --git a/src/main/java/com/ewhatever/qna/post/service/PostService.java b/src/main/java/com/ewhatever/qna/post/service/PostService.java index 69e0a72..052a323 100644 --- a/src/main/java/com/ewhatever/qna/post/service/PostService.java +++ b/src/main/java/com/ewhatever/qna/post/service/PostService.java @@ -84,19 +84,19 @@ private List getCardList(Post post) { public Page getPostsByCategory(String category, Pageable page) throws BaseException { try { Category categoryName = Category.valueOf(category.toUpperCase()); - if (categoryName != null) { //TODO: 삭제 필요. IllegalArgumentException 처리 필요. - boolean postExists = postRepository.existsByCategory(categoryName); - if (postExists) { - Page postPage = postRepository.findByCategory(categoryName, page); //TODO: 쥬시글 여부 추가 - return postPage.map(post -> new GetPostsRes( - post.getCategory().name(), - post.getLastModifiedDate(), - post.getScrapCount(), - post.getCommentCount(), - getCardList(post) // 질문제목, 질문상세, 답변(0-3개) - )); - } else throw new BaseException(NULL_POST); - } else throw new BaseException(INVALID_CATEGORY); + boolean postExists = postRepository.existsByCategory(categoryName); + if (postExists) { + Page postPage = postRepository.findAllByCategoryAndIsJuicy(categoryName, page); + return postPage.map(post -> new GetPostsRes( + post.getCategory().name(), + post.getLastModifiedDate(), + post.getScrapCount(), + post.getCommentCount(), + getCardList(post) // 질문제목, 질문상세, 답변(0-3개) + )); + } else throw new BaseException(NULL_POST); + } catch (IllegalArgumentException e) { + throw new BaseException(INVALID_CATEGORY); } catch (BaseException e) { throw e; } catch (Exception e) { @@ -146,8 +146,7 @@ private List getCommentList(Post post, User user) { // 댓글 작성자 여부 private Boolean isWriter(User user, Comment comment) { - if (comment.getWriter().equals(user)) return true; - else return false; + return comment.getWriter().equals(user); } // 댓글 작성자 @@ -173,7 +172,7 @@ public void scrapPost(Long postIdx, Long userIdx) throws BaseException { if (existsByPostAndUser) { // 스크랩 존재 Scrap scrap = scrapRepository.findByPostAndUser(post, user); if (scrap.getStatus().equals(ACTIVE)) { // ACTIVE -> INACTIVE - Long currentScrapCount = Optional.ofNullable(post.getScrapCount()).orElse(0L); + long currentScrapCount = Optional.ofNullable(post.getScrapCount()).orElse(0L); if (currentScrapCount > 0) { post.setScrapCount(currentScrapCount - 1L); } else throw new BaseException(ZERO_SCRAP_COUNT);