Skip to content

Commit

Permalink
Merge pull request #19 from whatever-mentoring/fix/3-getPosts
Browse files Browse the repository at this point in the history
[fix/3-getPosts] 쥬시글 목록 조회 API 수정-쥬시글 여부 조건 추가 및 예외 처리
  • Loading branch information
Haeun-Y authored Sep 19, 2023
2 parents 7e35bd1 + 21f6ec6 commit 1d3b12b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post, Long> {
Page<Post> findAllByIsJuicy(Pageable pageable, Boolean isJuicy);
Boolean existsByCategory(Category category);
Page<Post> findByCategory(Category category, Pageable pageable);
Page<Post> findAllByCategoryAndIsJuicy(Category category, Pageable pageable);
Long countByQuestioner_UserIdxAndStatusEquals(Long userIdx, String status);
Page<Post> findByQuestioner_UserIdxAndIsJuicyAndStatusEquals(Long userIdx, Boolean isJuicy, String status, Pageable pageable);
}
31 changes: 15 additions & 16 deletions src/main/java/com/ewhatever/qna/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ private List<String> getCardList(Post post) {
public Page<GetPostsRes> 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<Post> 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<Post> 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) {
Expand Down Expand Up @@ -146,8 +146,7 @@ private List<GetPostRes.CommentDto> 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);
}

// 댓글 작성자
Expand All @@ -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);
Expand Down

0 comments on commit 1d3b12b

Please sign in to comment.