Skip to content

Commit

Permalink
Merge pull request #50 from Domitory-CheckMate/feature/49-post
Browse files Browse the repository at this point in the history
[feat] 정렬기준 추가
  • Loading branch information
RyuKwanKon authored Jan 15, 2024
2 parents 46bf908 + 632a43c commit fdaaf4f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
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);
}
}

0 comments on commit fdaaf4f

Please sign in to comment.