Skip to content

Commit

Permalink
[Order API TDD] in-memory to jpa
Browse files Browse the repository at this point in the history
  • Loading branch information
jihunparkme committed Feb 26, 2024
1 parent 23cb68c commit 2ae8051
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.example.productorderservice.product;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

@Entity
@Table(name = "products")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private final String name;
private final int price;
private final DiscountPolicy discountPolicy;
private String name;
private int price;
private DiscountPolicy discountPolicy;

public Product(final String name, final int price, final DiscountPolicy discountPolicy) {
Assert.hasText(name, "상품명은 필수입니다.");
Expand All @@ -16,12 +26,4 @@ public Product(final String name, final int price, final DiscountPolicy discount
this.price = price;
this.discountPolicy = discountPolicy;
}

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

public Long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
package com.example.productorderservice.product;

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

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

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

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

import org.springframework.http.HttpStatus;
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 ProductService {
}

@PostMapping
@Transactional
public ResponseEntity<Void> addProduct(@RequestBody final AddProductRequest request) {
final Product product = new Product(request.name(), request.price(), request.discountPolicy()); // 05. Product 클래스 생성

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

0 comments on commit 2ae8051

Please sign in to comment.