1
0
forked from s434786/DINO_SCRUM
This commit is contained in:
prgres 2019-01-22 04:37:58 +01:00
parent 02118fe1de
commit ddd8e13abc
15 changed files with 0 additions and 500 deletions

View File

@ -1,14 +0,0 @@
package com.dino.scrum.sysmag;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SysmagApplication {
public static void main(String[] args) {
SpringApplication.run(SysmagApplication.class, args);
}
}

View File

@ -1,18 +0,0 @@
package com.dino.scrum.sysmag.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by prgres on 2019-01-13.
*/
@RestController
public class HomeController {
@GetMapping("/")
public String home(){
return "System magazynowy";
}
}

View File

@ -1,99 +0,0 @@
package com.dino.scrum.sysmag.controller;
import com.dino.scrum.sysmag.model.Product;
import com.dino.scrum.sysmag.model.dto.IdDto;
import com.dino.scrum.sysmag.model.dto.QuantityChange;
import com.dino.scrum.sysmag.service.ProductServiceImpl;
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.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import javax.validation.Valid;
import java.util.Collections;
import java.util.Map;
/**
* Created by prgres on 2019-01-12.
*/
@RestController
@RequestMapping(value = "/api")
public class ProductController {
private final
ProductServiceImpl productService;
private final
QuantityChangeValidator quantityChangeValidator;
@Autowired
public ProductController(ProductServiceImpl productService, QuantityChangeValidator quantityChangeValidator) {
this.productService = productService;
this.quantityChangeValidator = quantityChangeValidator;
}
@GetMapping(value = "/get-all")
public Iterable<Product> getAll(Pageable pageable) {
return productService.getAll(pageable);
}
@GetMapping(value = "/get-price-of-all")
public
Map getPriceOfAllProducts(){
return Collections.singletonMap("price-of-all", productService.getPriceOfAll());
}
@PostMapping(value = "/product/add")
public Product addProduct(@Valid @RequestBody Product product) {
try {
return productService.add(product);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.toString());
}
}
@DeleteMapping(value = "/product/delete")
public
String deleteProduct(@RequestBody IdDto id){
try {
productService.delete(id.getId());
return "Deleted" + id;
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.toString());
}
}
@GetMapping(value = "/product/get-by-id")
public
Product getById(@RequestBody IdDto id){
try {
return productService.getById(id.getId());
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.toString());
}
}
@PostMapping(value = "/product/update/{id}")
public Product update(@Validated(UpdateGroup.class) @RequestBody Product product, @PathVariable("id") long id) {
try {
return productService.update(product, id);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.toString());
}
}
@PostMapping(value = "/product/change-quantity")
public
Product changeQuantity(@RequestBody QuantityChange quantityChange){
try {
quantityChangeValidator.validate(quantityChange);
return productService.changeQuantity(quantityChange);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.toString());
}
}
}

View File

@ -1,8 +0,0 @@
package com.dino.scrum.sysmag.exception;
/**
* Created by prgres on 2019-01-21.
*/
public class ProductNotFoundException extends ClassNotFoundException {
}

View File

@ -1,59 +0,0 @@
package com.dino.scrum.sysmag.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.*;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
/**
* Created by prgres on 2019-01-12.
*/
@Getter
@Setter
@ToString
@Entity
@Table(name = "Product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@NotNull(message = "name cannot be null")
@Size(min = 2, message = "name cannot be shorter that 2")
@Column(name = "name")
private String name;
@NotNull(message = "price cannot be null")
@DecimalMin(value = "0.00", message = "price cannot be under 0.00")
@Column(name = "price")
private BigDecimal price;
@NotNull(message = "quantity cannot be null")
@Min(value = 0, message = "quantity cannot be under 0")
@Column(name = "quantity")
private long quantity;
@NotNull(message = "quantityMax cannot be null")
@Min(value = 1, message = "quantityMax cannot be under 1")
@Column(name = "quantityMax")
private long quantityMax;
@NotNull(message = "image_link cannot be null")
@Column(name = "image_link")
private String imageLink;
public Product setChangeQuantity(long change) {
this.quantity += change;
return this;
}
}

View File

@ -1,13 +0,0 @@
package com.dino.scrum.sysmag.model.dto;
import lombok.Getter;
/**
* Created by prgres on 2019-01-13.
*/
@Getter
public class IdDto {
long id;
}

View File

@ -1,15 +0,0 @@
package com.dino.scrum.sysmag.model.dto;
import lombok.EqualsAndHashCode;
import lombok.Getter;
/**
* Created by prgres on 2019-01-12.
*/
@Getter
@EqualsAndHashCode
public class QuantityChange {
long id;
long change;
}

View File

@ -1,19 +0,0 @@
package com.dino.scrum.sysmag.repository;
import com.dino.scrum.sysmag.model.Product;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
* Created by prgres on 2019-01-12.
*/
@Repository
public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {
boolean existsByName(String name);
Optional<Product> findByName(String name);
Product findById(long id);
}

View File

@ -1,27 +0,0 @@
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.Pageable;
import org.springframework.data.domain.Slice;
/**
* Created by prgres on 2019-01-12.
*/
public interface ProductService {
Slice<Product> getAll(Pageable pageable);
Product changeQuantity(QuantityChange quantityChange) throws Exception;
Product getById(Long id) throws Exception;
Product add(Product product) throws Exception;
Product update(Product product, long id) throws Exception;
float getPriceOfAll();
void delete(long id) throws Exception;
}

View File

