Sobes.tech
Junior — Middle+

User search by filter with ignoring empty parameters

livecode

Task condition

It is necessary to implement the method searchByCriteria in the UserService class, which searches for users in the database according to the provided UserFilter object. When building the query:

  • skip filter fields that are null or empty strings;
  • if both fields (firstName and lastName) are specified, apply them together;
  • use the capabilities of Spring Data JPA through UserRepository;
  • return a list of all found User entities.
public class UserService {

  private final UserRepository userRepo;

  public List<User> searchByCriteria(UserFilter filter) {
  }
}

@RequiredArgsConstructor
@Getter
public class UserFilter {

  private final String firstName;
  private final String lastName;
}

@Entity
public class User {

  @Id
  private Long id;
  private String firstName;
  private String lastName;
}

public interface UserRepository extends JpaRepository<User, Long> {
}