Skip to content

Commit

Permalink
[refactor] Order 검증 분리
Browse files Browse the repository at this point in the history
- Order 생성 시 필요한 검증 및 처리를 팩토리패턴으로 분리해 처리하도록 수정
  • Loading branch information
kimhyun5u committed Sep 19, 2024
1 parent 4c103d5 commit 3bf99bc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 52 deletions.
46 changes: 6 additions & 40 deletions src/main/java/camp/woowak/lab/order/domain/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@
import java.util.Collections;
import java.util.List;

import camp.woowak.lab.cart.domain.vo.CartItem;
import camp.woowak.lab.customer.domain.Customer;
import camp.woowak.lab.menu.exception.NotEnoughStockException;
import camp.woowak.lab.order.domain.vo.OrderItem;
import camp.woowak.lab.order.exception.EmptyCartException;
import camp.woowak.lab.order.exception.MinimumOrderPriceNotMetException;
import camp.woowak.lab.order.exception.MultiStoreOrderException;
import camp.woowak.lab.order.exception.NotFoundMenuException;
import camp.woowak.lab.payaccount.exception.InsufficientBalanceException;
import camp.woowak.lab.payaccount.exception.NotFoundAccountException;
import camp.woowak.lab.store.domain.Store;
import camp.woowak.lab.store.exception.NotFoundStoreException;
import camp.woowak.lab.vendor.domain.Vendor;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.ElementCollection;
Expand Down Expand Up @@ -54,37 +45,12 @@ public class Order {

private LocalDateTime createdAt;

/**
* @throws EmptyCartException 카트가 비어 있는 경우
* @throws NotFoundStoreException 가게가 조회되지 않는 경우
* @throws MultiStoreOrderException 여러 가게의 메뉴를 주문한 경우
* @throws NotEnoughStockException 메뉴의 재고가 부족한 경우
* @throws NotFoundMenuException 주문한 메뉴가 조회되지 않는 경우
* @throws MinimumOrderPriceNotMetException 가게의 최소 주문금액보다 적은 금액을 주문한 경우
* @throws NotFoundAccountException 구매자의 계좌가 조회되지 않는 경우
* @throws InsufficientBalanceException 구매자의 계좌에 잔액이 충분하지 않은 경우
*/
public Order(Customer requester, List<CartItem> cartItems,
SingleStoreOrderValidator singleStoreOrderValidator,
StockRequester stockRequester, PriceChecker priceChecker, WithdrawPointService withdrawPointService,
LocalDateTime createdAt) {
Store store = singleStoreOrderValidator.check(cartItems);

List<CartItem> stockDecreaseSuccessCartItems = null;
try {
stockDecreaseSuccessCartItems = stockRequester.request(cartItems);
List<OrderItem> orderItems = priceChecker.check(store, cartItems);
withdrawPointService.withdraw(requester, orderItems);
this.requester = requester;
this.store = store;
this.orderItems = orderItems;
this.createdAt = createdAt;
} catch (Exception e) {
if (stockDecreaseSuccessCartItems != null) {
stockRequester.rollback(stockDecreaseSuccessCartItems);
}
throw e;
}
public Order(Customer requester
, Store store, List<OrderItem> orderItems, LocalDateTime createdAt) {
this.requester = requester;
this.store = store;
this.orderItems = orderItems;
this.createdAt = createdAt;
}

public Customer getRequester() {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/camp/woowak/lab/order/domain/OrderFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package camp.woowak.lab.order.domain;

import java.util.List;

import org.springframework.stereotype.Component;

import camp.woowak.lab.cart.domain.vo.CartItem;
import camp.woowak.lab.customer.domain.Customer;
import camp.woowak.lab.infra.date.DateTimeProvider;
import camp.woowak.lab.order.domain.vo.OrderItem;
import camp.woowak.lab.store.domain.Store;

@Component
public class OrderFactory {
private final SingleStoreOrderValidator singleStoreOrderValidator;
private final StockRequester stockRequester;
private final PriceChecker priceChecker;
private final WithdrawPointService withdrawPointService;
private final DateTimeProvider dateTimeProvider;

public OrderFactory(SingleStoreOrderValidator singleStoreOrderValidator,
StockRequester stockRequester,
PriceChecker priceChecker,
WithdrawPointService withdrawPointService, DateTimeProvider dateTimeProvider) {
this.singleStoreOrderValidator = singleStoreOrderValidator;
this.stockRequester = stockRequester;
this.priceChecker = priceChecker;
this.withdrawPointService = withdrawPointService;
this.dateTimeProvider = dateTimeProvider;
}

public Order createOrder(Customer requester, List<CartItem> cartItems) {
Store store = singleStoreOrderValidator.check(cartItems);

List<CartItem> stockDecreaseSuccessCartItems = null;
try {
stockDecreaseSuccessCartItems = stockRequester.request(cartItems);
List<OrderItem> orderItems = priceChecker.check(store, cartItems);
withdrawPointService.withdraw(requester, orderItems);

return new Order(requester, store, orderItems, dateTimeProvider.now());
} catch (Exception e) {
if (stockDecreaseSuccessCartItems != null) {
stockRequester.rollback(stockDecreaseSuccessCartItems);
}
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
import camp.woowak.lab.customer.domain.Customer;
import camp.woowak.lab.customer.repository.CustomerRepository;
import camp.woowak.lab.infra.aop.DistributedLock;
import camp.woowak.lab.infra.date.DateTimeProvider;
import camp.woowak.lab.menu.exception.NotEnoughStockException;
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.domain.OrderFactory;
import camp.woowak.lab.order.exception.DuplicatedOrderException;
import camp.woowak.lab.order.exception.EmptyCartException;
import camp.woowak.lab.order.exception.MinimumOrderPriceNotMetException;
Expand All @@ -41,13 +37,9 @@ public class OrderCreationService {
private final OrderRepository orderRepository;
private final CartRepository cartRepository;
private final CustomerRepository customerRepository;
private final SingleStoreOrderValidator singleStoreOrderValidator;
private final StockRequester stockRequester;
private final WithdrawPointService withdrawPointService;
private final PriceChecker priceChecker;
private final OrderPaymentRepository orderPaymentRepository;

private final DateTimeProvider dateTimeProvider;
private final OrderFactory orderFactory;

/**
* @throws EmptyCartException 카트가 비어 있는 경우
Expand All @@ -68,8 +60,7 @@ public Long create(final OrderCreationCommand cmd) {
List<CartItem> cartItems = cart.getCartItems();

Order savedOrder = orderRepository.save(
new Order(requester, cartItems, singleStoreOrderValidator, stockRequester, priceChecker,
withdrawPointService, dateTimeProvider.now())
orderFactory.createOrder(requester, cartItems)
);

saveOrderPayment(savedOrder);
Expand Down

0 comments on commit 3bf99bc

Please sign in to comment.