Skip to content

Commit

Permalink
[Order API TDD] JPA 적용하기]
Browse files Browse the repository at this point in the history
  • Loading branch information
jihunparkme committed Feb 27, 2024
1 parent 001dc06 commit 3423b8c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
2 changes: 2 additions & 0 deletions product-order-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ final UpdateProductRequest request = new UpdateProductRequest(name, price, disco
**JPA 적용**

> [JPA 적용하기]()
---

Point.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import org.springframework.data.jpa.repository.JpaRepository;

interface OrderRepository extends JpaRepository<Order, Long> {
public interface OrderRepository extends JpaRepository<Order, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.example.productorderservice.payment;

import com.example.productorderservice.order.Order;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

@Entity
@Table(name = "payment")
@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(final Order order, final String cardNumber) {
Assert.notNull(order, "주문은 필수입니다.");
Expand All @@ -17,10 +26,6 @@ public Payment(final Order order, final String cardNumber) {
this.cardNumber = cardNumber;
}

public void assignId(final Long id) {
this.id = id;
}

public int getPrice() {
return order.getTotalPrice();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,15 +10,18 @@
class PaymentAdapter implements PaymentPort {
private final PaymentGateway paymentGateway;
private final PaymentRepository paymentRepository;
private final OrderRepository orderRepository;

PaymentAdapter(final PaymentGateway paymentGateway, final PaymentRepository paymentRepository) {
PaymentAdapter(final PaymentGateway paymentGateway, final PaymentRepository paymentRepository, final OrderRepository orderRepository) {
this.paymentGateway = paymentGateway;
this.paymentRepository = paymentRepository;
this.orderRepository = orderRepository;
}

@Override
public Order getOrder(final long orderId) {
return new Order(new Product("상품1", 1000, DiscountPolicy.NONE), 1);
return orderRepository.findById(orderId)
.orElseThrow(() -> new IllegalArgumentException("주문이 존재하지 않습니다."));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
package com.example.productorderservice.payment;

import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.HashMap;
import java.util.Map;

@Repository
class PaymentRepository {
private Map<Long, Payment> persistence = new HashMap<>();
private Long sequence = 0L;

public void save(final Payment payment) {
payment.assignId(++sequence);
persistence.put(payment.getId(), payment);
}
interface PaymentRepository extends JpaRepository<Payment, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.productorderservice.order.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,6 +18,7 @@ class PaymentService {
}

@PostMapping
@Transactional
public ResponseEntity<Void> payment(@RequestBody final PaymentRequest request) {
Order order = paymentPort.getOrder(request.orderId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ class OrderApiTest extends ApiTest {

assertThat(response.statusCode()).isEqualTo(HttpStatus.CREATED.value());
}



}

0 comments on commit 3423b8c

Please sign in to comment.