From 3bf99bc7dc1e9dc99922d8c7f836aaa96fde6775 Mon Sep 17 00:00:00 2001 From: kimhyun5u <22kimhyun5u@gmail.com> Date: Thu, 19 Sep 2024 19:27:52 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20Order=20=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20-=20Order=20=EC=83=9D=EC=84=B1=20=EC=8B=9C?= =?UTF-8?q?=20=ED=95=84=EC=9A=94=ED=95=9C=20=EA=B2=80=EC=A6=9D=20=EB=B0=8F?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC=EB=A5=BC=20=ED=8C=A9=ED=86=A0=EB=A6=AC?= =?UTF-8?q?=ED=8C=A8=ED=84=B4=EC=9C=BC=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= =?UTF-8?q?=ED=95=B4=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../camp/woowak/lab/order/domain/Order.java | 46 +++-------------- .../woowak/lab/order/domain/OrderFactory.java | 49 +++++++++++++++++++ .../order/service/OrderCreationService.java | 15 ++---- 3 files changed, 58 insertions(+), 52 deletions(-) create mode 100644 src/main/java/camp/woowak/lab/order/domain/OrderFactory.java 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 237b171c..4fd7044b 100644 --- a/src/main/java/camp/woowak/lab/order/domain/Order.java +++ b/src/main/java/camp/woowak/lab/order/domain/Order.java @@ -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; @@ -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 cartItems, - SingleStoreOrderValidator singleStoreOrderValidator, - StockRequester stockRequester, PriceChecker priceChecker, WithdrawPointService withdrawPointService, - LocalDateTime createdAt) { - Store store = singleStoreOrderValidator.check(cartItems); - - List stockDecreaseSuccessCartItems = null; - try { - stockDecreaseSuccessCartItems = stockRequester.request(cartItems); - List 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 orderItems, LocalDateTime createdAt) { + this.requester = requester; + this.store = store; + this.orderItems = orderItems; + this.createdAt = createdAt; } public Customer getRequester() { diff --git a/src/main/java/camp/woowak/lab/order/domain/OrderFactory.java b/src/main/java/camp/woowak/lab/order/domain/OrderFactory.java new file mode 100644 index 00000000..753c8feb --- /dev/null +++ b/src/main/java/camp/woowak/lab/order/domain/OrderFactory.java @@ -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 cartItems) { + Store store = singleStoreOrderValidator.check(cartItems); + + List stockDecreaseSuccessCartItems = null; + try { + stockDecreaseSuccessCartItems = stockRequester.request(cartItems); + List 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; + } + } +} 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 eace181f..efdd69ce 100644 --- a/src/main/java/camp/woowak/lab/order/service/OrderCreationService.java +++ b/src/main/java/camp/woowak/lab/order/service/OrderCreationService.java @@ -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; @@ -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 카트가 비어 있는 경우 @@ -68,8 +60,7 @@ public Long create(final OrderCreationCommand cmd) { List cartItems = cart.getCartItems(); Order savedOrder = orderRepository.save( - new Order(requester, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, dateTimeProvider.now()) + orderFactory.createOrder(requester, cartItems) ); saveOrderPayment(savedOrder);