This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Order <-> Payment 일대일 매핑 - PaymentService factory 생성 - PaymentService 공통 로직 추가
- Loading branch information
Showing
11 changed files
with
137 additions
and
29 deletions.
There are no files selected for viewing
22 changes: 17 additions & 5 deletions
22
src/main/java/me/jjeda/mall/orders/controller/PaymentController.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 |
---|---|---|
@@ -1,19 +1,31 @@ | ||
package me.jjeda.mall.orders.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import me.jjeda.mall.orders.domain.PaymentFactory; | ||
import me.jjeda.mall.orders.domain.PaymentType; | ||
import me.jjeda.mall.orders.dto.PaymentDto; | ||
import me.jjeda.mall.orders.service.PaymentService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@Controller | ||
@RequestMapping("/api/orders/payment") | ||
@RequiredArgsConstructor | ||
public class PaymentController { | ||
|
||
@PostMapping | ||
public ResponseEntity payForOrder() { | ||
private final PaymentFactory paymentFactory; | ||
|
||
//TODO : factory 패턴을 통해 client 에서 받아온 메시지의 PaymentType 에 따라 PaymentService 를 주입받는다. | ||
// final PaymentService paymentService = PaymentFactory.getType(); | ||
return null; | ||
@PostMapping(value = "/{orderId}") | ||
public ResponseEntity payForOrder(@RequestBody PaymentDto paymentDto, @PathVariable Long orderId) { | ||
|
||
final PaymentService paymentService = paymentFactory.getType(paymentDto.getPaymentType()); | ||
|
||
return ResponseEntity.ok(paymentService.payForOrder(paymentDto, orderId)); | ||
} | ||
|
||
} |
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
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
36 changes: 36 additions & 0 deletions
36
src/main/java/me/jjeda/mall/orders/domain/PaymentFactory.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,36 @@ | ||
package me.jjeda.mall.orders.domain; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import me.jjeda.mall.orders.service.CashPaymentService; | ||
import me.jjeda.mall.orders.service.CreditPaymentService; | ||
import me.jjeda.mall.orders.service.MobilePaymentService; | ||
import me.jjeda.mall.orders.service.PaymentService; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class PaymentFactory { | ||
|
||
private final CashPaymentService cashPaymentService; | ||
private final CreditPaymentService creditPaymentService; | ||
private final MobilePaymentService mobilePaymentService; | ||
|
||
public PaymentService getType(PaymentType paymentType) { | ||
final PaymentService paymentService; | ||
|
||
switch (paymentType) { | ||
case CASH: | ||
paymentService = cashPaymentService; | ||
break; | ||
case CREDIT: | ||
paymentService = creditPaymentService; | ||
break; | ||
case MOBILE: | ||
paymentService = mobilePaymentService; | ||
break; | ||
default: | ||
throw new IllegalArgumentException(); | ||
} | ||
return paymentService; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,26 @@ | ||
package me.jjeda.mall.orders.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import me.jjeda.mall.orders.domain.Payment; | ||
import me.jjeda.mall.orders.domain.PaymentStatus; | ||
import me.jjeda.mall.orders.domain.PaymentType; | ||
|
||
@Builder | ||
@Getter | ||
public class PaymentDto { | ||
|
||
private Long id; | ||
|
||
private int price; | ||
|
||
private PaymentStatus paymentStatus; | ||
|
||
private PaymentType paymentType; | ||
|
||
public static Payment toReadyEntity() { | ||
return Payment.builder() | ||
.paymentStatus(PaymentStatus.READY) | ||
.build(); | ||
} | ||
} |
40 changes: 36 additions & 4 deletions
40
src/main/java/me/jjeda/mall/orders/service/CashPaymentService.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 |
---|---|---|
@@ -1,18 +1,50 @@ | ||
package me.jjeda.mall.orders.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import me.jjeda.mall.items.service.ItemService; | ||
import me.jjeda.mall.orders.domain.Order; | ||
import me.jjeda.mall.orders.domain.OrderItem; | ||
import me.jjeda.mall.orders.domain.Payment; | ||
import me.jjeda.mall.orders.domain.PaymentStatus; | ||
import me.jjeda.mall.orders.domain.PaymentType; | ||
import me.jjeda.mall.orders.dto.PaymentDto; | ||
import me.jjeda.mall.orders.repository.CashPaymentRepository; | ||
import me.jjeda.mall.orders.repository.PaymentRepository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class CashPaymentService implements PaymentService { | ||
|
||
private final PaymentRepository paymentRepository; | ||
private final CashPaymentRepository cashPaymentRepository; | ||
private final OrderService orderService; | ||
private final ItemService itemService; | ||
|
||
@Override | ||
public PaymentDto payForOrder(PaymentDto paymentDto, Long orderId) { | ||
return null; | ||
@Transactional | ||
public Payment payForOrder(PaymentDto paymentDto, Long orderId) { | ||
//TODO : [#40] 탬플릿-콜백 패턴으로 공통부분 재사용코드로 만들기(각 결제 정보 저장로직만 다름) | ||
Order order = orderService.getOrder(orderId); | ||
Payment payment = order.getPayment(); | ||
List<OrderItem> orderItems = order.getOrderItems(); | ||
|
||
payment = Payment.builder() | ||
.paymentStatus(PaymentStatus.COMP) | ||
.id(payment.getId()) | ||
.price(order.getTotalPrice()) | ||
.createdAt(LocalDateTime.now()) | ||
.paymentType(PaymentType.CASH) | ||
.build(); | ||
|
||
/* 주문이 완료되면 아이템의 전체 재고에서 주문수량만큼 빼주어야한다. */ | ||
//TODO : N+1문제 -> 벌크호출 | ||
orderItems.forEach((orderItem) -> | ||
itemService.decrementStock(orderItem.getItem().getId(), orderItem.getQuantity()) | ||
); | ||
|
||
// TODO : cashPaymentRepository 를 통해 현금결제 정보를 저장해야함 | ||
|
||
return payment; | ||
} | ||
} |
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
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
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
3 changes: 2 additions & 1 deletion
3
src/main/java/me/jjeda/mall/orders/service/PaymentService.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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package me.jjeda.mall.orders.service; | ||
|
||
import me.jjeda.mall.orders.domain.Payment; | ||
import me.jjeda.mall.orders.dto.PaymentDto; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public interface PaymentService { | ||
|
||
PaymentDto payForOrder(PaymentDto paymentDto, Long orderId); | ||
Payment payForOrder(PaymentDto paymentDto, Long orderId); | ||
|
||
} |