-
Notifications
You must be signed in to change notification settings - Fork 155
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
๐ 2๋จ๊ณ - ๋ฆฌํฉํฐ๋ง(๋ฉ๋ด) #144
base: choieungi
Are you sure you want to change the base?
Changes from all commits
eaedced
8edd801
3ef3acf
bb67269
91a2de3
a5c3f49
47d3a4d
d1e16a2
346af00
2091229
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 |
---|---|---|
@@ -1,14 +1,20 @@ | ||
package kitchenpos.menus.application; | ||
|
||
import kitchenpos.menus.domain.*; | ||
import kitchenpos.menus.domain.dto.MenuPriceRequest; | ||
import kitchenpos.menus.domain.dto.MenuRequest; | ||
import kitchenpos.menus.domain.dto.MenuRequest.MenuProductRequest; | ||
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.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
|
@@ -19,10 +25,10 @@ public class MenuService { | |
private final PurgomalumClient purgomalumClient; | ||
|
||
public MenuService( | ||
final MenuRepository menuRepository, | ||
final MenuGroupRepository menuGroupRepository, | ||
final ProductRepository productRepository, | ||
final PurgomalumClient purgomalumClient | ||
final MenuRepository menuRepository, | ||
final MenuGroupRepository menuGroupRepository, | ||
final ProductRepository productRepository, | ||
final PurgomalumClient purgomalumClient | ||
) { | ||
this.menuRepository = menuRepository; | ||
this.menuGroupRepository = menuGroupRepository; | ||
|
@@ -31,107 +37,74 @@ public MenuService( | |
} | ||
|
||
@Transactional | ||
public Menu create(final Menu request) { | ||
final BigDecimal price = request.getPrice(); | ||
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
public Menu create(final MenuRequest request) { | ||
final MenuGroup menuGroup = menuGroupRepository.findById(request.getMenuGroupId()) | ||
.orElseThrow(NoSuchElementException::new); | ||
final List<MenuProduct> menuProductRequests = request.getMenuProducts(); | ||
if (Objects.isNull(menuProductRequests) || menuProductRequests.isEmpty()) { | ||
throw new IllegalArgumentException(); | ||
} | ||
.orElseThrow(NoSuchElementException::new); | ||
final List<MenuProduct> menuProducts = new ArrayList<>(); | ||
final Menu menu = new Menu(UUID.randomUUID(), request.getName(), request.getPrice(), menuGroup, request.isDisplayed(), menuProducts); | ||
|
||
final List<MenuProductRequest> menuProductRequests = request.getMenuProducts(); | ||
final List<Product> products = productRepository.findAllByIdIn( | ||
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. ๋ฉ๋ด ์ปจํ
์คํธ์์ ์ํ ์กฐํ๋ฅผ ํ๊ฒ๋๋ค๋ฉด ์ปจํ
์คํธ๊ฐ ์นจ๋ฒ ๋ฐ ๊ฒฐํฉ์ด ๋ฐ์ํ์๋ค๊ณ ๋ณผ์ ์์ด์. |
||
menuProductRequests.stream() | ||
.map(MenuProduct::getProductId) | ||
.collect(Collectors.toList()) | ||
menuProductRequests.stream() | ||
.map(MenuProductRequest::getProductId) | ||
.collect(Collectors.toList()) | ||
); | ||
|
||
if (products.size() != menuProductRequests.size()) { | ||
throw new IllegalArgumentException(); | ||
} | ||
final List<MenuProduct> menuProducts = new ArrayList<>(); | ||
BigDecimal sum = BigDecimal.ZERO; | ||
for (final MenuProduct menuProductRequest : menuProductRequests) { | ||
final long quantity = menuProductRequest.getQuantity(); | ||
if (quantity < 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
for (final MenuProductRequest menuProductRequest : menuProductRequests) { | ||
final Product product = productRepository.findById(menuProductRequest.getProductId()) | ||
.orElseThrow(NoSuchElementException::new); | ||
sum = sum.add( | ||
product.getPrice() | ||
.multiply(BigDecimal.valueOf(quantity)) | ||
); | ||
final MenuProduct menuProduct = new MenuProduct(); | ||
menuProduct.setProduct(product); | ||
menuProduct.setQuantity(quantity); | ||
.orElseThrow(NoSuchElementException::new); | ||
|
||
final MenuProduct menuProduct = new MenuProduct(product.getId(), product.getPrice(), menuProductRequest.getQuantity()); | ||
menuProducts.add(menuProduct); | ||
} | ||
if (price.compareTo(sum) > 0) { | ||
|
||
BigDecimal sum = menu.calculateMenuProductsPrice(); | ||
|
||
if (menu.getPrice().compareTo(sum) > 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
final String name = request.getName(); | ||
if (Objects.isNull(name) || purgomalumClient.containsProfanity(name)) { | ||
|
||
if (purgomalumClient.containsProfanity(menu.getName())) { | ||
throw new IllegalArgumentException(); | ||
} | ||
final Menu menu = new Menu(); | ||
menu.setId(UUID.randomUUID()); | ||
menu.setName(name); | ||
menu.setPrice(price); | ||
menu.setMenuGroup(menuGroup); | ||
menu.setDisplayed(request.isDisplayed()); | ||
menu.setMenuProducts(menuProducts); | ||
return menuRepository.save(menu); | ||
} | ||
|
||
@Transactional | ||
public Menu changePrice(final UUID menuId, final Menu request) { | ||
public Menu changePrice(final UUID menuId, final MenuPriceRequest request) { | ||
final BigDecimal price = request.getPrice(); | ||
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
final Menu menu = menuRepository.findById(menuId) | ||
.orElseThrow(NoSuchElementException::new); | ||
BigDecimal sum = BigDecimal.ZERO; | ||
for (final MenuProduct menuProduct : menu.getMenuProducts()) { | ||
sum = sum.add( | ||
menuProduct.getProduct() | ||
.getPrice() | ||
.multiply(BigDecimal.valueOf(menuProduct.getQuantity())) | ||
); | ||
} | ||
.orElseThrow(NoSuchElementException::new); | ||
menu.changePrice(price); | ||
|
||
BigDecimal sum = menu.calculateMenuProductsPrice(); | ||
|
||
if (price.compareTo(sum) > 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
menu.setPrice(price); | ||
return menu; | ||
} | ||
|
||
@Transactional | ||
public Menu display(final UUID menuId) { | ||
final Menu menu = menuRepository.findById(menuId) | ||
.orElseThrow(NoSuchElementException::new); | ||
BigDecimal sum = BigDecimal.ZERO; | ||
for (final MenuProduct menuProduct : menu.getMenuProducts()) { | ||
sum = sum.add( | ||
menuProduct.getProduct() | ||
.getPrice() | ||
.multiply(BigDecimal.valueOf(menuProduct.getQuantity())) | ||
); | ||
} | ||
.orElseThrow(NoSuchElementException::new); | ||
BigDecimal sum = menu.calculateMenuProductsPrice(); | ||
if (menu.getPrice().compareTo(sum) > 0) { | ||
throw new IllegalStateException(); | ||
} | ||
menu.setDisplayed(true); | ||
menu.changeDisplay(true); | ||
return menu; | ||
} | ||
|
||
@Transactional | ||
public Menu hide(final UUID menuId) { | ||
final Menu menu = menuRepository.findById(menuId) | ||
.orElseThrow(NoSuchElementException::new); | ||
menu.setDisplayed(false); | ||
.orElseThrow(NoSuchElementException::new); | ||
menu.changeDisplay(false); | ||
return menu; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package kitchenpos.menus.application; | ||
|
||
import kitchenpos.menus.domain.Menu; | ||
import kitchenpos.menus.domain.MenuProduct; | ||
import kitchenpos.menus.domain.MenuRepository; | ||
import kitchenpos.products.application.ProductPriceChangeEvent; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
|
||
@Component | ||
public class ProductPriceChangeEventListener implements ApplicationListener<ProductPriceChangeEvent> { | ||
|
||
private final MenuRepository menuRepository; | ||
|
||
public ProductPriceChangeEventListener(MenuRepository menuRepository) { | ||
this.menuRepository = menuRepository; | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(ProductPriceChangeEvent event) { | ||
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 List<Menu> menus = menuRepository.findAllByProductId(event.getProductId()); | ||
for (final Menu menu : menus) { | ||
BigDecimal sum = BigDecimal.ZERO; | ||
for (final MenuProduct menuProduct : menu.getMenuProducts()) { | ||
menuProduct.changePrice(event.getPrice()); | ||
sum = sum.add( | ||
menuProduct | ||
.getPrice() | ||
.multiply(BigDecimal.valueOf(menuProduct.getQuantity())) | ||
); | ||
} | ||
if (menu.getPrice().compareTo(sum) > 0) { | ||
menu.changeDisplay(false); | ||
} | ||
} | ||
} | ||
} |
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.
๋ฏธ์ ๋ด์ฉ๊ณผ๋ ์กฐ๊ธ ๋ฒ์ด๋ซ์ง๋ง ์ ๊ฐ Service Layer์ DTO๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ์๊ฐ ๋๋ฆด๊ฒ์.
ํ์ฌ๋ domain ํ์์ dto๋ก ์ ์ํด์ฃผ์ จ์ง๋ง DTO๋ฅผ Layer ๊ฐ ๊ตฌ๋ถ ์์ด ์ฌ์ฉํ ์๋ ์์ง๋ง Layer๊ฐ ๊ตฌ๋ถ์ ํ๋ค๋ฉด
MenuRequest ๊ฐ์ฒด๋ UI Layer ๊ณ์ธต์ ์ํ๋ DTO ๋ผ๊ณ ๋ณผ์ ์์ด์. (DTO๋ฅผ Layer๊ฐ ๊ตฌ๋ถ ์์ด ์ฌ์ฉํ๋ค๋ฉด ์ด๋์๊ฐ ๋ถํ์ํ ๋ฐ์ดํฐ๊ฐ ์นจ๋ฒํ ์ ์์ด์.)
ํ์ฌ Service Layer์ ์์ Layer๊ฐ ์นจ๋ฒํ๊ฒ์ ๋ณผ์ ์์ฃ .
๊ทธ๋ก ์ธํด ์ ํฌ ํ์์๋ Layer๊ฐ DTO ๋ถ๋ฆฌ๋ฅผ ํตํด ๊ณ์ธต์ ์๊ฒฉํ๊ฒ ๋๋๊ณ ์์ต๋๋ค.
UI Layer์์๋ Request / Response ์ ๊ฐ์ DTO ๋ค์ด๋ฐ์ ์ฌ์ฉํ๊ณ , Service Layer ๋ก ์ ๋ฌํ๊ธฐ ์ํด์ Command ์ ๊ฐ์ ๋ค์ด๋ฐ์ ์ฌ์ฉํ๊ณค ํฉ๋๋ค.
์ฐธ๊ณ ๋ง ๋ถํ๋๋ ค์ !