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

[feat] 정렬기준 추가 #50

Merged
merged 4 commits into from
Jan 15, 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 @@ -41,6 +41,7 @@ public Optional<PostDetailDto> findPostDetail(Long postId) {
.where(
eqPostId(postId)
)
.orderBy(post.id.desc())
.fetchOne());
}

Expand All @@ -67,6 +68,7 @@ public Page<PostSearchDto> searchPosts(PostSearchCondition condition) {
eqGenderType(condition.genderType()),
validateUserState()
)
.orderBy(post.id.desc())
.fetch();

JPAQuery<Post> countQuery = queryFactory
Expand Down Expand Up @@ -106,6 +108,7 @@ public Page<PostSearchDto> searchPostsWithPaging(PostPagingSearchCondition condi
)
.offset(condition.pageable().getOffset())
.limit(condition.pageable().getPageSize())
.orderBy(post.id.desc())
.fetch();

JPAQuery<Post> countQuery = queryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum PostSortType implements EnumField {
DATE("1", "register"),
REMAIN_DATE("2", "remain date"),
ACCURACY("3", "accuracy");
ACCURACY("1", "accuracy"),
REGISTER("2", "register"),
REMAIN_DATE("3", "remain date"),
SCRAP("4", "scrap");

private String code;
private String desc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,33 @@
public class PostSortingUtils {
public static void sortByTypeForSearchResults(List<PostSearchElementResponseDto> posts, PostSortType postSortType) {
if (PostSortType.ACCURACY.equals(postSortType))
sortByAccuracyType(posts);
sortByAccuracy(posts);
else if (PostSortType.REMAIN_DATE.equals(postSortType))
sortByRemainDate(posts);
else if (PostSortType.SCRAP.equals(postSortType))
sortByScrapCount(posts);

}

private static void sortByAccuracyType(List<PostSearchElementResponseDto> posts) {
posts.sort(Comparator.comparingInt(PostSearchElementResponseDto::accuracy));
private static void sortByAccuracy(List<PostSearchElementResponseDto> posts) {
posts.sort(Comparator.comparingInt(PostSearchElementResponseDto::accuracy).reversed()
.thenComparing(dto -> filterRegisterDate(dto.remainDate())));
}

private static void sortByRemainDate(List<PostSearchElementResponseDto> posts) {
posts.sort(Comparator.comparingInt(PostSearchElementResponseDto::remainDate));
posts.sort(Comparator.comparingInt(dto -> filterPositiveRemainDate(dto.remainDate())));
}

private static void sortByScrapCount(List<PostSearchElementResponseDto> posts) {
posts.sort(Comparator.comparingInt(PostSearchElementResponseDto::scrapCount).reversed()
.thenComparing(dto -> filterRegisterDate(dto.remainDate())));
}

private static int filterPositiveRemainDate(int remainDate) {
return remainDate < 0 ? Integer.MAX_VALUE : remainDate;
}

private static int filterRegisterDate(int remainDate) {
return remainDate < 0 ? Integer.MAX_VALUE : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Page<PostSearchDto> searchMyScrapPosts(ScrapSearchCondition condition) {
eqUserId(condition.userId()),
validateUserState()
)
.orderBy(post.id.desc())
.offset(condition.pageable().getOffset())
.limit(condition.pageable().getPageSize())
.fetch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

@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)
public static <T> List<T> convertPaging(List<T> dataList, long offset, int size) {
if (dataList.size() <= offset)
throw new InvalidValueException(INVALID_PAGING_SIZE);
int startIndex = (int) page * size;
int endIndex = Math.min(dataList.size(), (int) (page + 1) * size);
int startIndex = (int) offset;
int endIndex = Math.min(dataList.size(), startIndex + size);
return dataList.subList(startIndex, endIndex);
}
}