Bael 3653: Adding Pageable Support (#8766)

* Removed the commented code

* Upgrading the Spring Doc Version

* Adding the Pageable Example

In order to use `Pageable`, I had to add `spring-data-jpa` dep and H2. Also, the Spring doc version was quite outdated, so an upgrade for that, too.

* Adding the Pageable Example

In order to use `Pageable`, I had to add `spring-data-jpa` dep and H2. Also, the Spring doc version was quite outdated, so an upgrade for that, too.
This commit is contained in:
Mona Mohamadinia
2020-03-09 19:48:44 +03:30
committed by GitHub
parent eda766c83c
commit a2488e4943
4 changed files with 37 additions and 22 deletions
@@ -6,6 +6,8 @@ import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@@ -41,6 +43,11 @@ public class BookController {
return repository.getBooks();
}
@GetMapping("/filter")
public Page<Book> filterBooks(Pageable pageable) {
return repository.getBooks(pageable);
}
@PutMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public Book updateBook(@PathVariable("id") final String id, @RequestBody final Book book) {
@@ -1,13 +1,14 @@
package com.baeldung.springdoc.repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import com.baeldung.springdoc.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import com.baeldung.springdoc.model.Book;
import java.util.*;
import static java.util.stream.Collectors.toList;
@Repository
public class BookRepository {
@@ -25,4 +26,11 @@ public class BookRepository {
public Collection<Book> getBooks() {
return books.values();
}
public Page<Book> getBooks(Pageable pageable) {
int toSkip = pageable.getPageSize() * pageable.getPageNumber();
List<Book> result = books.values().stream().skip(toSkip).limit(pageable.getPageSize()).collect(toList());
return new PageImpl<>(result, pageable, books.size());
}
}