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

Teste Java Boticario #207

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
63 changes: 62 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<lombok.version>1.18.24</lombok.version>
</properties>

<dependencies>
Expand All @@ -36,14 +37,74 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.2.Final</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package br.com.blz.testjava.api.enums;

public enum TypeWarehouse {
ECOMMERCE, PHYSICAL_STORE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package br.com.blz.testjava.api.inventory.entity;

import br.com.blz.testjava.api.werehouse.entity.WarehouseEntity;
import lombok.Data;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.List;

@Data
@Entity
@Table(name = "inventory")
public class InventoryEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<WarehouseEntity> werehouses;
}
142 changes: 142 additions & 0 deletions src/main/java/br/com/blz/testjava/api/mapper/MapperUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package br.com.blz.testjava.api.mapper;

import br.com.blz.testjava.api.inventory.entity.InventoryEntity;
import br.com.blz.testjava.api.product.controller.domain.ProductRequest;
import br.com.blz.testjava.api.product.controller.domain.ProductResponse;
import br.com.blz.testjava.api.product.dto.InventoryDto;
import br.com.blz.testjava.api.product.dto.WarehousesDto;
import br.com.blz.testjava.api.product.entity.ProductEntity;
import br.com.blz.testjava.api.werehouse.entity.WarehouseEntity;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@org.mapstruct.Mapper(componentModel = "spring")
@Configuration
public interface MapperUtil {

default ProductEntity productRequestToEntity(ProductRequest productRequest) {

if (productRequest == null) {
return null;
} else {
ProductEntity productEntity = new ProductEntity();
productEntity.setSku(productRequest.getSku());
productEntity.setName(productRequest.getName());
productEntity.setInventory(this.inventoryDtoToInventoryEntity(productRequest.getInventory()));

return productEntity;
}
}

default InventoryEntity inventoryDtoToInventoryEntity(InventoryDto inventoryDto) {
if (inventoryDto == null) {
return null;
} else {
final List<WarehouseEntity> warehouseEntityList = new ArrayList<>();
InventoryEntity inventoryEntity = new InventoryEntity();
for (WarehousesDto wh : inventoryDto.getWarehouses()) {
warehouseEntityList.add(toEntityWareHouse(wh));
}

inventoryEntity.setWerehouses(warehouseEntityList);
return inventoryEntity;
}
}

default InventoryDto inventoryEntityToInventoryDto(InventoryEntity inventoryEntity) {
if (inventoryEntity == null) {
return null;
} else {

final List<WarehousesDto> warehouseDtoList = new ArrayList<>();
InventoryDto inventoryDto = new InventoryDto();

for (WarehouseEntity wh : inventoryEntity.getWerehouses()) {

inventoryDto.setQuantity(
Optional.ofNullable(inventoryDto.getQuantity()).orElse(0)
+ Optional.ofNullable(wh.getQuantity()).orElse(0));

warehouseDtoList.add(warehousesEntityToDto(wh));
}
inventoryDto.setWarehouses(warehouseDtoList);
return inventoryDto;
}
}

WarehouseEntity toEntityWareHouse(WarehousesDto warehousesDto);

WarehousesDto warehousesEntityToDto(WarehouseEntity warehousesEntity);

default ProductResponse productEntitytoResponse(ProductEntity productEntity) {
if (productEntity == null) {
return null;
} else {
ProductResponse productResponse = new ProductResponse();
if (productEntity.getSku() != null) {
productResponse.setSku(productEntity.getSku().longValue());
}
productResponse.setName(productEntity.getName());

productResponse.setInventory(inventoryEntityToInventoryDto(productEntity.getInventory()));
productResponse.setIsMarketable((productResponse.getInventory().getQuantity() > 0));

return productResponse;
}
}

default ProductEntity productEntitytoEntityAtualizar(
ProductRequest productRequest, ProductEntity productEntity) {
if (productRequest == null) {
return null;
} else {

if (productRequest.getSku() != null) {
productEntity.setSku(productRequest.getSku());
}
productEntity.setName(productRequest.getName());

productEntity.setInventory(
inventoryDtoToInventoryEntityAtualizar(
productRequest.getInventory(), productEntity.getInventory()));

return productEntity;
}
}

default InventoryEntity inventoryDtoToInventoryEntityAtualizar(
InventoryDto inventoryDto, InventoryEntity inventoryEntity) {
if (inventoryDto == null) {
return null;
} else {

final List<WarehouseEntity> warehouseEntityList = new ArrayList<>();

int size =
Math.min(inventoryDto.getWarehouses().size(), inventoryEntity.getWerehouses().size());
for (int i = 0; i < size; i++) {
warehouseEntityList.add(
toEntityWareHouseAtualizar(
inventoryDto.getWarehouses().get(i), inventoryEntity.getWerehouses().get(i)));
}

inventoryEntity.setWerehouses(warehouseEntityList);
return inventoryEntity;
}
}

default WarehouseEntity toEntityWareHouseAtualizar(
WarehousesDto warehousesDto, WarehouseEntity warehouseEntity) {
if (warehousesDto == null) {
return null;
} else {
warehouseEntity.setLocality(warehousesDto.getLocality());
warehouseEntity.setQuantity(warehousesDto.getQuantity());
warehouseEntity.setType(warehousesDto.getType());
return warehouseEntity;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package br.com.blz.testjava.api.product.controller;

import br.com.blz.testjava.api.product.controller.domain.ProductRequest;
import br.com.blz.testjava.api.product.controller.domain.ProductResponse;
import br.com.blz.testjava.api.product.service.ProductService;
import br.com.blz.testjava.common.exceptions.ProductAlreadyExistsException;
import br.com.blz.testjava.common.exceptions.ProductNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("product")
public class ProductController {

private final ProductService service;

@PostMapping
public ResponseEntity<ProductResponse> createProduct(@RequestBody ProductRequest product)
throws ProductAlreadyExistsException {
return ResponseEntity.status(HttpStatus.CREATED).body(service.create(product));
}

@GetMapping("{id}")
public ResponseEntity<ProductResponse> getProduct(@PathVariable("id") final Integer id)
throws ProductNotFoundException {
return ResponseEntity.status(HttpStatus.CREATED).body(service.getProduct(id));
}

@PutMapping
public ResponseEntity<ProductResponse> update(@RequestBody final ProductRequest request)
throws ProductNotFoundException {
return ResponseEntity.status(HttpStatus.CREATED).body(service.update(request));
}

@DeleteMapping("{id}")
public ResponseEntity delete(@PathVariable("id") final Integer id)
throws ProductNotFoundException {
service.delete(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package br.com.blz.testjava.api.product.controller.domain;

import br.com.blz.testjava.api.product.dto.InventoryDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ProductRequest {
private Integer sku;
private String name;
private InventoryDto inventory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package br.com.blz.testjava.api.product.controller.domain;

import br.com.blz.testjava.api.product.dto.InventoryDto;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ProductResponse {
private Long sku;
private String name;
private InventoryDto inventory;
private Boolean isMarketable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.com.blz.testjava.api.product.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class InventoryDto {
private Integer quantity;
private List<WarehousesDto> warehouses;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.blz.testjava.api.product.dto;

import br.com.blz.testjava.api.enums.TypeWarehouse;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class WarehousesDto {

private String locality;
private Integer quantity;
private TypeWarehouse type;
}
Loading