-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Order 생성 시 필요한 검증 및 처리를 팩토리패턴으로 분리해 처리하도록 수정
- Loading branch information
Showing
3 changed files
with
58 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/camp/woowak/lab/order/domain/OrderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters