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

Let filter be ignored if they do not contain a filter value #43

Merged
merged 1 commit into from
Sep 17, 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 @@ -2,13 +2,15 @@

import jakarta.persistence.metamodel.SingularAttribute;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Singular;
import lombok.With;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;

/**
* A filter, which can be used to filter a query (remove elements from the result).
Expand Down Expand Up @@ -81,10 +83,17 @@ public static Filter attributeIs( final SingularAttribute<?, ? extends Comparabl
* @param qb the query builder
*/
public void apply( final QueryBuilder qb ) {
if ( values.size() > 1 ) {
qb.andWhere( qb.isIn( attribute, values ) );
} else {
qb.andWhere( qb.equalTo( attribute, values.get( 0 ) ) );
final List<? extends Comparable<?>> filterValues = values.stream().filter( Objects::nonNull ).toList();
if ( filterValues.size() > 1 ) {
qb.andWhere( qb.isIn( attribute, filterValues ) );
} else if ( filterValues.size() == 1 ) {
qb.andWhere( qb.equalTo( attribute, filterValues.get( 0 ) ) );
}
}

public boolean isEmpty() {
return values.isEmpty() || values.stream()
.allMatch( v -> Objects.isNull( v ) || (v instanceof final CharSequence cs && !StringUtils.hasText(
cs )) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import jakarta.persistence.Transient;
import jakarta.persistence.metamodel.SingularAttribute;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
import lombok.Builder;
Expand Down Expand Up @@ -44,8 +46,8 @@ public class PageRequest<E> {
/**
* The filters to apply to the query (removing results)
*/
@Singular
private final List<Filter> filters;
@Builder.Default
private final List<Filter> filters = new LinkedList<>();

/**
* The filter rules to apply to the query (removing results). Note that filter rules are <i>not</i> passed to the
Expand Down Expand Up @@ -120,6 +122,38 @@ public PageRequestBuilder<E> desc( final Attribute attribute ) {
return addPosition( Position.create( b -> b.attribute( attribute ).order( Order.DESC ) ) );
}

/**
* Add a filter to the request. Filter which do not contain a filter value or empty char-sequences as values are
* silently ignored for convenience reasons when creating page requests out of query parameters.
*
* @param filter A new filter definition
* @return the builder
*/
public PageRequestBuilder<E> filter( final Filter filter ) {
if ( this.filters$value == null ) {
this.filters$value = new LinkedList<>();
}
if ( filter != null && !filter.isEmpty() ) {
this.filters$value.add( filter );
this.filters$set = true;
}
return this;
}

/**
* Add a list of filters to the page request. Filter which do not contain a filter value or empty char-sequences
* as values are silently ignored for convenience reasons when creating page requests out of query parameters.
*
* @param filters the list of filters to be added
* @return the builder
*/
public PageRequestBuilder<E> filters( final Collection<Filter> filters ) {
if ( filters != null ) {
filters.forEach( this::filter );
}
return this;
}

private PageRequestBuilder<E> addPosition( final Position pos ) {
if ( this.positions == null ) {
this.positions = new ArrayList<>( 3 );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.vigier.cursorpaging.jpa;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;

public class PageRequestTest {

@ParameterizedTest
@NullAndEmptySource
@ValueSource( strings = { " ", "\t", "\n" } )
void shouldIgnoreEmptyFilter( final String value ) {
final var pageRequest = PageRequest.create( b -> b.asc( Attribute.of( "id", Long.class ) )
.filter( Filter.create( f -> f.attribute( Attribute.of( "test", String.class ) ).value( value ) ) ) );

assertThat( pageRequest.filters() ).isEmpty();
}

@Test
void shouldAddPositionAndFilterIfValuePresent() {
final var pageRequest = PageRequest.create( b -> b.asc( Attribute.of( "id", Long.class ) )
.filter( Filter.create( f -> f.attribute( Attribute.of( "test", String.class ) ).value( "value" ) ) ) );

assertThat( pageRequest.filters() ).hasSize( 1 );
assertThat( pageRequest.positions() ).hasSize( 1 ).first().satisfies( p -> {
assertThat( p.attribute().name() ).isEqualTo( "id" );
assertThat( p.order() ).isEqualTo( Order.ASC );
} );
}
}
Loading