1
0
forked from s434786/DINO_SCRUM

fix default page size in /get-all

This commit is contained in:
prgres 2019-01-22 20:21:36 +01:00
parent 58732fe0c9
commit 6de24c5519
4 changed files with 45 additions and 15 deletions

View File

@ -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<Product> getAll(Pageable pageable) {
return productService.getAll(pageable);
public Iterable<Product> 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")

View File

@ -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<Product> getAll(Pageable pageable);
Page<Product> 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;

View File

@ -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<Product> getAll(Pageable pageable) {
public Page<Product> 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<Product> 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);
}
}

View File

@ -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.");
}
}