diff --git a/src/main/java/com/aptner/v3/board/common_post/PostSpecification.java b/src/main/java/com/aptner/v3/board/common_post/PostSpecification.java index 4e2355b..3c75246 100644 --- a/src/main/java/com/aptner/v3/board/common_post/PostSpecification.java +++ b/src/main/java/com/aptner/v3/board/common_post/PostSpecification.java @@ -56,6 +56,7 @@ public static Specification hasKeyword(String keyword) public static Specification hasAuthor(Long authorId) { return (root, query, criteriaBuilder) -> { if (authorId != null) { + log.debug("specification - authorId : {}", authorId); return criteriaBuilder.equal(root.get("member").get("id"), authorId); } else { return criteriaBuilder.conjunction(); @@ -66,6 +67,7 @@ public static Specification hasAuthor(Long authorId) { public static Specification isDuty(Boolean isDuty) { return (root, query, criteriaBuilder) -> { if (isDuty != null) { + log.debug("specification - isDuty : {}", isDuty); return criteriaBuilder.equal(root.get("isDuty"), isDuty); } else { return criteriaBuilder.conjunction(); diff --git a/src/main/java/com/aptner/v3/board/common_post/service/CommonPostService.java b/src/main/java/com/aptner/v3/board/common_post/service/CommonPostService.java index 2140074..dec6d95 100644 --- a/src/main/java/com/aptner/v3/board/common_post/service/CommonPostService.java +++ b/src/main/java/com/aptner/v3/board/common_post/service/CommonPostService.java @@ -75,12 +75,23 @@ public Page getPostListWithComment(BoardGroup boardGroup, Long categoryId, St } private static Specification geteSpecification(BoardGroup boardGroup, Long categoryId, String keyword, Status status, Long userId) { - Specification spec = (Specification) Specification - .where(PostSpecification.hasBoardGroup(boardGroup)) - .and(PostSpecification.hasCategoryId(categoryId)) - .and(PostSpecification.hasKeyword(keyword)) - .and(PostSpecification.hasStatus(status)) - .and(PostSpecification.hasAuthor(userId)); + Specification spec = Specification.where(PostSpecification.hasBoardGroup(boardGroup)); + + if (categoryId != null && categoryId > 0) { + spec = spec.and(PostSpecification.hasCategoryId(categoryId)); + } + + if (keyword != null && !keyword.isEmpty()) { + spec = spec.and(PostSpecification.hasKeyword(keyword)); + } + + if (status != null) { + spec = spec.and(PostSpecification.hasStatus(status)); + } + + if (userId != null) { + spec = spec.and(PostSpecification.hasAuthor(userId)); + } return spec; } diff --git a/src/main/java/com/aptner/v3/board/free_post/FreePostService.java b/src/main/java/com/aptner/v3/board/free_post/FreePostService.java index b0dd149..88bcf3f 100644 --- a/src/main/java/com/aptner/v3/board/free_post/FreePostService.java +++ b/src/main/java/com/aptner/v3/board/free_post/FreePostService.java @@ -49,33 +49,68 @@ public Page getPostList(BoardGroup boardGroup, Long categoryId, Str return super.getPostList(boardGroup, categoryId, keyword, status, userId, pageable); } + /** + * 기본 조회 + * */ public Page getPostListWithoutHotPost(BoardGroup boardGroup, Long categoryId, String keyword, Status status, Long userId, Pageable pageable) { return super.getPostList(boardGroup, categoryId, keyword, status, userId, pageable); } + /** + * 인기글 포함된 조회 + * */ private Page getFirstPageWithTopPosts(BoardGroup boardGroup, Long categoryId, String keyword, Status status, Long userId, Pageable pageable) { LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7); - Specification spec = Specification - .where(PostSpecification.hasBoardGroup(boardGroup)) - .and(PostSpecification.hasCategoryId(categoryId)) - .and(PostSpecification.hasKeyword(keyword)) - .and(PostSpecification.hasStatus(status)) - .and(PostSpecification.hasAuthor(userId)); +// Specification spec = Specification +// .where(PostSpecification.hasBoardGroup(boardGroup)) +// .and(PostSpecification.hasCategoryId(categoryId)) +// .and(PostSpecification.hasKeyword(keyword)) +// .and(PostSpecification.hasStatus(status)) +// .and(PostSpecification.hasAuthor(userId)); + + Specification spec = Specification.where(PostSpecification.hasBoardGroup(boardGroup)); + + if (categoryId != null && categoryId > 0) { + log.debug("인기글 조건(categoryId) : {}", categoryId); + spec = spec.and(PostSpecification.hasCategoryId(categoryId)); + } + + if (keyword != null && !keyword.isEmpty()) { + log.debug("인기글 조건(keyword) : {}", keyword); + spec = spec.and(PostSpecification.hasKeyword(keyword)); + } + + if (status != null) { + log.debug("인기글 조건(status) : {}", status); + spec = spec.and(PostSpecification.hasStatus(status)); + } + + if (userId != null) { + log.debug("인기글 조건(userId) : {}", userId); + spec = spec.and(PostSpecification.hasAuthor(userId)); + } // 상위 3개 ORDER BY (p.hits + p.reactionColumns.countReactionTypeGood) DESC List topPosts = commonPostRepository.findTopPosts(sevenDaysAgo, boardGroup.getTable(), PageRequest.of(0, 3)); List topPostIds = topPosts.stream().map(CommonPost::getId).collect(Collectors.toList()); - Page remainingPosts = commonPostRepository.findAll(Specification.where(spec) - .and((root, query, criteriaBuilder) -> root.get("id").in(topPostIds).not()), PageRequest.of(0, pageable.getPageSize() - topPosts.size(), pageable.getSort())); + Page remainingPosts = null; + if (!topPostIds.isEmpty()) { + // + remainingPosts = commonPostRepository.findAll(Specification.where(spec) + .and((root, query, criteriaBuilder) -> root.get("id").in(topPostIds).not()), PageRequest.of(0, pageable.getPageSize() - topPosts.size(), pageable.getSort())); + } else { + remainingPosts = commonPostRepository.findAll(Specification.where(spec), PageRequest.of(0, pageable.getPageSize() - topPosts.size(), pageable.getSort())); + } List combinedPosts = Stream.concat( topPosts.stream().map(this::toFreePostDto), remainingPosts.getContent().stream().map(FreePost::toDto) ).collect(Collectors.toList()); + log.debug("인기글 : {} 나머지글 : {} -> 전체 : {}", topPosts.size(), remainingPosts.getContent().size(), combinedPosts.size()); return new PageImpl<>(combinedPosts, pageable, remainingPosts.getTotalElements() + topPosts.size()); } diff --git a/src/test/java/com/aptner/v3/board/common_post/controller/FreePostControllerTest.java b/src/test/java/com/aptner/v3/board/common_post/controller/FreePostControllerTest.java index a0fab54..a73c5ba 100644 --- a/src/test/java/com/aptner/v3/board/common_post/controller/FreePostControllerTest.java +++ b/src/test/java/com/aptner/v3/board/common_post/controller/FreePostControllerTest.java @@ -1,8 +1,8 @@ package com.aptner.v3.board.common_post.controller; -import com.aptner.v3.board.common_post.dto.CommonPostDto; import com.aptner.v3.board.common_post.CommonPostRepository; import com.aptner.v3.board.common_post.domain.CommonPost; +import com.aptner.v3.board.common_post.dto.CommonPostDto; import com.aptner.v3.board.common_post.service.CommonPostService; import com.aptner.v3.board.free_post.domain.FreePost; import com.fasterxml.jackson.databind.ObjectMapper; @@ -11,7 +11,6 @@ import net.minidev.json.JSONObject; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -79,7 +78,6 @@ void setUp() throws Exception { prefix = "http://localhost:" + port; } - @Disabled @WithUserDetails(value="user1") @Test void 자유_게시판_전체_조회() throws Exception {