diff --git a/src/main/java/org/gachon/checkmate/domain/post/controller/PostController.java b/src/main/java/org/gachon/checkmate/domain/post/controller/PostController.java index 6247135..f64fdfd 100644 --- a/src/main/java/org/gachon/checkmate/domain/post/controller/PostController.java +++ b/src/main/java/org/gachon/checkmate/domain/post/controller/PostController.java @@ -15,11 +15,11 @@ public class PostController { private final PostService postService; - @GetMapping("/search") - public ResponseEntity> searchTextPost(@UserId final Long userId, - @RequestParam final String text, - final Pageable pageable) { - final PostSearchResponseDto responseDto = postService.searchTextPost(userId, text, pageable); + @GetMapping + public ResponseEntity> getAllPosts(@UserId final Long userId, + @RequestParam final String type, + final Pageable pageable){ + final PostSearchResponseDto responseDto = postService.getAllPosts(userId, type, pageable); return SuccessResponse.ok(responseDto); } @@ -31,4 +31,12 @@ public ResponseEntity> searchKeyWordPost(@UserId final Long u final PostSearchResponseDto responseDto = postService.searchKeyWordPost(userId, key, type, pageable); return SuccessResponse.ok(responseDto); } + + @GetMapping("/search") + public ResponseEntity> searchTextPost(@UserId final Long userId, + @RequestParam final String text, + final Pageable pageable) { + final PostSearchResponseDto responseDto = postService.searchTextPost(userId, text, pageable); + return SuccessResponse.ok(responseDto); + } } diff --git a/src/main/java/org/gachon/checkmate/domain/post/repository/PostQuerydslRepository.java b/src/main/java/org/gachon/checkmate/domain/post/repository/PostQuerydslRepository.java index c62ec9e..8158e65 100644 --- a/src/main/java/org/gachon/checkmate/domain/post/repository/PostQuerydslRepository.java +++ b/src/main/java/org/gachon/checkmate/domain/post/repository/PostQuerydslRepository.java @@ -24,6 +24,29 @@ public class PostQuerydslRepository { private final JPAQueryFactory queryFactory; + public Page findAllPosts(Pageable pageable) { + List content = queryFactory + .select(new QPostSearchDto( + post.title, + post.content, + post.importantKeyType, + post.similarityKeyType, + post.endDate, + post.scrapList.size(), + postCheckList + )) + .from(post) + .leftJoin(post.postCheckList, postCheckList) + .where( + validatePostDate() + ) + .fetch(); + + JPAQuery countQuery = queryFactory + .selectFrom(post); + return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchCount); + } + public Page searchKeyPost(ImportantKeyType importantKeyType, Pageable pageable) { List content = queryFactory .select(new QPostSearchDto( diff --git a/src/main/java/org/gachon/checkmate/domain/post/service/PostService.java b/src/main/java/org/gachon/checkmate/domain/post/service/PostService.java index 48e3757..34c1054 100644 --- a/src/main/java/org/gachon/checkmate/domain/post/service/PostService.java +++ b/src/main/java/org/gachon/checkmate/domain/post/service/PostService.java @@ -11,7 +11,6 @@ import org.gachon.checkmate.domain.post.entity.SortType; import org.gachon.checkmate.domain.post.repository.PostQuerydslRepository; import org.gachon.checkmate.global.error.exception.EntityNotFoundException; -import org.gachon.checkmate.global.utils.EnumValueUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -19,7 +18,6 @@ import java.time.LocalDate; import java.time.temporal.ChronoUnit; -import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Objects; @@ -37,6 +35,17 @@ public class PostService { private final CheckListRepository checkListRepository; private final PostQuerydslRepository postQuerydslRepository; + public PostSearchResponseDto getAllPosts(Long userId, String type, Pageable pageable) { + CheckList checkList = getCheckList(userId); + SortType sortType = toEntityCode(SortType.class, type); + Page postSearchList = getAllPostsResults(pageable); + List searchResults = createPostSearchResponseDto(postSearchList, checkList); + sortByTypeForSearchResults(searchResults, Objects.requireNonNull(sortType)); + List pagingSearchResults + = convertPaging(searchResults, pageable.getOffset(), pageable.getPageSize()); + return PostSearchResponseDto.of(pagingSearchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements()); + } + public PostSearchResponseDto searchKeyWordPost(Long userId, String key, String type, Pageable pageable) { CheckList checkList = getCheckList(userId); SortType sortType = toEntityCode(SortType.class, type); @@ -100,6 +109,10 @@ private int getRemainDate(LocalDate endDate) { return (int) endDate.until(LocalDate.now(), ChronoUnit.DAYS); } + private Page getAllPostsResults(Pageable pageable) { + return postQuerydslRepository.findAllPosts(pageable); + } + private Page getKeySearchResults(ImportantKeyType importantKeyType, Pageable pageable) { return postQuerydslRepository.searchKeyPost(importantKeyType, pageable); }