Refactoring
This commit is contained in:
parent
c1b36b3bb0
commit
154b23203e
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.hexagonal;
|
||||||
|
|
||||||
|
public interface ShoppingCartService {
|
||||||
|
|
||||||
|
Float getTotalCartValue();
|
||||||
|
|
||||||
|
void addItem(String name, Integer quantity);
|
||||||
|
}
|
@ -3,20 +3,22 @@ package com.baeldung.hexagonal.adapter.in;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import com.baeldung.hexagonal.ShoppingCartService;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/shopping-cart")
|
@RequestMapping("/shopping-cart")
|
||||||
public class ShoppingCartController {
|
public class ShoppingCartController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
ShoppingCartFacade apiAdapter;
|
ShoppingCartService shoppingCartService;
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public Float getTotalCartValue() {
|
public Float getTotalCartValue() {
|
||||||
return apiAdapter.getTotalCartValue();
|
return shoppingCartService.getTotalCartValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping()
|
@PostMapping()
|
||||||
public void addItem(@RequestBody ItemTransportObject item) {
|
public void addItem(@RequestBody ItemTransportObject item) {
|
||||||
apiAdapter.addItem(item);
|
shoppingCartService.addItem(item.getName(), item.getQuantity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user