Skip to content
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

Open
wants to merge 10 commits into
base: choieungi
Choose a base branch
from
Open
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ docker compose -p kitchenpos up -d

- ์ƒํ’ˆ์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ์ƒํ’ˆ์˜ ์ด๋ฆ„์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ์ƒํ’ˆ์˜ ์ด๋ฆ„์—๋Š” ๋น„์†์–ด๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์—†๋‹ค.
- ์ƒํ’ˆ์˜ ์ด๋ฆ„์—๋Š” ๋น„์†์–ด๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์—†๋‹ค.
- ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
- ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์ด ๋ณ€๊ฒฝ๋  ๋•Œ ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์ด ๋ฉ”๋‰ด์— ์†ํ•œ ์ƒํ’ˆ ๊ธˆ์•ก์˜ ํ•ฉ๋ณด๋‹ค ํฌ๋ฉด ๋ฉ”๋‰ด๊ฐ€ ์ˆจ๊ฒจ์ง„๋‹ค.
- ์ƒํ’ˆ์˜ ๋ชฉ๋ก์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

### ๋ฉ”๋‰ด ๊ทธ๋ฃน

- ๋ฉ”๋‰ด ๊ทธ๋ฃน์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.
- ๋ฉ”๋‰ด ๊ทธ๋ฃน์˜ ์ด๋ฆ„์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด ๊ทธ๋ฃน์˜ ์ด๋ฆ„์€ ๋น„์›Œ ๋‘˜ ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด ๊ทธ๋ฃน์˜ ์ด๋ฆ„์€ ๋น„์›Œ ๋‘˜ ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด ๊ทธ๋ฃน์˜ ๋ชฉ๋ก์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

### ๋ฉ”๋‰ด
Expand All @@ -35,14 +35,14 @@ docker compose -p kitchenpos up -d
- ์ƒํ’ˆ์ด ์—†์œผ๋ฉด ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด์— ์†ํ•œ ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์€ 0 ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ๋ฉ”๋‰ด์— ์†ํ•œ ์ƒํ’ˆ ๊ธˆ์•ก์˜ ํ•ฉ์€ ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
- ๋ฉ”๋‰ด๋Š” ํŠน์ • ๋ฉ”๋‰ด ๊ทธ๋ฃน์— ์†ํ•ด์•ผ ํ•œ๋‹ค.
- ๋ฉ”๋‰ด์˜ ์ด๋ฆ„์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด์˜ ์ด๋ฆ„์—๋Š” ๋น„์†์–ด๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด์˜ ์ด๋ฆ„์—๋Š” ๋น„์†์–ด๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
- ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ๋ฉ”๋‰ด์— ์†ํ•œ ์ƒํ’ˆ ๊ธˆ์•ก์˜ ํ•ฉ์€ ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
- ๋ฉ”๋‰ด๋ฅผ ๋…ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
- ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์ด ๋ฉ”๋‰ด์— ์†ํ•œ ์ƒํ’ˆ ๊ธˆ์•ก์˜ ํ•ฉ๋ณด๋‹ค ๋†’์„ ๊ฒฝ์šฐ ๋ฉ”๋‰ด๋ฅผ ๋…ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค.
Expand All @@ -53,13 +53,13 @@ docker compose -p kitchenpos up -d

- ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์€ ๋น„์›Œ ๋‘˜ ์ˆ˜ ์—†๋‹ค.
- ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์€ ๋น„์›Œ ๋‘˜ ์ˆ˜ ์—†๋‹ค.
- ๋นˆ ํ…Œ์ด๋ธ”์„ ํ•ด์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
- ๋นˆ ํ…Œ์ด๋ธ”๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์™„๋ฃŒ๋˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ด ์žˆ๋Š” ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์€ ๋นˆ ํ…Œ์ด๋ธ”๋กœ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ฐฉ๋ฌธํ•œ ์†๋‹˜ ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
- ๋ฐฉ๋ฌธํ•œ ์†๋‹˜ ์ˆ˜๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ฐฉ๋ฌธํ•œ ์†๋‹˜ ์ˆ˜๋Š” 0 ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ๋ฐฉ๋ฌธํ•œ ์†๋‹˜ ์ˆ˜๋Š” 0 ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ๋นˆ ํ…Œ์ด๋ธ”์€ ๋ฐฉ๋ฌธํ•œ ์†๋‹˜ ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
- ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์˜ ๋ชฉ๋ก์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

