Skip to content
Snippets Groups Projects
Commit 747d3114 authored by Lukas Karsch's avatar Lukas Karsch
Browse files

#29 now returning pages as collection, query params for page number and page size available.

parent b4838c91
No related branches found
No related tags found
1 merge request!15#29 Pagination for Plants endpoint w/ CustomPageDTO
### Get all plants
GET http://localhost:8080/api/v1/plants
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsdWthcy5rYXJzY2hAZ214LmRlIiwiaWF0IjoxNzAwOTMzNjQ4LCJleHAiOjE3MDEwMjAwNDh9.IXoMuNecB7ARvZpEyx5SraMbdoZrHgADeHd7wAo0ddw
GET http://localhost:8080/api/v1/plants?page=0
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsdWthcy5rYXJzY2hAZ214LmRlIiwiaWF0IjoxNzAxMjc4MzkzLCJleHAiOjE3MDEzNjQ3OTN9.TVQlG3yAl_zJddPoWDndY9Xj-9EaR0P__x3aGU3sdBA
### Get garden entries
GET http://localhost:8080/api/v1/garden
......
......@@ -15,8 +15,11 @@ public class PlantsController {
private final PlantsService plantsService;
@GetMapping("/api/v1/plants")
public Collection<Plant> getAllPlants() {
return plantsService.getPlants();
public Collection<Plant> getAllPlants(
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "pageSize", defaultValue = "25") int pageSize
) {
return plantsService.getPlants(page, pageSize);
}
@GetMapping("api/v1/randomPlants/{numberOfPlants}")
......
......@@ -5,6 +5,8 @@ import hdm.mi.growbros.repositories.PlantRepository;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
......@@ -19,8 +21,9 @@ public class PlantsService {
private final PlantRepository plantRepository;
public Collection<Plant> getPlants() {
return plantRepository.findAll();
public Collection<Plant> getPlants(int pageNumber, int pageSize) {
final Pageable page = PageRequest.of(pageNumber, pageSize);
return plantRepository.findAll(page).getContent();
}
public Collection<Plant> getRandomPlants(int numberOfPlants) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment