Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Fork do teste Java #214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/br/com/blz/testjava/TestJavaApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Void> createProduct(@RequestBody Product product) {
productService.createProduct(product);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PutMapping("/{sku}")
public ResponseEntity<Void> updateProduct(@PathVariable Integer sku, @RequestBody Product updatedProduct) {
productService.updateProduct(sku, updatedProduct);
return ResponseEntity.status(HttpStatus.OK).build();
}

@GetMapping("/{sku}")
public ResponseEntity<Product> 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<Void> deleteProductBySku(@PathVariable Integer sku) {
productService.deleteProductBySku(sku);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}
24 changes: 24 additions & 0 deletions src/main/java/br/com/blz/testjava/model/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package br.com.blz.testjava.model;

import java.util.List;

public class Inventory {
private Integer quantity;
private List<Warehouse> warehouses;

public Integer getQuantity() {
return quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}

public List<Warehouse> getWarehouses() {
return warehouses;
}

public void setWarehouses(List<Warehouse> warehouses) {
this.warehouses = warehouses;
}
}
40 changes: 40 additions & 0 deletions src/main/java/br/com/blz/testjava/model/Product.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/br/com/blz/testjava/model/Warehouse.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Product> products = new ArrayList<>();

public void save(Product product) {
products.add(product);
}

public Optional<Product> 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));
}
}
61 changes: 61 additions & 0 deletions src/main/java/br/com/blz/testjava/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -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<Warehouse> 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);
}
}
6 changes: 6 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

server:
port: 8081
16 changes: 0 additions & 16 deletions src/test/java/br/com/blz/testjava/TestJavaApplicationTests.java

This file was deleted.