-
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.
주문 금액만큼 사용자의 계좌에서 차감하는 서비스
- Loading branch information
Showing
3 changed files
with
52 additions
and
7 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
35 changes: 35 additions & 0 deletions
35
src/main/java/camp/woowak/lab/order/domain/WithdrawPointService.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,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; | ||
} | ||
} |
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