Skip to content

Commit

Permalink
Merge pull request #145 from fastcampus-711/develop
Browse files Browse the repository at this point in the history
[정기배포]240609-1
  • Loading branch information
having-dlrow authored Jun 9, 2024
2 parents fdbf4a9 + e4218a3 commit e3d8c92
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static <E extends CommonPost> Specification<E> hasKeyword(String keyword)
public static <E extends CommonPost> Specification<E> 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();
Expand All @@ -66,6 +67,7 @@ public static <E extends CommonPost> Specification<E> hasAuthor(Long authorId) {
public static <E extends CommonPost> Specification<E> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,23 @@ public Page<T> getPostListWithComment(BoardGroup boardGroup, Long categoryId, St
}

private static <E extends CommonPost> Specification<E> geteSpecification(BoardGroup boardGroup, Long categoryId, String keyword, Status status, Long userId) {
Specification<E> spec = (Specification<E>) Specification
.where(PostSpecification.hasBoardGroup(boardGroup))
.and(PostSpecification.hasCategoryId(categoryId))
.and(PostSpecification.hasKeyword(keyword))
.and(PostSpecification.hasStatus(status))
.and(PostSpecification.hasAuthor(userId));
Specification<E> 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;
}

Expand Down
51 changes: 43 additions & 8 deletions src/main/java/com/aptner/v3/board/free_post/FreePostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,68 @@ public Page<FreePostDto> getPostList(BoardGroup boardGroup, Long categoryId, Str
return super.getPostList(boardGroup, categoryId, keyword, status, userId, pageable);
}

/**
* 기본 조회
* */
public Page<FreePostDto> getPostListWithoutHotPost(BoardGroup boardGroup, Long categoryId, String keyword, Status status, Long userId, Pageable pageable) {
return super.getPostList(boardGroup, categoryId, keyword, status, userId, pageable);
}

/**
* 인기글 포함된 조회
* */
private Page<FreePostDto> getFirstPageWithTopPosts(BoardGroup boardGroup, Long categoryId, String keyword, Status status, Long userId, Pageable pageable) {

LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7);

Specification<FreePost> spec = Specification
.where(PostSpecification.<FreePost>hasBoardGroup(boardGroup))
.and(PostSpecification.hasCategoryId(categoryId))
.and(PostSpecification.hasKeyword(keyword))
.and(PostSpecification.hasStatus(status))
.and(PostSpecification.hasAuthor(userId));
// Specification<FreePost> spec = Specification
// .where(PostSpecification.<FreePost>hasBoardGroup(boardGroup))
// .and(PostSpecification.hasCategoryId(categoryId))
// .and(PostSpecification.hasKeyword(keyword))
// .and(PostSpecification.hasStatus(status))
// .and(PostSpecification.hasAuthor(userId));

Specification<FreePost> 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<FreePost> topPosts = commonPostRepository.findTopPosts(sevenDaysAgo, boardGroup.getTable(), PageRequest.of(0, 3));
List<Long> topPostIds = topPosts.stream().map(CommonPost::getId).collect(Collectors.toList());

Page<FreePost> 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<FreePost> 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<FreePostDto> 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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -79,7 +78,6 @@ void setUp() throws Exception {
prefix = "http://localhost:" + port;
}

@Disabled
@WithUserDetails(value="user1")
@Test
void 자유_게시판_전체_조회() throws Exception {
Expand Down

0 comments on commit e3d8c92

Please sign in to comment.