Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] keyword 중심 search 정렬 기준 반영 #16

Merged
merged 6 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public ResponseEntity<SuccessResponse<?>> searchTextPost(@UserId final Long user
return SuccessResponse.ok(responseDto);
}

@GetMapping("/search/{key}")
@GetMapping("/{key}")
public ResponseEntity<SuccessResponse<?>> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public Page<PostSearchDto> searchKeyPost(ImportantKeyType importantKeyType, Page
containKeyWordCondition(importantKeyType),
validatePostDate()
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
RyuKwanKon marked this conversation as resolved.
Show resolved Hide resolved
.fetch();

JPAQuery<Post> countQuery = queryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<PostSearchDto> postSearchList = getKeySearchResults(importantKeyType, pageable);
List<PostSearchElementResponseDto> searchResults = createPostSearchResponseDto(postSearchList, checkList);
return PostSearchResponseDto.of(searchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements());
sortByTypeForSearchResults(searchResults, Objects.requireNonNull(sortType));
List<PostSearchElementResponseDto> pagingSearchResults
= convertPaging(searchResults, pageable.getOffset(), pageable.getPageSize());
return PostSearchResponseDto.of(pagingSearchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements());
}

public PostSearchResponseDto searchTextPost(Long userId, String text, Pageable pageable) {
Expand All @@ -56,6 +66,21 @@ private List<PostSearchElementResponseDto> createPostSearchResponseDto(Page<Post
.collect(Collectors.toList());
}

private void sortByTypeForSearchResults(List<PostSearchElementResponseDto> posts, SortType sortType) {
if (sortType.equals(SortType.ACCURACY))
sortByAccuracyType(posts);
else if (sortType.equals(SortType.REMAIN_DATE))
sortByRemainDate(posts);
}

private void sortByAccuracyType(List<PostSearchElementResponseDto> posts) {
posts.sort(Comparator.comparingInt(PostSearchElementResponseDto::accuracy));
}

private void sortByRemainDate(List<PostSearchElementResponseDto> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/gachon/checkmate/global/utils/PagingUtils.java
RyuKwanKon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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 <T> List<T> convertPaging(List<T> 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);
}
}