Skip to content

Commit

Permalink
return users when search user keyword is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed Sep 7, 2017
1 parent b9bca87 commit ec448a1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Expand Up @@ -11,7 +11,9 @@
*/
public interface UserRepository extends PagingAndSortingRepository<UserPO, Long> {

List<UserPO> findByUsernameLike(String username);
List<UserPO> findFirst20ByEnabled(int enabled);

List<UserPO> findByUsernameLikeAndEnabled(String username, int enabled);

UserPO findByUsername(String username);
}
Expand Up @@ -18,7 +18,6 @@
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -58,18 +57,19 @@ public void createOrUpdate(User user) {

@Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit) {
List<UserPO> users;
if (StringUtils.isEmpty(keyword)) {
return Collections.emptyList();
users = userRepository.findFirst20ByEnabled(1);
} else {
users = userRepository.findByUsernameLikeAndEnabled("%" + keyword + "%", 1);
}

List<UserPO> userPOs = userRepository.findByUsernameLike("%" + keyword + "%");

List<UserInfo> result = Lists.newArrayList();
if (CollectionUtils.isEmpty(userPOs)) {
if (CollectionUtils.isEmpty(users)) {
return result;
}

result.addAll(userPOs.stream().map(UserPO::toUserInfo).collect(Collectors.toList()));
result.addAll(users.stream().map(UserPO::toUserInfo).collect(Collectors.toList()));

return result;
}
Expand Down

0 comments on commit ec448a1

Please sign in to comment.