Skip to content
Merged
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
35 changes: 9 additions & 26 deletions src/main/java/goorm/ddok/player/service/ProfileSearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -46,38 +47,20 @@ public Page<ProfileSearchResponse> searchPlayers(String keyword, int page, int s
page = Math.max(page, 0);
size = (size <= 0) ? 10 : size;

Pageable pageable = PageRequest.of(page, size);
Sort sort = Sort.by(
Sort.Order.asc("nickname"),
Sort.Order.asc("id")
);
Pageable pageable = PageRequest.of(page, size, sort);

Specification<User> spec = Specification
.where(orderByNicknameAscCaseInsensitive())
.and(hasText(keyword) ? keywordSpec(keyword)
: (r, q, cb) -> cb.isTrue(r.get("isPublic")));
Specification<User> spec = hasText(keyword)
? keywordSpec(keyword)
: (r, q, cb) -> cb.isTrue(r.get("isPublic"));

Page<User> rows = userRepository.findAll(spec, pageable);
return rows.map(u -> toResponse(u, currentUserId));
}

private Specification<User> orderByNicknameAscCaseInsensitive() {
return (root, query, cb) -> {
Class<?> rt = Objects.requireNonNull(query).getResultType();
if (rt != Long.class && rt != long.class) {
HibernateCriteriaBuilder hcb = (HibernateCriteriaBuilder) cb;

var nickname = root.get("nickname");

JpaOrder byNickname = (JpaOrder) hcb.asc(nickname);
byNickname.nullPrecedence(NullPrecedence.LAST);

var lowerNick = hcb.lower(hcb.coalesce(nickname, "").asString());
JpaOrder byLowerNick = (JpaOrder) hcb.asc(lowerNick);

query.orderBy(byNickname, byLowerNick);
}
return null;
};
}


private Specification<User> keywordSpec(String raw) {
List<String> tokens = splitTokens(raw);

Expand Down