-
Notifications
You must be signed in to change notification settings - Fork 177
π 1λ¨κ³ - 리ν©ν°λ§(μν) #328
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: sumiini
Are you sure you want to change the base?
Changes from all commits
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,81 @@ | ||||||
| package kitchenpos.products.tobe.application; | ||||||
|
|
||||||
| import kitchenpos.menus.domain.Menu; | ||||||
| import kitchenpos.menus.domain.MenuProduct; | ||||||
| import kitchenpos.menus.domain.MenuRepository; | ||||||
| import kitchenpos.products.domain.Product; | ||||||
| import kitchenpos.products.domain.ProductRepository; | ||||||
| import kitchenpos.products.infra.PurgomalumClient; | ||||||
| import org.springframework.stereotype.Service; | ||||||
| import org.springframework.transaction.annotation.Transactional; | ||||||
|
|
||||||
| import java.math.BigDecimal; | ||||||
| import java.util.List; | ||||||
| import java.util.NoSuchElementException; | ||||||
| import java.util.Objects; | ||||||
| import java.util.UUID; | ||||||
|
|
||||||
| @Service | ||||||
| public class ProductService { | ||||||
| private final ProductRepository productRepository; | ||||||
| private final MenuRepository menuRepository; | ||||||
| private final PurgomalumClient purgomalumClient; | ||||||
|
|
||||||
| public ProductService( | ||||||
| final ProductRepository productRepository, | ||||||
| final MenuRepository menuRepository, | ||||||
| final PurgomalumClient purgomalumClient | ||||||
| ) { | ||||||
| this.productRepository = productRepository; | ||||||
| this.menuRepository = menuRepository; | ||||||
| this.purgomalumClient = purgomalumClient; | ||||||
| } | ||||||
|
|
||||||
| @Transactional | ||||||
| public Product create(final Product request) { | ||||||
|
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. κΈ°μ‘΄μ μλΉμ€ λ‘μ§μ μ¬μ©νλ μμΉλ€μ 리ν©ν°λ§ν λ‘μ§λ€μ λμ μ¬μ©νλλ‘ λ³κ²½ν΄μ£ΌμΈμ. μ§κΈμ μνμμλ 리ν©ν°λ§ν μ½λλ€μ΄ μ΄μ μ²λΌ μ λμν μ§μ λν νμ μ΄ λ€μ§ μλλ°μ. π€ |
||||||
| final BigDecimal price = request.getPrice(); | ||||||
| if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) { | ||||||
| throw new IllegalArgumentException(); | ||||||
| } | ||||||
| final String name = request.getName(); | ||||||
| if (Objects.isNull(name) || purgomalumClient.containsProfanity(name)) { | ||||||
| throw new IllegalArgumentException(); | ||||||
| } | ||||||
|
Comment on lines
+36
to
+43
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. Price, Name κ° κ°μ²΄λ₯Ό μμ±νκΈ°λ§ν΄λ μ΄ κ²μ¦μ μμ°μ€λ½κ² μνλ κ² κ°μλ°μ. π€ |
||||||
| final Product product = new Product(); | ||||||
| product.setId(UUID.randomUUID()); | ||||||
| product.setName(name); | ||||||
| product.setPrice(price); | ||||||
| return productRepository.save(product); | ||||||
| } | ||||||
|
|
||||||
| @Transactional | ||||||
| public Product changePrice(final UUID productId, final Product request) { | ||||||
| final BigDecimal price = request.getPrice(); | ||||||
| if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) { | ||||||
| throw new IllegalArgumentException(); | ||||||
| } | ||||||
| final Product product = productRepository.findById(productId) | ||||||
| .orElseThrow(NoSuchElementException::new); | ||||||
|
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. μμΈκ° λ°μνμ λ, μ μ ν μλ¬ λ©μμ§λ₯Ό λμ Έμ€λ€λ©΄ μλ¬λ©μμ§λ₯Ό ν΅ν΄μ λ¬Έμ μ μμΈμ νμ νκΈ°κ° λ μμν΄μ§μ§ μμκΉμ? π
Suggested change
|
||||||
| product.setPrice(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. setterλ₯Ό μ΄μ©ν기보λ€λ, λ³κ²½μ μλ―Έλ₯Ό λλ¬λΌ μ μλλ‘ μ μ ν λ©μλλͺ μ λ€μ΄λ°ν΄λ³΄λ©΄ μ’μ κ² κ°μ΅λλ€.
Suggested change
|
||||||
| 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 product; | ||||||
| } | ||||||
|
|
||||||
| @Transactional(readOnly = true) | ||||||
| public List<Product> findAll() { | ||||||
| return productRepository.findAll(); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package kitchenpos.products.tobe.domain; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import jakarta.persistence.Embeddable; | ||
| import kitchenpos.products.infra.PurgomalumClient; | ||
| @Embeddable | ||
| public class Name { | ||
|
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. README.mdμ μ ν μ©μ΄ μ¬μ , λͺ¨λΈλ§μ λ΄€μ λλ Nameμ΄ μλ 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. Name, Priceμ κ²½μ° VOλΌκ³ λ³Ό μ μμ§ μμκΉμ? π |
||
| private final String name; | ||
| private final PurgomalumClient purgomalumClient; | ||
|
|
||
| public Name(final String name, final PurgomalumClient purgomalumClient) { | ||
| this.purgomalumClient = Objects.requireNonNull(purgomalumClient, "purgomalumClient must not be null"); | ||
| validate(name); | ||
| this.name = name; | ||
| } | ||
|
|
||
| private void validate(final String name) { | ||
| if (Objects.isNull(name) || name.isEmpty() || purgomalumClient.containsProfanity(name)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package kitchenpos.products.tobe.domain; | ||
|
|
||
| import jakarta.persistence.Embeddable; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.Objects; | ||
| @Embeddable | ||
| public class 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. μΆν λ€λ₯Έ 컨ν
μ€νΈμμλ Priceκ° λ±μ₯ν κ² κ°μλ°μ. |
||
| private final BigDecimal price; | ||
|
|
||
| public Price(BigDecimal price) { | ||
| validate(price); | ||
| this.price = price; | ||
| } | ||
|
|
||
| private void validate(final BigDecimal price) { | ||
| if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| public BigDecimal getPrice() { | ||
| return 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.Embedded; | ||
| import jakarta.persistence.Id; | ||
| import kitchenpos.products.tobe.persistence.ProductEntity; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class Product { | ||
| private final UUID id; | ||
|
|
||
| private final Name name; | ||
|
|
||
| private final Price price; | ||
|
|
||
| public Product(UUID id, Name name, Price price) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public Name getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public Price getPrice() { | ||
| return price; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| 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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package kitchenpos.products.tobe.persistence; | ||
|
|
||
| import kitchenpos.products.tobe.persistence.ProductEntity; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public interface JpaProductRepository extends JpaRepository<ProductEntity, UUID> { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package kitchenpos.products.tobe.persistence; | ||
|
|
||
| import kitchenpos.products.tobe.domain.Product; | ||
| import kitchenpos.products.tobe.domain.ProductRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Repository | ||
| public class JpaProductRepositoryImpl implements ProductRepository { | ||
|
|
||
| private final JpaProductRepository jpaProductRepository; | ||
|
|
||
| public JpaProductRepositoryImpl(JpaProductRepository jpaProductRepository) { | ||
| this.jpaProductRepository = jpaProductRepository; | ||
| } | ||
|
|
||
| @Override | ||
| public Product save(Product product) { | ||
| ProductEntity productEntity = new ProductEntity(product); | ||
| return jpaProductRepository.save(productEntity).toDomain(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Product> findById(UUID id) { | ||
| Optional<ProductEntity> productEntity = jpaProductRepository.findById(id); | ||
| return productEntity.map(ProductEntity::toDomain); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Product> findAll() { | ||
| return jpaProductRepository.findAll() | ||
| .stream() | ||
| .map(ProductEntity::toDomain) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Product> findAllByIdIn(List<UUID> ids) { | ||
| return jpaProductRepository.findAllById(ids) | ||
| .stream() | ||
| .map(ProductEntity::toDomain) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package kitchenpos.products.tobe.persistence; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import kitchenpos.products.tobe.domain.Name; | ||
| import kitchenpos.products.tobe.domain.Price; | ||
| import kitchenpos.products.tobe.domain.Product; | ||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(name = "product") | ||
| public class ProductEntity { | ||
|
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. μμ λλ©μΈ κ°μ²΄μ JPAμ Entityλ₯Ό λΆλ¦¬νμ ¨κ΅°μ? π |
||
|
|
||
| @Id | ||
| @Column(name = "id", columnDefinition = "binary(16)") | ||
| private UUID id; | ||
|
|
||
| @Embedded | ||
| private Name name; | ||
|
|
||
| @Embedded | ||
| private Price price; | ||
|
|
||
| protected ProductEntity() { | ||
| } | ||
|
|
||
| public ProductEntity(Product product) { | ||
| this.id = product.getId(); | ||
| this.name = product.getName(); | ||
| this.price = product.getPrice(); | ||
| } | ||
|
|
||
| public Product toDomain() { | ||
| return new Product(id, name, price); | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public Name getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public Price getPrice() { | ||
| return price; | ||
| } | ||
| } | ||
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.
νμ¬ μ ν리μΌμ΄μ κΈ°λμ΄ μλκ³ μλ κ² κ°μμ.
λ€μ 리뷰 μμ² λλ, μ ν리μΌμ΄μ μ΄ κΈ°λλλ μνκ° λλλ‘ν΄μ£ΌμΈμ. π