Реализация разбиения на страницы и сортировки в ReactiveMongoRepository с помощью динамического запроса c - PullRequest
0 голосов
/ 15 апреля 2020

Я знаю, что нумерация страниц несколько противоречит принципам реагирования, но из-за требований я должен как-то заставить это работать. Я использую Spring Data 2.1.6 и не могу обновить, поэтому ReactiveQuerydslSpecification для динамического запроса c не может быть и речи. Я подумал, что могу использовать ReactiveMongoTemplate, поэтому придумал следующее:

public interface IPersonRepository extends ReactiveMongoRepository<Person, String>, IPersonFilterRepository {
    Flux<Person> findAllByCarId(String carId);
}

public interface IPersonFilterRepository {
    Flux<Person> findAllByCarIdAndCreatedDateBetween(String carId, PersonStatus status,
                                                             OffsetDateTime from, OffsetDateTime to,
                                                             Pageable pageable);
}

@Repository
public class PersonFilterRepository implements IPersonFilterRepository {

    @Autowired
    private ReactiveMongoTemplate reactiveMongoTemplate;

    @Override
    public Flux<Person> findAllByCarIdAndCreatedDateBetween(String carId, PersonStatus status,
                                                                   OffsetDateTime from, OffsetDateTime to,
                                                                   Pageable pageable) {
        Query query = new Query(Criteria.where("carId").is(carId));

        if (status != null) {
            query.addCriteria(Criteria.where("status").is(status));
        }

        OffsetDateTime maxLimit = OffsetDateTime.now(ZoneOffset.UTC).minusMonths(3).withDayOfMonth(1); // beginning of month
        if (from == null || from.isBefore(maxLimit)) {
            from = maxLimit;
        }

        query.addCriteria(Criteria.where("createdDateTime").gte(from));

        if (to == null) {
            to = OffsetDateTime.now(ZoneOffset.UTC);
        }

        query.addCriteria(Criteria.where("createdDateTime").lte(to));

        // problem is trying to come up with a decent page-ish behavior compatible with Flux
        /*return reactiveMongoTemplate.count(query, Person.class)
                .flatMap(count -> reactiveMongoTemplate.find(query, Person.class)
                        .flatMap(p -> new PageImpl<Person>(p, pageable, count))
                        .collectList()
                        .map());*/

        /* return reactiveMongoTemplate.find(query, Person.class)
                .buffer(pageable.getPageSize(), pageable.getPageNumber() + 1)
                //.elementAt(pageable.getPageNumber(), new ArrayList<>())
                .thenMany(Flux::from);*/
    }

Я пытался вернуть Page<Person> (предполагая, что один этот метод может быть нереактивным, на этот раз) и происходит сбой со следующей ошибкой во время выполнения тестирования (контекст Spring не загружается успешно из-за: InvalidDataAccessApiUsageException: 'IDocumentFilterRepository.findAllByCustomerIdAndCreatedDateBetween' must not use sliced or paged execution. Please use Flux.buffer(size, skip). Я также попытался вернуть Mono<Page<Person>>, а затем завершился неудачно с "Method has to use a either multi-item reactive wrapper return type or a wrapped Page/Slice type. Offending method: 'IDocumentFilterRepository.findAllByCustomerIdAndCreatedDateBetween', поэтому я предполагаю, что мой единственный вариант - возвращать Flux, согласно Пример 133, фрагмент 3

1 Ответ

0 голосов
/ 22 апреля 2020

Оказывается, вы можете просто добавить следующее к объекту запроса:

query.with(pageable);

reactiveMongoTemplate.find(query, Person.class);

Возврат Flux<T>, и оно будет работать из коробки.

...