I am implementing a Repository which implements QuerydslBinderCustomizer to help create Predicates from the url filter parameters. It is as such
@NoRepositoryBean
public interface TableRepository<T extends TableEntity, S extends EntityPathBase<T>> extends PagingAndSortingRepository<T, UUID>, QuerydslPredicateExecutor<T>, QuerydslBinderCustomizer<S> {
@Override
default void customize(@NonNull QuerydslBindings bindings, @NonNull S root) {
bindings.bind(String.class).all((MultiValueBinding<StringPath, String>) (path, values) -> {
BooleanBuilder predicate = new BooleanBuilder();
values.forEach(value -> predicate.or(path.containsIgnoreCase(value)));
return Optional.of(predicate);
});
}
}
My individual entity repositories are extending from this like,
public interface UserRepository extends TableRepository<User, QUser> {
}
This gives rise to an endpoint http://localhost:port/users
. Things are working fine and I can make requests such as http://localhost:port/users?name="somename"
.
Now, I am implementing soft deletes and I don't want to return soft deleted records on querying. I was planning on editing the customize
method for this like
default void customize(@NonNull QuerydslBindings bindings, @NonNull S root) {
bindings.bind(String.class).all((MultiValueBinding<StringPath, String>) (path, values) -> {
BooleanBuilder predicate = new BooleanBuilder();
values.forEach(value -> predicate.or(path.containsIgnoreCase(value)));
// If path does not have delete flag, add deleted=false to the predicate
return Optional.of(predicate);
});
}
But the issue arises when a call is made to the endpoint without any parameter i.e. at http://localhost:port/users
. In this case bindings.bind()
gets executed but BooleanBuilder predicate = new BooleanBuilder();
part is not. Thus I am unable to add the required predicate to filter out soft deleted records. How to proceed?
I might be doing the filtering of deleted records the wrong way. Is there a better alternative?
EDIT: As requested, I am linking a basic repository https://github.com/SayakMukhopadhyay/demo-querydsl