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

First commit API REST SKU #212

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,5 @@ buildNumber.properties


# End of https://www.gitignore.io/api/java,maven,macos,linux,intellij+all,eclipse
.vscode/launch.json
.vscode/settings.json
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>17</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/br/com/blz/testjava/controller/SkuController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package br.com.blz.testjava.controller;

import java.net.URI;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import br.com.blz.testjava.entities.Sku;
import br.com.blz.testjava.service.SkuService;

@RestController
@RequestMapping(value = "/sku")
public class SkuController {

@Autowired
private SkuService skuService;

@PostMapping
public ResponseEntity<Sku> insertSku(@RequestBody Sku sku) {
sku = skuService.insert(sku);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand(sku.getSku()).toUri();
return ResponseEntity.created(uri).body(sku);

}

@PutMapping(value = "/{idSku}")
public ResponseEntity<Sku> updateSku(@PathVariable Long idSku, @RequestBody Sku sku) {
sku = skuService.update(idSku, sku);
return ResponseEntity.ok().body(sku);
}

@GetMapping(value = "/{idSku}")
public ResponseEntity<Sku> getAllSku(@PathVariable Long idSku) {
Sku sku = skuService.findIdSku(idSku);
return ResponseEntity.ok().body(sku);
}

@GetMapping
public ResponseEntity<List<Sku>> getAllSku() {
List<Sku> sku = skuService.findAllSku();
return ResponseEntity.ok().body(sku);
}

@DeleteMapping(value = "/{idSku}")
public ResponseEntity<Void> deleteSku(@PathVariable Long idSku) {
skuService.delete(idSku);
return ResponseEntity.noContent().build();
}

}
23 changes: 23 additions & 0 deletions src/main/java/br/com/blz/testjava/entities/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package br.com.blz.testjava.entities;

public class Inventory {

private int quantity;

public Inventory() {

}

public Inventory(int quantity) {
this.quantity = quantity;
}

public int getQuantity() {
return quantity;
}

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

}
61 changes: 61 additions & 0 deletions src/main/java/br/com/blz/testjava/entities/Sku.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package br.com.blz.testjava.entities;

import java.util.List;

public class Sku {

private Long sku;
private String name;
private Inventory inventory;
private List<Warehouses> warehouses;
private boolean isMarketable;

public Sku() {

}

public Sku(Long sku, String name, Inventory inventory, List<Warehouses> warehouses, boolean isMarketable) {
this.sku = sku;
this.name = name;
this.inventory = inventory;
this.warehouses = warehouses;
this.isMarketable = isMarketable;
}

public Long getSku() {
return sku;
}

public void setSku(Long 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 List<Warehouses> getWarehouses() {
return warehouses;
}

public boolean getIsMarketable() {
return isMarketable;
}

public void setIsMarketable(boolean isMarketable) {
this.isMarketable = isMarketable;
}

}
43 changes: 43 additions & 0 deletions src/main/java/br/com/blz/testjava/entities/Warehouses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package br.com.blz.testjava.entities;

public class Warehouses {

private String locality;
private Integer quantity;
private String type;

public Warehouses() {

}

public Warehouses(String locality, Integer quantity, String type) {
this.locality = locality;
this.quantity = quantity;
this.type = type;
}

public String getLocality() {
return locality;
}

public void setLocality(String locality) {
this.locality = locality;
}

public Integer getQuantity() {
return quantity;
}

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

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

}
8 changes: 8 additions & 0 deletions src/main/java/br/com/blz/testjava/exception/SkuException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package br.com.blz.testjava.exception;

public class SkuException extends RuntimeException {

public SkuException(String exception) {
super(exception);
}
}
100 changes: 100 additions & 0 deletions src/main/java/br/com/blz/testjava/service/SkuService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package br.com.blz.testjava.service;

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

import org.springframework.stereotype.Service;

import br.com.blz.testjava.entities.Inventory;
import br.com.blz.testjava.entities.Sku;
import br.com.blz.testjava.exception.SkuException;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SkuService {

private Integer quantities = 0;
private List<Sku> products = new ArrayList<>();

public Sku insert(Sku sku) {
try {
if (products.isEmpty()) {
products.add(sku);
} else {
for (Sku list : products) {
if (list.getSku().equals(sku.getSku())) {
throw new SkuException("This SKU already was registered!!");
}
}
products.add(sku);
}
log.info("SKU created!");
} catch (Exception e) {
log.warn(e.getMessage());
}
return sku;
}

public Sku update(Long idSku, Sku sku) {
try {
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getSku().equals(idSku)) {
products.remove(i);
products.add(i, sku);
log.info("SKU updated!");
i++;
}
}
} catch (Exception e) {
log.warn("SKU not found! Not possible update this SKU {}", e.getMessage());
}
return sku;
}

public Sku findIdSku(Long idSku) {
Sku sku = new Sku();
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getSku().equals(idSku)) {
sumQuantity(this.quantities);
sku = products.get(i);
}
}
return sku;
}

public List<Sku> findAllSku() {
sumQuantity(this.quantities);
return products;
}

public Integer sumQuantity(Integer quantities) {
for (int i = 0; i < products.size(); i++) {
for (int j = 0; j < products.get(i).getWarehouses().size(); j++) {
quantities += products.get(i).getWarehouses().get(j).getQuantity();
}
if (quantities > 0) {
Inventory inventory = new Inventory(quantities);
products.get(i).setInventory(inventory);
products.get(i).setIsMarketable(true);
}
quantities = 0;
}
return quantities;
}

public void delete(Long skuId) {
try {
for (int i = 0; i <= products.size(); i++) {
if (products.get(i).getSku().equals(skuId)) {
products.remove(i);
log.info("SKU deleted!");
i++;
}
}
} catch (Exception e) {
log.warn("SKU not found: {}", e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.blz.testjava;
/* package br.com.blz.testjava;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -13,4 +13,4 @@ public class TestJavaApplicationTests {
public void contextLoads() {
}

}
} */
Loading