Expand All @@ -73,7 +73,7 @@ docker compose -p kitchenpos up -d
- ๋งค์žฅ ์ฃผ๋ฌธ์€ ์ฃผ๋ฌธ ํ•ญ๋ชฉ์˜ ์ˆ˜๋Ÿ‰์ด 0 ๋ฏธ๋งŒ์ผ ์ˆ˜ ์žˆ๋‹ค.
- ๋งค์žฅ ์ฃผ๋ฌธ์„ ์ œ์™ธํ•œ ์ฃผ๋ฌธ์˜ ๊ฒฝ์šฐ ์ฃผ๋ฌธ ํ•ญ๋ชฉ์˜ ์ˆ˜๋Ÿ‰์€ 0 ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.
- ๋ฐฐ๋‹ฌ ์ฃผ์†Œ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋ฐฐ๋‹ฌ ์ฃผ๋ฌธ์„ ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ฐฐ๋‹ฌ ์ฃผ์†Œ๋Š” ๋น„์›Œ ๋‘˜ ์ˆ˜ ์—†๋‹ค.
- ๋ฐฐ๋‹ฌ ์ฃผ์†Œ๋Š” ๋น„์›Œ ๋‘˜ ์ˆ˜ ์—†๋‹ค.
- ๋นˆ ํ…Œ์ด๋ธ”์—๋Š” ๋งค์žฅ ์ฃผ๋ฌธ์„ ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.
- ์ˆจ๊ฒจ์ง„ ๋ฉ”๋‰ด๋Š” ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†๋‹ค.
- ์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์€ ์‹ค์ œ ๋ฉ”๋‰ด ๊ฐ€๊ฒฉ๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/kitchenpos/menus/application/MenuGroupService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kitchenpos.menus.domain.MenuGroup;
import kitchenpos.menus.domain.MenuGroupRepository;
import kitchenpos.menus.domain.dto.MenuGroupRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -18,14 +19,9 @@ public MenuGroupService(final MenuGroupRepository menuGroupRepository) {
}

@Transactional
public MenuGroup create(final MenuGroup request) {
public MenuGroup create(final MenuGroupRequest request) {
final String name = request.getName();
if (Objects.isNull(name) || name.isEmpty()) {
throw new IllegalArgumentException();
}
final MenuGroup menuGroup = new MenuGroup();
menuGroup.setId(UUID.randomUUID());
menuGroup.setName(name);
final MenuGroup menuGroup = new MenuGroup(UUID.randomUUID(), name);
return menuGroupRepository.save(menuGroup);
}

Expand Down
111 changes: 42 additions & 69 deletions src/main/java/kitchenpos/menus/application/MenuService.java
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
Expand All @@ -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;
Expand All @@ -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) {
Copy link

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 ์™€ ๊ฐ™์€ ๋„ค์ด๋ฐ์„ ์‚ฌ์šฉํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋งŒ ๋ถ€ํƒ๋“œ๋ ค์š” !

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(
Copy link

Choose a reason for hiding this comment

The 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;
}

Expand Down
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) {
Copy link

Choose a reason for hiding this comment

The 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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.UUID;

public interface JpaMenuRepository extends MenuRepository, JpaRepository<Menu, UUID> {
@Query("select m from Menu m join m.menuProducts mp where mp.product.id = :productId")
@Query("select m from Menu m join m.menuProducts mp where mp.productId = :productId")
@Override
List<Menu> findAllByProductId(@Param("productId") UUID productId);
}
Loading