1
0
Fork 0

fix ignore

This commit is contained in:
prgres 2019-01-22 04:38:43 +01:00
parent ddd8e13abc
commit b80b51730c
15 changed files with 501 additions and 8 deletions

View File

@ -2,8 +2,21 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="2dc093a7-8382-4e31-b457-ce6c9fd10d13" name="Default Changelist" comment="a">
<change afterPath="$PROJECT_DIR$/target/classes/META-INF/sysmag.kotlin_module" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/SysmagApplication.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/controller/HomeController.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/controller/ProductController.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/exception/ProductNotFoundException.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/model/Product.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/model/dto/IdDto.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/model/dto/QuantityChange.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/repository/ProductRepository.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/service/ProductService.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/service/ProductServiceImpl.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/validationGroup/UpdateGroup.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/validator/ProductValidator.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/dino/scrum/sysmag/validator/QuantityChangeValidator.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application.properties" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/com/dino/scrum/sysmag/SysmagApplicationTests.java" beforeDir="false" />
</list>
<ignored path="$PROJECT_DIR$/target/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -153,8 +166,8 @@
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/.gitignore">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="265">
<caret line="97" column="3" selection-start-line="97" selection-start-column="3" selection-end-line="97" selection-end-column="3" />
<state relative-caret-position="400">
<caret line="106" lean-forward="true" selection-start-line="106" selection-end-line="106" />
</state>
</provider>
</entry>
@ -357,7 +370,7 @@
<option name="presentableId" value="Default" />
<updated>1547322958782</updated>
<workItem from="1547322960420" duration="21872000" />
<workItem from="1547401179909" duration="32923000" />
<workItem from="1547401179909" duration="33163000" />
</task>
<task id="LOCAL-00001" summary="added quantityChange validation, product validation, checking existing object in db, throwing exeption">
<created>1548121370553</created>
@ -398,7 +411,7 @@
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="54795000" />
<option name="totallyTimeSpent" value="55035000" />
</component>
<component name="TodoView">
<todo-panel id="selected-file">
@ -411,6 +424,7 @@
</component>
<component name="ToolWindowManager">
<frame x="0" y="23" width="1680" height="1027" extended-state="6" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49810606" visible="true" weight="0.17948718" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
@ -686,8 +700,8 @@
</entry>
<entry file="file://$PROJECT_DIR$/.gitignore">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="265">
<caret line="97" column="3" selection-start-line="97" selection-start-column="3" selection-end-line="97" selection-end-column="3" />
<state relative-caret-position="400">
<caret line="106" lean-forward="true" selection-start-line="106" selection-end-line="106" />
</state>
</provider>
</entry>

View File

@ -0,0 +1,14 @@
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

@ -0,0 +1,17 @@
package com.dino.scrum.sysmag.controller;
import org.springframework.web.bind.annotation.GetMapping;
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

@ -0,0 +1,95 @@
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

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

View File

@ -0,0 +1,59 @@
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

@ -0,0 +1,13 @@
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

@ -0,0 +1,15 @@
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

@ -0,0 +1,19 @@
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

@ -0,0 +1,27 @@
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

@ -0,0 +1,105 @@
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

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

View File

@ -0,0 +1,38 @@
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

@ -0,0 +1,44 @@
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

@ -0,0 +1,17 @@
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() {
}
}