From 6de24c5519168eb16674ddb15341008f07bd31ce Mon Sep 17 00:00:00 2001 From: prgres Date: Tue, 22 Jan 2019 20:21:36 +0100 Subject: [PATCH] fix default page size in /get-all --- .../sysmag/controller/ProductController.java | 11 ++++++-- .../scrum/sysmag/service/ProductService.java | 6 ++-- .../sysmag/service/ProductServiceImpl.java | 28 +++++++++++++------ .../sysmag/validator/ProductValidator.java | 15 ++++++++-- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/dino/scrum/sysmag/controller/ProductController.java b/src/main/java/com/dino/scrum/sysmag/controller/ProductController.java index 20a592e..87572f2 100644 --- a/src/main/java/com/dino/scrum/sysmag/controller/ProductController.java +++ b/src/main/java/com/dino/scrum/sysmag/controller/ProductController.java @@ -8,6 +8,7 @@ import com.dino.scrum.sysmag.validationGroup.UpdateGroup; import com.dino.scrum.sysmag.validator.QuantityChangeValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; @@ -38,8 +39,14 @@ public class ProductController { } @GetMapping(value = "/get-all") - public Iterable getAll(Pageable pageable) { - return productService.getAll(pageable); + public Iterable getAll(@PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) { + System.out.println(pageable.getPageSize()); + + try { + return productService.getAll(pageable); + } catch (Exception e) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.toString()); + } } @GetMapping(value = "/get-price-of-all") diff --git a/src/main/java/com/dino/scrum/sysmag/service/ProductService.java b/src/main/java/com/dino/scrum/sysmag/service/ProductService.java index 54824e0..2ffd3c5 100644 --- a/src/main/java/com/dino/scrum/sysmag/service/ProductService.java +++ b/src/main/java/com/dino/scrum/sysmag/service/ProductService.java @@ -2,15 +2,15 @@ package com.dino.scrum.sysmag.service; import com.dino.scrum.sysmag.model.Product; import com.dino.scrum.sysmag.model.dto.QuantityChange; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; /** * Created by prgres on 2019-01-12. */ public interface ProductService { - Slice getAll(Pageable pageable); + Page getAll(Pageable pageable) throws Exception; Product changeQuantity(QuantityChange quantityChange) throws Exception; @@ -20,6 +20,8 @@ public interface ProductService { Product update(Product product, long id) throws Exception; + long count(); + float getPriceOfAll(); void delete(long id) throws Exception; diff --git a/src/main/java/com/dino/scrum/sysmag/service/ProductServiceImpl.java b/src/main/java/com/dino/scrum/sysmag/service/ProductServiceImpl.java index 354a4d8..37fa85f 100644 --- a/src/main/java/com/dino/scrum/sysmag/service/ProductServiceImpl.java +++ b/src/main/java/com/dino/scrum/sysmag/service/ProductServiceImpl.java @@ -5,9 +5,9 @@ import com.dino.scrum.sysmag.model.dto.QuantityChange; import com.dino.scrum.sysmag.repository.ProductRepository; import com.dino.scrum.sysmag.validator.ProductValidator; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; @@ -33,16 +33,23 @@ public class ProductServiceImpl implements ProductService { } @Override - public Slice getAll(Pageable pageable) { + public Page getAll(Pageable pageable) throws Exception { + System.out.println("pagenumber: " + pageable.getPageNumber() + " pageSize: " + pageable.getPageSize() + " sort: " + pageable.getSort()); + if (pageable.getSort().isUnsorted()) { pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), new Sort(Sort.Direction.ASC, "id")); } - return productRepository.findAll(pageable); + + Page productSlice = productRepository.findAll(pageable); + + productValidator.checkIfPageIsOutOfBound(productSlice.getTotalPages(), pageable.getPageNumber()); + + return productSlice; } @Override public Product changeQuantity(QuantityChange quantityChange) throws Exception { - productValidator.checkIfExists(quantityChange.getId()); + productValidator.checkIfNotExists(quantityChange.getId()); productRepository.save( productRepository.findById(quantityChange.getId()) @@ -54,7 +61,7 @@ public class ProductServiceImpl implements ProductService { @Override public Product getById(Long id) throws Exception { - productValidator.checkIfExists(id); + productValidator.checkIfNotExists(id); return productRepository.findById(id.longValue()); } @@ -73,13 +80,13 @@ public class ProductServiceImpl implements ProductService { @Override public Product add(Product product) throws Exception { - productValidator.checkIfExists(product.getName()); + productValidator.checkIfNotExists(product.getName()); return productRepository.save(product); } @Override public Product update(Product productReceived, long id) throws Exception { - productValidator.checkIfExists(productReceived.getId()); + productValidator.checkIfNotExists(productReceived.getId()); Product productToChange = productRepository.findById(id); @@ -103,9 +110,14 @@ public class ProductServiceImpl implements ProductService { return productRepository.findById(productToChange.getId().longValue()); } + @Override + public long count() { + return productRepository.count(); + } + @Override public void delete(long id) throws Exception { - productValidator.checkIfExists(id); + productValidator.checkIfNotExists(id); productRepository.deleteById(id); } } diff --git a/src/main/java/com/dino/scrum/sysmag/validator/ProductValidator.java b/src/main/java/com/dino/scrum/sysmag/validator/ProductValidator.java index dd6dcac..d3c7654 100644 --- a/src/main/java/com/dino/scrum/sysmag/validator/ProductValidator.java +++ b/src/main/java/com/dino/scrum/sysmag/validator/ProductValidator.java @@ -26,14 +26,23 @@ public class ProductValidator { return productRepository.existsByName(name); } - public void checkIfExists(long id) throws Exception { + private boolean ifRequestPageInLimit(int limit, int requestPage) { + return requestPage < limit; + } + + public void checkIfNotExists(long id) throws Exception { if (!existsById(id)) throw new Exception("Product with id: " + id + " not found"); System.out.println(id + " exists"); } - public void checkIfExists(String name) throws Exception { - if (!existsByName(name)) + public void checkIfNotExists(String name) throws Exception { + if (existsByName(name)) throw new Exception("Product " + name + " already exists"); } + + public void checkIfPageIsOutOfBound(int totalPages, int requestPage) throws Exception { + if (ifRequestPageInLimit(requestPage, totalPages)) + throw new Exception("Page " + requestPage + " is out of the total pages number."); + } } -- 2.20.1