diff --git a/pom.xml b/pom.xml index 0c508267..54e90d1d 100644 --- a/pom.xml +++ b/pom.xml @@ -30,12 +30,17 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-data-jpa + org.springframework.boot spring-boot-starter-test test + + diff --git a/src/main/java/br/com/blz/testjava/TestJavaApplication.java b/src/main/java/br/com/blz/testjava/TestJavaApplication.java index f12c87db..9197f837 100644 --- a/src/main/java/br/com/blz/testjava/TestJavaApplication.java +++ b/src/main/java/br/com/blz/testjava/TestJavaApplication.java @@ -2,6 +2,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; @SpringBootApplication(scanBasePackageClasses = TestJavaApplication.class) public class TestJavaApplication { diff --git a/src/main/java/br/com/blz/testjava/controller/ProductController.java b/src/main/java/br/com/blz/testjava/controller/ProductController.java new file mode 100644 index 00000000..66d6d181 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/controller/ProductController.java @@ -0,0 +1,47 @@ +package br.com.blz.testjava.controller; + + +import br.com.blz.testjava.model.Product; +import br.com.blz.testjava.service.ProductService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/products/test/") +public class ProductController { + + private final ProductService productService; + + public ProductController(ProductService productService) { + this.productService = productService; + } + + @PostMapping + public ResponseEntity createProduct(@RequestBody Product product) { + productService.createProduct(product); + return ResponseEntity.status(HttpStatus.CREATED).build(); + } + + @PutMapping("/{sku}") + public ResponseEntity updateProduct(@PathVariable Integer sku, @RequestBody Product updatedProduct) { + productService.updateProduct(sku, updatedProduct); + return ResponseEntity.status(HttpStatus.OK).build(); + } + + @GetMapping("/{sku}") + public ResponseEntity getProductBySku(@PathVariable Integer sku) { + Product product = productService.getProductBySku(sku); + if (product != null) { + return ResponseEntity.ok(product); + } else { + return ResponseEntity.notFound().build(); + } + } + + @DeleteMapping("/{sku}") + public ResponseEntity deleteProductBySku(@PathVariable Integer sku) { + productService.deleteProductBySku(sku); + return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); + } +} diff --git a/src/main/java/br/com/blz/testjava/model/Inventory.java b/src/main/java/br/com/blz/testjava/model/Inventory.java new file mode 100644 index 00000000..5b621b3e --- /dev/null +++ b/src/main/java/br/com/blz/testjava/model/Inventory.java @@ -0,0 +1,24 @@ +package br.com.blz.testjava.model; + +import java.util.List; + +public class Inventory { + private Integer quantity; + private List warehouses; + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public List getWarehouses() { + return warehouses; + } + + public void setWarehouses(List warehouses) { + this.warehouses = warehouses; + } +} diff --git a/src/main/java/br/com/blz/testjava/model/Product.java b/src/main/java/br/com/blz/testjava/model/Product.java new file mode 100644 index 00000000..961beac7 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/model/Product.java @@ -0,0 +1,40 @@ +package br.com.blz.testjava.model; + +public class Product { + private Integer sku; + private String name; + private Inventory inventory; + private boolean isMarketable; + + public Integer getSku() { + return sku; + } + + public void setSku(Integer sku) { + this.sku = sku; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Inventory getInventory() { + return inventory; + } + + public void setInventory(Inventory inventory) { + this.inventory = inventory; + } + + public boolean isMarketable() { + return isMarketable; + } + + public void setMarketable(boolean marketable) { + isMarketable = marketable; + } +} diff --git a/src/main/java/br/com/blz/testjava/model/Warehouse.java b/src/main/java/br/com/blz/testjava/model/Warehouse.java new file mode 100644 index 00000000..0ddb0731 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/model/Warehouse.java @@ -0,0 +1,31 @@ +package br.com.blz.testjava.model; + +public class Warehouse { + private String locality; + private int quantity; + private String type; + + public String getLocality() { + return locality; + } + + public void setLocality(String locality) { + this.locality = locality; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/src/main/java/br/com/blz/testjava/repositories/ProductRepository.java b/src/main/java/br/com/blz/testjava/repositories/ProductRepository.java new file mode 100644 index 00000000..d56cc190 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/repositories/ProductRepository.java @@ -0,0 +1,27 @@ +package br.com.blz.testjava.repositories; + +import br.com.blz.testjava.model.Product; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Repository +public class ProductRepository { + private List products = new ArrayList<>(); + + public void save(Product product) { + products.add(product); + } + + public Optional findById(Integer sku) { + return products.stream() + .filter(p -> p.getSku().equals(sku)) + .findFirst(); + } + + public void deleteById(Integer sku) { + products.removeIf(p -> p.getSku().equals(sku)); + } +} diff --git a/src/main/java/br/com/blz/testjava/service/ProductService.java b/src/main/java/br/com/blz/testjava/service/ProductService.java new file mode 100644 index 00000000..3feb7d28 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/service/ProductService.java @@ -0,0 +1,61 @@ +package br.com.blz.testjava.service; + + +import br.com.blz.testjava.model.Inventory; +import br.com.blz.testjava.model.Product; +import br.com.blz.testjava.model.Warehouse; +import br.com.blz.testjava.repositories.ProductRepository; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ProductService { + + private final ProductRepository productRepository; + + public ProductService(ProductRepository productRepository) { + this.productRepository = productRepository; + } + + public void createProduct(Product product) { + calculateInventoryQuantity(product); + calculateIsMarketable(product); + productRepository.save(product); + } + + public void updateProduct(Integer sku, Product updatedProduct) { + Product existingProduct = getProductBySku(sku); + if (existingProduct != null) { + // Atualizar os campos do produto existente com os valores do produto atualizado + existingProduct.setName(updatedProduct.getName()); + existingProduct.setInventory(updatedProduct.getInventory()); + + calculateInventoryQuantity(existingProduct); + calculateIsMarketable(existingProduct); + + productRepository.save(existingProduct); + } + } + + public Product getProductBySku(Integer sku) { + return productRepository.findById(sku).orElse(null); + } + + public void deleteProductBySku(Integer sku) { + productRepository.deleteById(sku); + } + + private void calculateInventoryQuantity(Product product) { + Inventory inventory = product.getInventory(); + List warehouses = inventory.getWarehouses(); + int totalQuantity = warehouses.stream().mapToInt(Warehouse::getQuantity).sum(); + inventory.setQuantity(totalQuantity); + } + + private void calculateIsMarketable(Product product) { + Inventory inventory = product.getInventory(); + int totalQuantity = inventory.getQuantity(); + product.setMarketable(totalQuantity > 0); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e69de29b..20f2250b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + autoconfigure: + exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration + +server: + port: 8081 diff --git a/src/test/java/br/com/blz/testjava/TestJavaApplicationTests.java b/src/test/java/br/com/blz/testjava/TestJavaApplicationTests.java deleted file mode 100644 index 08c41062..00000000 --- a/src/test/java/br/com/blz/testjava/TestJavaApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package br.com.blz.testjava; - -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 TestJavaApplicationTests { - - @Test - public void contextLoads() { - } - -}