-
Notifications
You must be signed in to change notification settings - Fork 177
๐ 2๋จ๊ณ - ๋ฆฌํฉํฐ๋ง(๋ฉ๋ด) #372
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: albertimkr
Are you sure you want to change the base?
Changes from 2 commits
ded1486
3d5e505
ec4ce24
ed8b06d
7407c96
1485f63
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,39 @@ | ||
| package kitchenpos.menus.tobe.application; | ||
|
|
||
| import kitchenpos.menus.tobe.domain.MenuGroup; | ||
| import kitchenpos.menus.tobe.domain.MenuGroupName; | ||
| import kitchenpos.menus.tobe.domain.MenuGroupRepository; | ||
| import kitchenpos.menus.tobe.ui.MenuGroupCreateRequest; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.UUID; | ||
|
|
||
| @Service("tobeMenuGroupService") | ||
| public class MenuGroupService { | ||
| private final MenuGroupRepository menuGroupRepository; | ||
|
|
||
| public MenuGroupService(final MenuGroupRepository menuGroupRepository) { | ||
| this.menuGroupRepository = menuGroupRepository; | ||
| } | ||
|
|
||
| @Transactional | ||
| public MenuGroup create(final MenuGroupCreateRequest request) { | ||
| final MenuGroupName name = new MenuGroupName(request.name()); | ||
| final MenuGroup menuGroup = new MenuGroup(UUID.randomUUID(), name); | ||
| return menuGroupRepository.save(menuGroup); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<MenuGroup> findAll() { | ||
| return menuGroupRepository.findAll(); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public MenuGroup findById(UUID menuGroupId) { | ||
| return menuGroupRepository.findById(menuGroupId) | ||
| .orElseThrow(NoSuchElementException::new); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package kitchenpos.menus.tobe.application; | ||
|
|
||
| import kitchenpos.menus.tobe.domain.MenuProduct; | ||
| import kitchenpos.menus.tobe.domain.MenuProductQuantity; | ||
| import kitchenpos.menus.tobe.infra.ProductResponse; | ||
| import kitchenpos.menus.tobe.infra.ProductResponses; | ||
| import kitchenpos.menus.tobe.ui.MenuProductRequest; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class MenuProductCreator { | ||
|
|
||
| private MenuProductCreator() { | ||
| throw new AssertionError(); | ||
| } | ||
|
|
||
| public static List<MenuProduct> create(List<MenuProductRequest> menuProductRequests, ProductResponses productResponses) { | ||
| final List<MenuProduct> menuProducts = new ArrayList<>(); | ||
| for (MenuProductRequest menuProductRequest : menuProductRequests) { | ||
| final MenuProductQuantity quantity = new MenuProductQuantity(menuProductRequest.quantity()); | ||
| final ProductResponse productResponse = productResponses.findById(menuProductRequest.productId()); | ||
| menuProducts.add(new MenuProduct(productResponse.id(), quantity)); | ||
| } | ||
| return menuProducts; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package kitchenpos.menus.tobe.application; | ||
|
|
||
| import kitchenpos.menus.tobe.domain.MenuPrice; | ||
| import kitchenpos.menus.tobe.domain.MenuProduct; | ||
| import kitchenpos.menus.tobe.domain.MenuProducts; | ||
| import kitchenpos.menus.tobe.infra.ProductClient; | ||
| import kitchenpos.menus.tobe.infra.ProductResponses; | ||
| import kitchenpos.menus.tobe.ui.MenuProductRequest; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Service | ||
| public class MenuProductService { | ||
|
|
||
| private final ProductClient productClient; | ||
|
|
||
| public MenuProductService(ProductClient productClient) { | ||
| this.productClient = productClient; | ||
| } | ||
|
|
||
| public List<MenuProduct> getMenuProducts(MenuPrice price, List<MenuProductRequest> menuProductRequests, List<UUID> productIds) { | ||
| MenuValidator.validateMenuProductRequests(menuProductRequests); | ||
| final ProductResponses productResponses = productClient.listByIds(productIds); | ||
| MenuValidator.validateProductResponses(productResponses, productIds); | ||
| MenuValidator.validateMenuPriceWithRequests(price, productResponses, menuProductRequests); | ||
| return MenuProductCreator.create(menuProductRequests, productResponses); | ||
| } | ||
|
|
||
| public void checkMenuProductPrice(final MenuProducts menuProducts, final MenuPrice price) { | ||
| List<UUID> productIds = menuProducts.getProductIds(); | ||
| final ProductResponses productResponses = productClient.listByIds(productIds); | ||
| MenuValidator.validateMenuPrice(price, productResponses, menuProducts); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package kitchenpos.menus.tobe.application; | ||
|
|
||
| import kitchenpos.menus.tobe.domain.*; | ||
| import kitchenpos.menus.tobe.ui.MenuCreateRequest; | ||
| import kitchenpos.menus.tobe.ui.MenuPriceChangeRequest; | ||
| import kitchenpos.products.infra.PurgomalumClient; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.UUID; | ||
|
|
||
| @Service("tobeMenuService") | ||
| public class MenuService { | ||
|
|
||
| private final MenuRepository menuRepository; | ||
| private final MenuGroupService menuGroupService; | ||
| private final PurgomalumClient purgomalumClient; | ||
| private final MenuProductService menuProductService; | ||
|
|
||
| public MenuService( | ||
| final MenuRepository menuRepository, | ||
| final MenuGroupService menuGroupService, | ||
| final PurgomalumClient purgomalumClient, | ||
| final MenuProductService menuProductService | ||
| ) { | ||
| this.menuRepository = menuRepository; | ||
| this.menuGroupService = menuGroupService; | ||
| this.purgomalumClient = purgomalumClient; | ||
| this.menuProductService = menuProductService; | ||
| } | ||
|
|
||
| @Transactional | ||
| public Menu create(final MenuCreateRequest request) { | ||
| final MenuPrice price = new MenuPrice(request.price()); | ||
| final MenuGroup menuGroup = menuGroupService.findById(request.menuGroupId()); | ||
| final List<MenuProduct> menuProducts = menuProductService.getMenuProducts(price, request.menuProducts(), request.productIds()); | ||
|
||
| final MenuName name = new MenuName(request.name(), purgomalumClient::containsProfanity); | ||
| final Menu menu = new Menu(UUID.randomUUID(), name, price, menuGroup, request.displayed(), menuProducts); | ||
| return menuRepository.save(menu); | ||
| } | ||
|
|
||
| @Transactional | ||
| public Menu changePrice(final UUID menuId, final MenuPriceChangeRequest request) { | ||
| final MenuPrice price = new MenuPrice(request.price()); | ||
| final Menu menu = menuRepository.findById(menuId) | ||
| .orElseThrow(NoSuchElementException::new); | ||
| menuProductService.checkMenuProductPrice(menu.getMenuProducts(), price); | ||
| menu.changePrice(price); | ||
| return menu; | ||
| } | ||
|
|
||
| @Transactional | ||
| public Menu display(final UUID menuId) { | ||
| final Menu menu = menuRepository.findById(menuId) | ||
| .orElseThrow(NoSuchElementException::new); | ||
| menuProductService.checkMenuProductPrice(menu.getMenuProducts(), menu.getPrice()); | ||
AlbertImKr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| menu.display(); | ||
| return menu; | ||
| } | ||
|
|
||
| @Transactional | ||
| public Menu hide(final UUID menuId) { | ||
| final Menu menu = menuRepository.findById(menuId) | ||
| .orElseThrow(NoSuchElementException::new); | ||
| menu.hide(); | ||
| return menu; | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<Menu> findAll() { | ||
| return menuRepository.findAll(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package kitchenpos.menus.tobe.application; | ||
|
|
||
| import kitchenpos.menus.tobe.domain.MenuPrice; | ||
| import kitchenpos.menus.tobe.domain.MenuProducts; | ||
| import kitchenpos.menus.tobe.infra.ProductResponses; | ||
| import kitchenpos.menus.tobe.ui.MenuProductRequest; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.UUID; | ||
|
|
||
| public class MenuValidator { | ||
|
|
||
| private MenuValidator() { | ||
| throw new AssertionError(); | ||
| } | ||
|
|
||
| public static void validateMenuPriceWithRequests(MenuPrice price, ProductResponses productResponses, List<MenuProductRequest> menuProductRequests) { | ||
| var sum = productResponses.getSumsWithRequests(menuProductRequests); | ||
| if (price.isGreaterThan(sum)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public static void validateMenuPrice(MenuPrice price, ProductResponses productResponses, MenuProducts menuProducts) { | ||
| var sum = productResponses.getSums(menuProducts); | ||
| if (price.isGreaterThan(sum)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public static void validateProductResponses(ProductResponses productResponses, List<UUID> productIds) { | ||
| if (productResponses.size() != productIds.size()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public static void validateMenuProductRequests(List<MenuProductRequest> menuProductRequests) { | ||
| if (Objects.isNull(menuProductRequests) || menuProductRequests.isEmpty()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package kitchenpos.menus.tobe.domain; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Repository("menuGroupToBeRepository") | ||
| public interface JpaMenuGroupRepository extends MenuGroupRepository, JpaRepository<MenuGroup, UUID> { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package kitchenpos.menus.tobe.domain; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Repository("menuToBeRepository") | ||
| public interface JpaMenuRepository extends MenuRepository, JpaRepository<Menu, UUID> { | ||
| @Query("select m from Menu m join m.menuProducts mp where mp.product.id = :productId") | ||
| @Override | ||
| List<Menu> findAllByProductId(@Param("productId") UUID productId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package kitchenpos.menus.tobe.domain; | ||
|
|
||
| import jakarta.persistence.*; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Table(name = "menu") | ||
| @Entity(name = "tobeMenu") | ||
| public class Menu { | ||
| @Column(name = "id", columnDefinition = "binary(16)") | ||
| @Id | ||
| private UUID id; | ||
|
|
||
| @Embedded | ||
| private MenuName name; | ||
|
|
||
| @Embedded | ||
| private MenuPrice price; | ||
|
|
||
| @ManyToOne(optional = false) | ||
| @JoinColumn( | ||
| name = "menu_group_id", | ||
| columnDefinition = "binary(16)", | ||
| foreignKey = @ForeignKey(name = "fk_menu_to_menu_group") | ||
| ) | ||
| private MenuGroup menuGroup; | ||
|
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. MenuGroup์ ์ง์ ์ฐธ์กฐ๋ก ๊ตฌํํด์ฃผ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค ๐
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. ํ Context์์ ์กด์ฌํ์ฌ ๋ง์ฝ์ Id๋ก ์ฐธ์กฐํ๋ฉด ์คํ๋ ค ๋ ๋ณต์กํ ๊ตฌ์กฐ๊ฐ ๋ ์ ์๋ค๊ณ ์๊ฐํ์ด์, |
||
|
|
||
| @Column(name = "displayed", nullable = false) | ||
| private boolean displayed; | ||
|
|
||
| @Embedded | ||
| private MenuProducts menuProducts; | ||
|
|
||
| @Transient | ||
| private UUID menuGroupId; | ||
|
|
||
| protected Menu() { | ||
| } | ||
|
|
||
| public Menu(UUID id, MenuName name, MenuPrice price, MenuGroup menuGroup, boolean displayed, List<MenuProduct> menuProducts) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.price = price; | ||
| this.menuGroup = menuGroup; | ||
| this.displayed = displayed; | ||
| this.menuProducts = new MenuProducts(menuProducts); | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void changePrice(MenuPrice price) { | ||
|
||
| this.price = price; | ||
| } | ||
|
|
||
| public MenuProducts getMenuProducts() { | ||
| return this.menuProducts; | ||
| } | ||
|
|
||
| public void display() { | ||
| this.displayed = true; | ||
| } | ||
|
|
||
| public void hide() { | ||
| this.displayed = false; | ||
| } | ||
|
|
||
| public MenuPrice getPrice() { | ||
| return price; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package kitchenpos.menus.tobe.domain; | ||
|
|
||
| import jakarta.persistence.*; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Table(name = "menu_group") | ||
| @Entity(name = "tobeMenuGroup") | ||
| public class MenuGroup { | ||
| @Column(name = "id", columnDefinition = "binary(16)") | ||
| @Id | ||
| private UUID id; | ||
|
|
||
| @Embedded | ||
| private MenuGroupName name; | ||
|
|
||
| protected MenuGroup() { | ||
| } | ||
|
|
||
| public MenuGroup(final UUID id, final MenuGroupName name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package kitchenpos.menus.tobe.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Embeddable; | ||
|
|
||
| @Embeddable | ||
| public class MenuGroupName { | ||
|
|
||
| @Column(name = "name", nullable = false) | ||
| private String name; | ||
|
|
||
| protected MenuGroupName() { | ||
| } | ||
|
|
||
| public MenuGroupName(final String name) { | ||
| if (name == null || name.isEmpty()) { | ||
| throw new IllegalArgumentException("๋ฉ๋ด ๊ทธ๋ฃน ์ด๋ฆ์ ํ์๋ก ์ ๋ ฅํด์ผ ํฉ๋๋ค."); | ||
| } | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String value() { | ||
| return name; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package kitchenpos.menus.tobe.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| public interface MenuGroupRepository { | ||
| MenuGroup save(MenuGroup menuGroup); | ||
|
|
||
| Optional<MenuGroup> findById(UUID id); | ||
|
|
||
| List<MenuGroup> findAll(); | ||
| } |
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.
Application Service ๊ฐ์ ๊ฒฐํฉ์ด ๋ฐ์ํ๋ฉด ์ด๋ค ๋ฌธ์ ์ ์ด ์์๊น์ ?
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.
์ ๊ฐ ์๊ฐํ๋ Application Service ๊ฐ์ ๊ฒฐํฉ์ด ๋ฐ์ํ ๋ ๋ํ๋ ์ ์๋ ๋ฌธ์ ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.