Skip to content

Commit

Permalink
[feat] 주문 결제 서비스
Browse files Browse the repository at this point in the history
주문 금액만큼 사용자의 계좌에서 차감하는 서비스
  • Loading branch information
Dr-KoKo committed Aug 16, 2024
1 parent 001c0b3 commit dba3013
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/main/java/camp/woowak/lab/order/domain/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ public class Order {
@ElementCollection(fetch = FetchType.EAGER)
private List<OrderItem> orderItems = new ArrayList<>();

public Order(Customer requester, Store store, List<CartItem> cartItems, SingleStoreOrderValidator validator,
StockRequester stockRequester, PriceChecker priceChecker) {
validator.check(store, cartItems);
public Order(Customer requester, Store store, List<CartItem> cartItems,
SingleStoreOrderValidator singleStoreOrderValidator,
StockRequester stockRequester, PriceChecker priceChecker, WithdrawPointService withdrawPointService) {
singleStoreOrderValidator.check(store, cartItems);
stockRequester.request(cartItems);
List<OrderItem> orderItem = priceChecker.check(cartItems);
List<OrderItem> orderItems = priceChecker.check(cartItems);
withdrawPointService.withdraw(requester, orderItems);
this.requester = requester;
this.store = store;
this.orderItems = orderItem;
this.orderItems = orderItems;
}

public Long getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<OrderItem> withdraw(Customer customer, List<OrderItem> orderItems) {
Optional<PayAccount> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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();
}
}

0 comments on commit dba3013

Please sign in to comment.