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 42f6465..6247135 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 @@ -23,11 +23,12 @@ public ResponseEntity> searchTextPost(@UserId final Long user return SuccessResponse.ok(responseDto); } - @GetMapping("/search/{key}") + @GetMapping("/{key}") public ResponseEntity> searchKeyWordPost(@UserId final Long userId, @PathVariable final String key, + @RequestParam final String type, final Pageable pageable) { - final PostSearchResponseDto responseDto = postService.searchKeyWordPost(userId, key, pageable); + final PostSearchResponseDto responseDto = postService.searchKeyWordPost(userId, key, type, pageable); return SuccessResponse.ok(responseDto); } } diff --git a/src/main/java/org/gachon/checkmate/domain/post/entity/SortType.java b/src/main/java/org/gachon/checkmate/domain/post/entity/SortType.java new file mode 100644 index 0000000..b1c9182 --- /dev/null +++ b/src/main/java/org/gachon/checkmate/domain/post/entity/SortType.java @@ -0,0 +1,17 @@ +package org.gachon.checkmate.domain.post.entity; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.gachon.checkmate.global.utils.EnumField; + +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +public enum SortType implements EnumField { + DATE("1", "register"), + REMAIN_DATE("2", "remain date"), + ACCURACY("3", "accuracy"); + + private String code; + private String desc; +} 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 5546c8f..c62ec9e 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 @@ -41,8 +41,6 @@ public Page searchKeyPost(ImportantKeyType importantKeyType, Page containKeyWordCondition(importantKeyType), validatePostDate() ) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize()) .fetch(); JPAQuery countQuery = queryFactory 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 32b2898..48e3757 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 @@ -8,6 +8,7 @@ import org.gachon.checkmate.domain.post.dto.response.PostSearchResponseDto; import org.gachon.checkmate.domain.post.dto.support.PostSearchDto; import org.gachon.checkmate.domain.post.entity.ImportantKeyType; +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; @@ -18,10 +19,15 @@ 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; import java.util.stream.Collectors; import static org.gachon.checkmate.global.error.ErrorCode.CHECK_LIST_NOT_FOUND; +import static org.gachon.checkmate.global.utils.EnumValueUtils.toEntityCode; +import static org.gachon.checkmate.global.utils.PagingUtils.convertPaging; @RequiredArgsConstructor @@ -31,12 +37,16 @@ public class PostService { private final CheckListRepository checkListRepository; private final PostQuerydslRepository postQuerydslRepository; - public PostSearchResponseDto searchKeyWordPost(Long userId, String key, Pageable pageable) { + public PostSearchResponseDto searchKeyWordPost(Long userId, String key, String type, Pageable pageable) { CheckList checkList = getCheckList(userId); - ImportantKeyType importantKeyType = EnumValueUtils.toEntityCode(ImportantKeyType.class, key); + SortType sortType = toEntityCode(SortType.class, type); + ImportantKeyType importantKeyType = toEntityCode(ImportantKeyType.class, key); Page postSearchList = getKeySearchResults(importantKeyType, pageable); List searchResults = createPostSearchResponseDto(postSearchList, checkList); - return PostSearchResponseDto.of(searchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements()); + sortByTypeForSearchResults(searchResults, Objects.requireNonNull(sortType)); + List pagingSearchResults + = convertPaging(searchResults, pageable.getOffset(), pageable.getPageSize()); + return PostSearchResponseDto.of(pagingSearchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements()); } public PostSearchResponseDto searchTextPost(Long userId, String text, Pageable pageable) { @@ -56,6 +66,21 @@ private List createPostSearchResponseDto(Page posts, SortType sortType) { + if (sortType.equals(SortType.ACCURACY)) + sortByAccuracyType(posts); + else if (sortType.equals(SortType.REMAIN_DATE)) + sortByRemainDate(posts); + } + + private void sortByAccuracyType(List posts) { + posts.sort(Comparator.comparingInt(PostSearchElementResponseDto::accuracy)); + } + + private void sortByRemainDate(List posts) { + posts.sort(Comparator.comparingInt(PostSearchElementResponseDto::remainDate)); + } + private int getAccuracy(PostCheckList postCheckList, CheckList checkList) { int count = 0; count += getRateForFrequencyElement(postCheckList.getCleanType().getCode(), checkList.getCleanType().getCode(), 4); diff --git a/src/main/java/org/gachon/checkmate/global/error/ErrorCode.java b/src/main/java/org/gachon/checkmate/global/error/ErrorCode.java index 61946f4..625226d 100644 --- a/src/main/java/org/gachon/checkmate/global/error/ErrorCode.java +++ b/src/main/java/org/gachon/checkmate/global/error/ErrorCode.java @@ -13,6 +13,7 @@ public enum ErrorCode { */ BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), INVALID_ENUM_CODE(HttpStatus.BAD_REQUEST, "잘못된 Enum class code 입니다."), + INVALID_PAGING_SIZE(HttpStatus.BAD_REQUEST, "잘못된 Paging 크기입니다."), /** * 401 Unauthorized diff --git a/src/main/java/org/gachon/checkmate/global/utils/PagingUtils.java b/src/main/java/org/gachon/checkmate/global/utils/PagingUtils.java new file mode 100644 index 0000000..81fa48e --- /dev/null +++ b/src/main/java/org/gachon/checkmate/global/utils/PagingUtils.java @@ -0,0 +1,20 @@ +package org.gachon.checkmate.global.utils; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.gachon.checkmate.global.error.exception.InvalidValueException; + +import java.util.List; + +import static org.gachon.checkmate.global.error.ErrorCode.INVALID_PAGING_SIZE; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class PagingUtils { + public static List convertPaging(List dataList, long page, int size) { + if (dataList.size() <= page * size) + throw new InvalidValueException(INVALID_PAGING_SIZE); + int startIndex = (int) page * size; + int endIndex = Math.min(dataList.size(), (int) page * (size + 1)); + return dataList.subList(startIndex, endIndex); + } +}