diff --git a/src/main/java/camp/woowak/lab/order/domain/Order.java b/src/main/java/camp/woowak/lab/order/domain/Order.java index bf1337d8..8e8dd488 100644 --- a/src/main/java/camp/woowak/lab/order/domain/Order.java +++ b/src/main/java/camp/woowak/lab/order/domain/Order.java @@ -35,14 +35,16 @@ public class Order { @ElementCollection(fetch = FetchType.EAGER) private List orderItems = new ArrayList<>(); - public Order(Customer requester, Store store, List cartItems, SingleStoreOrderValidator validator, - StockRequester stockRequester, PriceChecker priceChecker) { - validator.check(store, cartItems); + public Order(Customer requester, Store store, List cartItems, + SingleStoreOrderValidator singleStoreOrderValidator, + StockRequester stockRequester, PriceChecker priceChecker, WithdrawPointService withdrawPointService) { + singleStoreOrderValidator.check(store, cartItems); stockRequester.request(cartItems); - List orderItem = priceChecker.check(cartItems); + List orderItems = priceChecker.check(cartItems); + withdrawPointService.withdraw(requester, orderItems); this.requester = requester; this.store = store; - this.orderItems = orderItem; + this.orderItems = orderItems; } public Long getId() { diff --git a/src/main/java/camp/woowak/lab/order/domain/WithdrawPointService.java b/src/main/java/camp/woowak/lab/order/domain/WithdrawPointService.java new file mode 100644 index 00000000..d3501d5b --- /dev/null +++ b/src/main/java/camp/woowak/lab/order/domain/WithdrawPointService.java @@ -0,0 +1,35 @@ +package camp.woowak.lab.order.domain; + +import java.util.List; +import java.util.Optional; + +import org.springframework.stereotype.Component; + +import camp.woowak.lab.customer.domain.Customer; +import camp.woowak.lab.order.domain.vo.OrderItem; +import camp.woowak.lab.payaccount.domain.PayAccount; +import camp.woowak.lab.payaccount.exception.NotFoundAccountException; +import camp.woowak.lab.payaccount.repository.PayAccountRepository; + +@Component +public class WithdrawPointService { + private final PayAccountRepository payAccountRepository; + + public WithdrawPointService(PayAccountRepository payAccountRepository) { + this.payAccountRepository = payAccountRepository; + } + + public List withdraw(Customer customer, List orderItems) { + Optional findPayAccount = payAccountRepository.findByCustomerIdForUpdate(customer.getId()); + if (findPayAccount.isEmpty()) { + throw new NotFoundAccountException("주문을 처리할 계좌가 생성되지 않았습니다."); + } + PayAccount payAccount = findPayAccount.get(); + int totalPrice = 0; + for (OrderItem orderItem : orderItems) { + totalPrice += orderItem.getTotalPrice(); + } + payAccount.withdraw(totalPrice); + return orderItems; + } +} diff --git a/src/main/java/camp/woowak/lab/order/service/OrderCreationService.java b/src/main/java/camp/woowak/lab/order/service/OrderCreationService.java index 593891df..3873ade0 100644 --- a/src/main/java/camp/woowak/lab/order/service/OrderCreationService.java +++ b/src/main/java/camp/woowak/lab/order/service/OrderCreationService.java @@ -13,8 +13,10 @@ import camp.woowak.lab.customer.domain.Customer; import camp.woowak.lab.customer.repository.CustomerRepository; import camp.woowak.lab.order.domain.Order; +import camp.woowak.lab.order.domain.PriceChecker; import camp.woowak.lab.order.domain.SingleStoreOrderValidator; import camp.woowak.lab.order.domain.StockRequester; +import camp.woowak.lab.order.domain.WithdrawPointService; import camp.woowak.lab.order.exception.EmptyCartException; import camp.woowak.lab.order.repository.OrderRepository; import camp.woowak.lab.order.service.command.OrderCreationCommand; @@ -31,16 +33,21 @@ public class OrderCreationService { private final CustomerRepository customerRepository; private final SingleStoreOrderValidator singleStoreOrderValidator; private final StockRequester stockRequester; + private final WithdrawPointService withdrawPointService; + private final PriceChecker priceChecker; public OrderCreationService(OrderRepository orderRepository, CartRepository cartRepository, StoreRepository storeRepository, CustomerRepository customerRepository, - SingleStoreOrderValidator singleStoreOrderValidator, StockRequester stockRequester) { + SingleStoreOrderValidator singleStoreOrderValidator, StockRequester stockRequester, + WithdrawPointService withdrawPointService, PriceChecker priceChecker) { this.orderRepository = orderRepository; this.cartRepository = cartRepository; this.storeRepository = storeRepository; this.customerRepository = customerRepository; this.singleStoreOrderValidator = singleStoreOrderValidator; this.stockRequester = stockRequester; + this.withdrawPointService = withdrawPointService; + this.priceChecker = priceChecker; } public Long create(OrderCreationCommand cmd) { @@ -56,7 +63,8 @@ public Long create(OrderCreationCommand cmd) { throw new NotFoundStoreException("등록되지 않은 가게의 상품을 주문했습니다."); } Order savedOrder = orderRepository.save( - new Order(requester, findStore.get(), cartItems, singleStoreOrderValidator, stockRequester)); + new Order(requester, findStore.get(), cartItems, singleStoreOrderValidator, stockRequester, priceChecker, + withdrawPointService)); return savedOrder.getId(); } }