#33 tdd(order-service): payment - apply jpa
This commit is contained in:
@@ -1,13 +1,25 @@
|
|||||||
package com.example.productorderservice.payment;
|
package com.example.productorderservice.payment;
|
||||||
|
|
||||||
import com.example.productorderservice.order.Order;
|
import com.example.productorderservice.order.Order;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Getter
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||||
class Payment {
|
class Payment {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
private final Order order;
|
|
||||||
private final String cardNumber;
|
@OneToOne
|
||||||
|
private Order order;
|
||||||
|
private String cardNumber;
|
||||||
|
|
||||||
public Payment(Order order, String cardNumber) {
|
public Payment(Order order, String cardNumber) {
|
||||||
Assert.notNull(order, "주문은 필수입니다.");
|
Assert.notNull(order, "주문은 필수입니다.");
|
||||||
@@ -16,19 +28,8 @@ class Payment {
|
|||||||
this.cardNumber = cardNumber;
|
this.cardNumber = cardNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void assign(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPrice() {
|
public int getPrice() {
|
||||||
return order.getTotalPrice();
|
return order.getTotalPrice();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCardNumber() {
|
|
||||||
return cardNumber;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.example.productorderservice.payment;
|
package com.example.productorderservice.payment;
|
||||||
|
|
||||||
import com.example.productorderservice.order.Order;
|
import com.example.productorderservice.order.Order;
|
||||||
|
import com.example.productorderservice.order.OrderRepository;
|
||||||
import com.example.productorderservice.product.DiscountPolicy;
|
import com.example.productorderservice.product.DiscountPolicy;
|
||||||
import com.example.productorderservice.product.Product;
|
import com.example.productorderservice.product.Product;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -9,15 +10,18 @@ import org.springframework.stereotype.Component;
|
|||||||
class PaymentAdapter implements PaymentPort {
|
class PaymentAdapter implements PaymentPort {
|
||||||
private final PaymentGateway paymentGateway;
|
private final PaymentGateway paymentGateway;
|
||||||
private final PaymentRepository paymentRepository;
|
private final PaymentRepository paymentRepository;
|
||||||
|
private final OrderRepository orderRepository;
|
||||||
|
|
||||||
PaymentAdapter(PaymentGateway paymentGateway, PaymentRepository paymentRepository) {
|
PaymentAdapter(PaymentGateway paymentGateway, PaymentRepository paymentRepository, OrderRepository orderRepository) {
|
||||||
this.paymentGateway = paymentGateway;
|
this.paymentGateway = paymentGateway;
|
||||||
this.paymentRepository = paymentRepository;
|
this.paymentRepository = paymentRepository;
|
||||||
|
this.orderRepository = orderRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Order getOrder(Long orderId) {
|
public Order getOrder(Long orderId) {
|
||||||
return new Order(new Product("상품1", 1000, DiscountPolicy.NONE), 2);
|
return orderRepository.findById(orderId)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("주문이 존재하지 않습니다."));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,17 +1,8 @@
|
|||||||
package com.example.productorderservice.payment;
|
package com.example.productorderservice.payment;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
class PaymentRepository {
|
interface PaymentRepository extends JpaRepository<Payment, Long> {
|
||||||
private final Map<Long, Payment> persistence = new HashMap<>();
|
|
||||||
private Long sequence = 0L;
|
|
||||||
|
|
||||||
public void save(Payment payment) {
|
|
||||||
payment.assign(++sequence);
|
|
||||||
persistence.put(payment.getId(), payment);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.example.productorderservice.payment;
|
|||||||
|
|
||||||
import com.example.productorderservice.order.Order;
|
import com.example.productorderservice.order.Order;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PaymentService {
|
public class PaymentService {
|
||||||
@@ -11,6 +12,7 @@ public class PaymentService {
|
|||||||
this.paymentPort = paymentPort;
|
this.paymentPort = paymentPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public void payment(PaymentRequest request) {
|
public void payment(PaymentRequest request) {
|
||||||
final Order order = paymentPort.getOrder(request.orderId());
|
final Order order = paymentPort.getOrder(request.orderId());
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
package com.example.productorderservice.payment;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
class PaymentServicePojoTest {
|
|
||||||
|
|
||||||
private PaymentService paymentService;
|
|
||||||
private PaymentPort paymentPort;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
final PaymentGateway paymentGateway = new ConsolePaymentGateway();
|
|
||||||
final PaymentRepository paymentRepository= new PaymentRepository();
|
|
||||||
paymentPort = new PaymentAdapter(paymentGateway, paymentRepository);
|
|
||||||
paymentService = new PaymentService(paymentPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void 상품주문() {
|
|
||||||
|
|
||||||
final PaymentRequest request = PaymentSteps.주문결제요청_생성();
|
|
||||||
|
|
||||||
paymentService.payment(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user