microsoft / spring-data-cosmosdb

Access data with Azure Cosmos DB
MIT License
94 stars 64 forks source link

add sort sample and readme #174

Open sophiaso opened 6 years ago

jyotsnaravikumar commented 4 years ago

@sophiaso : I have this sample working , i would like to do a PR on this docs. Please let me know which docs

sophiaso commented 4 years ago

@kushagraThapar Could you help?

kushagraThapar commented 4 years ago

@sophiaso sure, I can help :) @jyotsnaravikumar how big is the sample ? Is it a project ?

jyotsnaravikumar commented 4 years ago

It is part of our project. @sophiaso @kushagraThapar This is the working and tested sample

 public List<Movie> getAllMovies(Optional<String> query, Integer pageNo, Integer pageSize, Sort sort) {
        final Pageable pageable = new DocumentDbPageRequest(pageNo, pageSize, null, sort);
        List<Movie> content = null;
        Page<Movie> page = null;
        if (query.isPresent() && !StringUtils.isEmpty(query.get())) {
            page = repository.findByTextSearchContaining(query.get().toLowerCase(), pageNo, pageSize);
        } else {
            page = repository.findAll(pageable);
        }

        if (pageNo == 0) {
            content = page.getContent();
            for (Movie movie : content) System.out.println(movie.toString());
            return content;
        } else {
            Page<Movie> nextPage = null;
            for (int i = 1; i <= pageNo; i++) {
                nextPage = this.repository.findAll(page.getPageable());
                /* reset page to nextpage like a linkedlist*/
                page = nextPage;
                content = nextPage.getContent();
                for (Movie movie : content) System.out.println(movie.toString());
            }
            return content;
        }
    }
kushagraThapar commented 4 years ago

@jyotsnaravikumar Thanks for the example, but this example uses the older version of spring-data-cosmosdb - v2.1.x We have now moved to v2.2.x DocumentDbPageRequest has been deprecated. On the readme file, there is a small paging example. If you want, you can update that with a more generic paging and sorting example, something like above. Please assign that PR to me and I will review it.