-
Notifications
You must be signed in to change notification settings - Fork 170
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단계 - 리팩터링(메뉴) #233
Open
eleeje97
wants to merge
6
commits into
next-step:eleeje97
Choose a base branch
from
eleeje97:step2
base: eleeje97
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
2단계 - 리팩터링(메뉴) #233
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
493f49d
feat: MenuGroup 도메인
eleeje97 146d968
feat: MenuProduct, Menu 도메인, DisplayedName, MenuPrice VO
eleeje97 07e4eae
feat: Menu도메인 생성자에서 menuGroupId 삭제
eleeje97 45a509c
feat: MenuProduct 도메인 생성자에서 productId 삭제
eleeje97 d978d4a
test: 테스트 픽스쳐 수정
eleeje97 61c365e
test: 테스트 코드 작성
eleeje97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
package kitchenpos.menus.application; | ||
|
||
import kitchenpos.menus.domain.*; | ||
import kitchenpos.menus.tobe.domain.Menu; | ||
import kitchenpos.menus.tobe.domain.MenuProduct; | ||
import kitchenpos.menus.tobe.domain.MenuGroup; | ||
import kitchenpos.menus.tobe.dto.MenuCreateRequestDto; | ||
import kitchenpos.menus.tobe.dto.MenuPriceChangeRequestDto; | ||
import kitchenpos.products.tobe.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.stream.Collectors; | ||
|
||
|
@@ -31,107 +35,59 @@ 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 MenuCreateRequestDto request) { | ||
final MenuGroup menuGroup = menuGroupRepository.findById(request.getMenuGroupId()) | ||
.orElseThrow(NoSuchElementException::new); | ||
final List<MenuProduct> menuProductRequests = request.getMenuProducts(); | ||
if (Objects.isNull(menuProductRequests) || menuProductRequests.isEmpty()) { | ||
.orElseThrow(NoSuchElementException::new); | ||
|
||
if (Objects.isNull(request.getMenuProducts()) || request.getMenuProducts().isEmpty()) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
final List<Product> products = productRepository.findAllByIdIn( | ||
menuProductRequests.stream() | ||
request.getMenuProducts().stream() | ||
.map(MenuProduct::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(); | ||
} | ||
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); | ||
menuProducts.add(menuProduct); | ||
} | ||
if (price.compareTo(sum) > 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
final String name = request.getName(); | ||
if (Objects.isNull(name) || purgomalumClient.containsProfanity(name)) { | ||
|
||
if (products.size() != request.getMenuProducts().size()) { | ||
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); | ||
|
||
List<MenuProduct> menuProducts = request.getMenuProducts().stream() | ||
.map(menuProduct -> { | ||
Product product = productRepository.findById(menuProduct.getProductId()) | ||
.orElseThrow(NoSuchElementException::new); | ||
return new MenuProduct(product, menuProduct.getQuantity()); | ||
}) | ||
.collect(Collectors.toList()); | ||
|
||
final Menu menu = new Menu(request.getName(), request.getPrice(), | ||
menuGroup, request.isDisplayed(), menuProducts, purgomalumClient); | ||
|
||
return menuRepository.save(menu); | ||
} | ||
|
||
@Transactional | ||
public Menu changePrice(final UUID menuId, final Menu request) { | ||
final BigDecimal price = request.getPrice(); | ||
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
public Menu changePrice(final UUID menuId, final MenuPriceChangeRequestDto 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 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())) | ||
); | ||
} | ||
if (price.compareTo(sum) > 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
menu.setPrice(price); | ||
menu.changePrice(request.getPrice()); | ||
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())) | ||
); | ||
} | ||
if (menu.getPrice().compareTo(sum) > 0) { | ||
throw new IllegalStateException(); | ||
} | ||
menu.setDisplayed(true); | ||
menu.display(); | ||
return menu; | ||
} | ||
|
||
@Transactional | ||
public Menu hide(final UUID menuId) { | ||
final Menu menu = menuRepository.findById(menuId) | ||
.orElseThrow(NoSuchElementException::new); | ||
menu.setDisplayed(false); | ||
menu.hide(); | ||
return menu; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/kitchenpos/menus/tobe/domain/DisplayedName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package kitchenpos.menus.tobe.domain; | ||
|
||
import kitchenpos.products.infra.PurgomalumClient; | ||
|
||
import java.util.Objects; | ||
|
||
|
||
public class DisplayedName { | ||
private String name; | ||
|
||
public DisplayedName() { | ||
} | ||
|
||
public DisplayedName(String name, PurgomalumClient purgomalumClient) { | ||
if (Objects.isNull(name) || purgomalumClient.containsProfanity(name)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Menu Service 에서 Product 도메인과의 결합을 제거해볼수 있을까요 ?
외부 도메인과 협력을 해야하는 로직들을 DomainService로 정의해볼수 있을것 같습니다 😄