Refactoring

This commit is contained in:
asia 2021-08-21 15:42:18 +02:00
parent c1b36b3bb0
commit 154b23203e
5 changed files with 52 additions and 62 deletions

View File

@ -0,0 +1,8 @@
package com.baeldung.hexagonal;
public interface ShoppingCartService {
Float getTotalCartValue();
void addItem(String name, Integer quantity);
}

View File

@ -3,20 +3,22 @@ package com.baeldung.hexagonal.adapter.in;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baeldung.hexagonal.ShoppingCartService;
@RestController
@RequestMapping("/shopping-cart")
public class ShoppingCartController {
@Autowired
ShoppingCartFacade apiAdapter;
@Autowired
ShoppingCartService shoppingCartService;
@GetMapping
public Float getTotalCartValue() {
return apiAdapter.getTotalCartValue();
}
@GetMapping
public Float getTotalCartValue() {
return shoppingCartService.getTotalCartValue();
}
@PostMapping()
public void addItem(@RequestBody ItemTransportObject item) {
apiAdapter.addItem(item);
}
@PostMapping()
public void addItem(@RequestBody ItemTransportObject item) {
shoppingCartService.addItem(item.getName(), item.getQuantity());
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.hexagonal.adapter.in;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.hexagonal.domain.ShoppingCartService;
@Component
class ShoppingCartFacade {
@Autowired
ShoppingCartService shoppingCartService;
void addItem(ItemTransportObject item) {
shoppingCartService.addItem(item.getName(), item.getQuantity());
}
Float getTotalCartValue() {
return shoppingCartService.getTotalCartValue();
}
}

View File

@ -1,31 +0,0 @@
package com.baeldung.hexagonal.domain;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.hexagonal.adapter.out.Product;
import com.baeldung.hexagonal.domain.ports.ProductRepository;
@Service
public class ShoppingCartService {
private final ShoppingCart shoppingCart;
@Autowired
ProductRepository productRepository;
public ShoppingCartService() {
this.shoppingCart = new ShoppingCart(new HashMap<>());
}
public Float getTotalCartValue() {
return shoppingCart.getTotalCartValue();
}
public void addItem(String name, Integer quantity) {
Product product = productRepository.findByName(name);
shoppingCart.addItem(product.toItem(), quantity);
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.hexagonal.domain;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.hexagonal.ShoppingCartService;
import com.baeldung.hexagonal.adapter.out.Product;
import com.baeldung.hexagonal.domain.ports.ProductRepository;
@Service
public class SimpleShoppingCartService implements ShoppingCartService {
private final ShoppingCart shoppingCart;
@Autowired
ProductRepository productRepository;
public SimpleShoppingCartService() {
this.shoppingCart = new ShoppingCart(new HashMap<>());
}
public Float getTotalCartValue() {
return shoppingCart.getTotalCartValue();
}
public void addItem(String name, Integer quantity) {
Product product = productRepository.findByName(name);
shoppingCart.addItem(product.toItem(), quantity);
}
}