@ -1,105 +0,0 @@
package com.dino.scrum.sysmag.service;
import com.dino.scrum.sysmag.model.Product;
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.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
/**
* Created by prgres on 2019-01-12.
*/
@Service
public class ProductServiceImpl implements ProductService {
private final
ProductRepository productRepository;
private final
ProductValidator productValidator;
@Autowired
public ProductServiceImpl(ProductRepository productRepository, ProductValidator productValidator) {
this.productRepository = productRepository;
this.productValidator = productValidator;
}
@Override
public Slice<Product> getAll(Pageable pageable) {
if (pageable.getSort().isUnsorted()) {
pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), new Sort(Sort.Direction.ASC, "id"));
}
return productRepository.findAll(pageable);
}
@Override
public Product changeQuantity(QuantityChange quantityChange) throws Exception {
productValidator.checkIfExists(quantityChange.getId());
return productRepository.findById(quantityChange.getId()).setChangeQuantity(quantityChange.getChange());
}
@Override
public Product getById(Long id) throws Exception {
productValidator.checkIfExists(id);
return productRepository.findById(id.longValue());
}
@Override
public float getPriceOfAll() {
BigDecimal result = new BigDecimal(0);
Iterable<Product> tempProductList = productRepository.findAll();
for (Product product : tempProductList) {
result = result.add(
product.getPrice()
.multiply( new BigDecimal(product.getQuantity())));
}
return result.floatValue();
}
@Override
public Product add(Product product) throws Exception {
productValidator.checkIfExists(product.getName());
return productRepository.save(product);
}
@Override
public Product update(Product productReceived, long id) throws Exception {
productValidator.checkIfExists(productReceived.getId());
Product productToChange = productRepository.findById(id);
if (productReceived.getName() != null)
productToChange.setName(productReceived.getName());
if (productReceived.getName() != null)
productToChange.setPrice(productReceived.getPrice());
if (productReceived.getName() != null)
productToChange.setQuantity(productReceived.getQuantity());
if (productReceived.getName() != null)
productToChange.setQuantityMax(productReceived.getQuantityMax());
if (productReceived.getName() != null)
productToChange.setImageLink(productReceived.getImageLink());
productRepository.save(productToChange);
return productRepository.findById(productToChange.getId().longValue());
}
@Override
public void delete(long id) throws Exception {
productValidator.checkIfExists(id);
productRepository.deleteById(id);
}
}

View File

@ -1,8 +0,0 @@
package com.dino.scrum.sysmag.validationGroup;
/**
* Created by prgres on 2019-01-22.
*/
public interface UpdateGroup {
}

View File

@ -1,38 +0,0 @@
package com.dino.scrum.sysmag.validator;
import com.dino.scrum.sysmag.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by prgres on 2019-01-22.
*/
@Component
public class ProductValidator {
private final ProductRepository productRepository;
@Autowired
public ProductValidator(ProductRepository productRepository) {
this.productRepository = productRepository;
}
private boolean existsById(long id) {
return productRepository.existsById(id);
}
private boolean existsByName(String name) {
return productRepository.existsByName(name);
}
public void checkIfExists(long id) throws Exception {
if (existsById(id))
throw new Exception("Product with id: " + id + " not found");
}
public void checkIfExists(String name) throws Exception {
if (existsByName(name))
throw new Exception("Product " + name + " already exists");
}
}

View File

@ -1,44 +0,0 @@
package com.dino.scrum.sysmag.validator;
import com.dino.scrum.sysmag.model.Product;
import com.dino.scrum.sysmag.model.dto.QuantityChange;
import com.dino.scrum.sysmag.service.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by prgres on 2019-01-20.
*/
@Component
public class QuantityChangeValidator {
private final
ProductServiceImpl productService;
@Autowired
public QuantityChangeValidator(ProductServiceImpl productService) {
this.productService = productService;
}
public void validate(QuantityChange quantityChange) throws Exception {
Product product = productService.getById(quantityChange.getId());
if (ifUnderStock(product.getQuantity(), quantityChange.getChange())) {
throw new RuntimeException("Too low product with id: " + quantityChange.getId() + " on stock");
}
if (ifAboveMaxLimitStock(product.getQuantity(), product.getQuantityMax(), quantityChange.getChange())) {
throw new RuntimeException("Over max quantity limit of product with id: " + quantityChange.getId());
}
}
private boolean ifUnderStock(long quantityOfProduct, long quantityChange) {
return ((quantityOfProduct + quantityChange) < 0);
}
private boolean ifAboveMaxLimitStock(long quantityOfProduct, long maxQuantityOfProduct, long quantityChange) {
return ((quantityOfProduct + quantityChange) > maxQuantityOfProduct);
}
}

View File

@ -1,16 +0,0 @@
# Details for our datasource heroku
spring.datasource.url=jdbc:postgresql://ec2-54-75-230-41.eu-west-1.compute.amazonaws.com/d3e1jrm08qe91q?ssl=true&sslmode=require&amp;sslfactory=org.postgresql.ssl.NonValidatingFactory
spring.datasource.username=eecsegponwphcn
spring.datasource.password=4045a1e5a2de22362149709c3a2a1d5eb6e243cd553fd1a15f76fc54923638ce
spring.datasource.driverClassName=org.postgresql.Driver
# Hibernate properties
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL94Dialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
#spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

View File

@ -1,17 +0,0 @@
package com.dino.scrum.sysmag;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SysmagApplicationTests {
@Test
public void contextLoads() {
}
}