#33 tdd(order-service): payment - apply jpa

This commit is contained in:
haerong22
2023-02-15 01:35:53 +09:00
parent 7c66361794
commit 9bcc7518fa
5 changed files with 24 additions and 53 deletions

View File

@@ -1,13 +1,25 @@
package com.example.productorderservice.payment;
import com.example.productorderservice.order.Order;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;
import javax.persistence.*;
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
class Payment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private final Order order;
private final String cardNumber;
@OneToOne
private Order order;
private String cardNumber;
public Payment(Order order, String cardNumber) {
Assert.notNull(order, "주문은 필수입니다.");
@@ -16,19 +28,8 @@ class Payment {
this.cardNumber = cardNumber;
}
public void assign(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public int getPrice() {
return order.getTotalPrice();
}
public String getCardNumber() {
return cardNumber;
}
}

View File

@@ -1,6 +1,7 @@
package com.example.productorderservice.payment;
import com.example.productorderservice.order.Order;
import com.example.productorderservice.order.OrderRepository;
import com.example.productorderservice.product.DiscountPolicy;
import com.example.productorderservice.product.Product;
import org.springframework.stereotype.Component;
@@ -9,15 +10,18 @@ import org.springframework.stereotype.Component;
class PaymentAdapter implements PaymentPort {
private final PaymentGateway paymentGateway;
private final PaymentRepository paymentRepository;
private final OrderRepository orderRepository;
PaymentAdapter(PaymentGateway paymentGateway, PaymentRepository paymentRepository) {
PaymentAdapter(PaymentGateway paymentGateway, PaymentRepository paymentRepository, OrderRepository orderRepository) {
this.paymentGateway = paymentGateway;
this.paymentRepository = paymentRepository;
this.orderRepository = orderRepository;
}
@Override
public Order getOrder(Long orderId) {
return new Order(new Product("상품1", 1000, DiscountPolicy.NONE), 2);
return orderRepository.findById(orderId)
.orElseThrow(() -> new IllegalArgumentException("주문이 존재하지 않습니다."));
}
@Override

View File

@@ -1,17 +1,8 @@
package com.example.productorderservice.payment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.Map;
@Repository
class PaymentRepository {
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);
}
interface PaymentRepository extends JpaRepository<Payment, Long> {
}

View File

@@ -2,6 +2,7 @@ package com.example.productorderservice.payment;
import com.example.productorderservice.order.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class PaymentService {
@@ -11,6 +12,7 @@ public class PaymentService {
this.paymentPort = paymentPort;
}
@Transactional
public void payment(PaymentRequest request) {
final Order order = paymentPort.getOrder(request.orderId());

View File

@@ -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);
}
}