-
Notifications
You must be signed in to change notification settings - Fork 177
๐ 1๋จ๊ณ - ๋ฆฌํฉํฐ๋ง(์ํ) #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: jwoo-o
Are you sure you want to change the base?
Changes from 3 commits
d0ec2cb
e6cc1f6
0021fa2
48bdba2
2d2f2ff
7039a93
500063d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package kitchenpos.products.tobe.application; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.UUID; | ||
| import kitchenpos.menus.domain.Menu; | ||
| import kitchenpos.menus.domain.MenuProduct; | ||
| import kitchenpos.menus.domain.MenuRepository; | ||
| import kitchenpos.products.tobe.application.dto.ProductCreateRequest; | ||
| import kitchenpos.products.tobe.application.dto.ProductPriceUpdateRequest; | ||
| import kitchenpos.products.tobe.application.dto.ProductResponse; | ||
| import kitchenpos.products.tobe.domain.Product; | ||
| import kitchenpos.products.tobe.domain.ProductRepository; | ||
| import kitchenpos.products.tobe.infra.PurgomalumClient; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| public class ProductService { | ||
|
|
||
| private final ProductRepository jpaProductRepository; | ||
| private final MenuRepository menuRepository; | ||
| private final PurgomalumClient purgomalumClient; | ||
|
|
||
| public ProductService( | ||
| final ProductRepository productRepository, | ||
| final MenuRepository menuRepository, | ||
| final PurgomalumClient purgomalumClient | ||
| ) { | ||
| this.jpaProductRepository = productRepository; | ||
| this.menuRepository = menuRepository; | ||
| this.purgomalumClient = purgomalumClient; | ||
| } | ||
|
|
||
| @Transactional | ||
| public ProductResponse create(ProductCreateRequest request) { | ||
| Product product = jpaProductRepository.save(Product.of(request.getName(), request.getPrice(), purgomalumClient)); | ||
|
|
||
| return ProductResponse.toResponse(product); | ||
| } | ||
|
|
||
| @Transactional | ||
| public ProductResponse changePrice(UUID productId, ProductPriceUpdateRequest request) { | ||
|
|
||
| Product product = jpaProductRepository.findById(productId) | ||
| .orElseThrow(NoSuchElementException::new); | ||
| product.updatePrice(request.getPrice()); | ||
|
|
||
| final List<Menu> menus = menuRepository.findAllByProductId(productId); | ||
| for (final Menu menu : menus) { | ||
| BigDecimal sum = BigDecimal.ZERO; | ||
| for (final MenuProduct menuProduct : menu.getMenuProducts()) { | ||
| sum = sum.add( | ||
| menuProduct.getProduct() | ||
| .getPrice() | ||
| .multiply(BigDecimal.valueOf(menuProduct.getQuantity())) | ||
| ); | ||
| } | ||
| if (menu.getPrice().compareTo(sum) > 0) { | ||
| menu.setDisplayed(false); | ||
| } | ||
| } | ||
| return ProductResponse.toResponse(product); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<ProductResponse> findAll() { | ||
| return jpaProductRepository.findAll() | ||
| .stream() | ||
| .map(ProductResponse::toResponse) | ||
| .toList(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package kitchenpos.products.tobe.application.dto; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public class ProductCreateRequest { | ||
| private final String name; | ||
| private final BigDecimal price; | ||
|
|
||
| public ProductCreateRequest(String name, BigDecimal price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public BigDecimal getPrice() { | ||
| return price; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package kitchenpos.products.tobe.application.dto; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public class ProductPriceUpdateRequest { | ||
| private final BigDecimal price; | ||
|
|
||
| public ProductPriceUpdateRequest(BigDecimal price) { | ||
| this.price = price; | ||
| } | ||
|
|
||
| public BigDecimal getPrice() { | ||
| return price; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package kitchenpos.products.tobe.application.dto; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.UUID; | ||
| import kitchenpos.products.tobe.domain.Product; | ||
|
|
||
| public class ProductResponse { | ||
| private final UUID id; | ||
| private final String name; | ||
| private final BigDecimal price; | ||
|
|
||
| public ProductResponse(UUID id, String name, BigDecimal price) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public BigDecimal getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public static ProductResponse toResponse(Product product) { | ||
| return new ProductResponse(product.getId(), product.getName(), product.getPrice()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package kitchenpos.products.tobe.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Embedded; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.UUID; | ||
| import kitchenpos.products.tobe.infra.PurgomalumClient; | ||
|
|
||
| @Table(name = "product") | ||
| @Entity | ||
| public class Product { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| @Column(name = "id", columnDefinition = "binary(16)") | ||
| @Id | ||
| private UUID id; | ||
|
|
||
| @Embedded | ||
| private ProductName name; | ||
|
|
||
| @Embedded | ||
| private ProductPrice price; | ||
|
|
||
| protected Product() { | ||
| } | ||
|
|
||
| private Product(ProductName name, ProductPrice price) { | ||
| this.id = UUID.randomUUID(); | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public static Product of(String name, BigDecimal price, PurgomalumClient purgomalumClient) { | ||
| return new Product(ProductName.from(name, purgomalumClient), | ||
| ProductPrice.from(price)); | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name.getName(); | ||
| } | ||
|
|
||
| public BigDecimal getPrice() { | ||
| return price.getPrice(); | ||
| } | ||
|
|
||
| public void updatePrice(BigDecimal price) { | ||
| this.price = ProductPrice.from(price); | ||
| } | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package kitchenpos.products.tobe.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Embeddable; | ||
| import kitchenpos.products.tobe.infra.PurgomalumClient; | ||
|
|
||
| @Embeddable | ||
| public class ProductName { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vo๋ก ์ ์ํด๋ณด์ จ๊ตฐ์ ๐๐ |
||
|
|
||
| @Column(name = "name", nullable = false) | ||
| private String name; | ||
|
|
||
| protected ProductName() { | ||
| } | ||
|
|
||
| private ProductName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public static ProductName from(String name, PurgomalumClient purgomalumClient) { | ||
| if (name == null || name.isEmpty()) { | ||
| throw new IllegalArgumentException("์ํ๋ช ์ ํ์๋ก ์ ๋ ฅํด์ผ ํฉ๋๋ค."); | ||
| } | ||
|
|
||
| if (purgomalumClient.containsProfanity(name)) { | ||
| throw new IllegalArgumentException("๋น์์ด๊ฐ ํฌํจ๋์ด ์์ต๋๋ค."); | ||
| } | ||
| return new ProductName(name); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package kitchenpos.products.tobe.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Embeddable; | ||
| import java.math.BigDecimal; | ||
|
|
||
| @Embeddable | ||
| public class ProductPrice { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pp1๊ณผ pp2๋ ๊ฐ์๊น์? :)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. euqals and hashcode๊ฐ ํ์ํ๊ฒ ๊ตฐ์ |
||
|
|
||
| @Column(name = "price", nullable = false) | ||
| private BigDecimal price; | ||
|
|
||
| protected ProductPrice() {} | ||
|
|
||
| private ProductPrice(BigDecimal price) { | ||
| this.price = price; | ||
| } | ||
|
|
||
| public static ProductPrice from(BigDecimal price) { | ||
| validate(price); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์์ฑ์์์ ๊ฒ์ฆํ์์ง ์์ ์ด์ ๊ฐ ๊ถ๊ธํด์!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊ฐ์ฒด ์์ฑ์ ์คํ๋๊ธฐ ์ ์ ๊ฐ์ฒด๊ฐ ๊ฒ์ฆ์ด ๋๋ ํ๋ฆ์ด ๋ง์๊ฑฐ ๊ฐ์ ์ ์ ํฉํฐ๋ฆฌ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ์ ์ ํฉํฐ๋ฆฌ์์ ๊ฒ์ฆํ์์ต๋๋ค. |
||
| return new ProductPrice(price); | ||
| } | ||
|
|
||
| private static void validate(BigDecimal price) { | ||
| if (price == null || price.compareTo(BigDecimal.ZERO) < 0) { | ||
| throw new IllegalArgumentException("๊ฐ๊ฒฉ์ 0 ์ด์์ด์ด์ผ ํฉ๋๋ค."); | ||
|
||
| } | ||
| } | ||
|
|
||
| public BigDecimal getPrice() { | ||
| return price; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package kitchenpos.products.tobe.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| public interface ProductRepository { | ||
| Product save(Product product); | ||
|
|
||
| Optional<Product> findById(UUID id); | ||
|
|
||
| List<Product> findAll(); | ||
|
|
||
| List<Product> findAllByIdIn(List<UUID> ids); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProductService์ ๋ฉ๋ด ๊ฐ๊ฒฉ์ ๋ํ ๊ฒ์ฆ์ฑ ์์ ๋ถ๋ฆฌํด๋ณด๋ฉด ์ด๋จ๊น์? :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ค์ ๋จ๊ณ์ธ ๋ฉ๋ด ๋ฆฌํํฐ๋ง๋ถ๋ถ ์์ ํด๋น ์ฝ๋๊ฐ ๋ณ๊ฒฝ์ด ๋ ๊ฑฐ ๊ฐ์ต๋๋ค!