Serwer 2.3 #17

Merged
s434786 merged 1 commits from s434805/DINO_SCRUM:master into backend 2019-01-22 20:44:27 +01:00
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 com.dino.scrum.sysmag.validator.QuantityChangeValidator;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -38,8 +39,14 @@ public class ProductController {
} }
@GetMapping(value = "/get-all") @GetMapping(value = "/get-all")
public Iterable<Product> getAll(Pageable pageable) { public Iterable<Product> getAll(@PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
return productService.getAll(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") @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.Product;
import com.dino.scrum.sysmag.model.dto.QuantityChange; 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.Pageable;
import org.springframework.data.domain.Slice;
/** /**
* Created by prgres on 2019-01-12. * Created by prgres on 2019-01-12.
*/ */
public interface ProductService { public interface ProductService {
Slice<Product> getAll(Pageable pageable); Page<Product> getAll(Pageable pageable) throws Exception;
Product changeQuantity(QuantityChange quantityChange) throws Exception; Product changeQuantity(QuantityChange quantityChange) throws Exception;
@ -20,6 +20,8 @@ public interface ProductService {
Product update(Product product, long id) throws Exception; Product update(Product product, long id) throws Exception;
long count();
float getPriceOfAll(); float getPriceOfAll();
void delete(long id) throws Exception; 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.repository.ProductRepository;
import com.dino.scrum.sysmag.validator.ProductValidator; import com.dino.scrum.sysmag.validator.ProductValidator;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -33,16 +33,23 @@ public class ProductServiceImpl implements ProductService {
} }
@Override @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()) { if (pageable.getSort().isUnsorted()) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), new Sort(Sort.Direction.ASC, "id")); 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 @Override
public Product changeQuantity(QuantityChange quantityChange) throws Exception { public Product changeQuantity(QuantityChange quantityChange) throws Exception {
productValidator.checkIfExists(quantityChange.getId()); productValidator.checkIfNotExists(quantityChange.getId());
productRepository.save( productRepository.save(
productRepository.findById(quantityChange.getId()) productRepository.findById(quantityChange.getId())
@ -54,7 +61,7 @@ public class ProductServiceImpl implements ProductService {
@Override @Override
public Product getById(Long id) throws Exception { public Product getById(Long id) throws Exception {
productValidator.checkIfExists(id); productValidator.checkIfNotExists(id);
return productRepository.findById(id.longValue()); return productRepository.findById(id.longValue());
} }
@ -73,13 +80,13 @@ public class ProductServiceImpl implements ProductService {
@Override @Override
public Product add(Product product) throws Exception { public Product add(Product product) throws Exception {
productValidator.checkIfExists(product.getName()); productValidator.checkIfNotExists(product.getName());
return productRepository.save(product); return productRepository.save(product);
} }
@Override @Override
public Product update(Product productReceived, long id) throws Exception { public Product update(Product productReceived, long id) throws Exception {
productValidator.checkIfExists(productReceived.getId()); productValidator.checkIfNotExists(productReceived.getId());
Product productToChange = productRepository.findById(id); Product productToChange = productRepository.findById(id);
@ -103,9 +110,14 @@ public class ProductServiceImpl implements ProductService {
return productRepository.findById(productToChange.getId().longValue()); return productRepository.findById(productToChange.getId().longValue());
} }
@Override
public long count() {
return productRepository.count();
}
@Override @Override
public void delete(long id) throws Exception { public void delete(long id) throws Exception {
productValidator.checkIfExists(id); productValidator.checkIfNotExists(id);
productRepository.deleteById(id); productRepository.deleteById(id);
} }
} }

View File

@ -26,14 +26,23 @@ public class ProductValidator {
return productRepository.existsByName(name); 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)) if (!existsById(id))
throw new Exception("Product with id: " + id + " not found"); throw new Exception("Product with id: " + id + " not found");
System.out.println(id + " exists"); System.out.println(id + " exists");
} }
public void checkIfExists(String name) throws Exception { public void checkIfNotExists(String name) throws Exception {
if (!existsByName(name)) if (existsByName(name))
throw new Exception("Product " + name + " already exists"); 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.");
}
} }