Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kitchenpos.menus.tobe.application;

import kitchenpos.menus.tobe.domain.ProfanityChecker;
import org.springframework.stereotype.Component;

@Component("tobeMenuContextProfanityChecker")
public class DefaultProfanityChecker implements ProfanityChecker {

private final PurgomalumClient purgomalumClient;

public DefaultProfanityChecker(final PurgomalumClient purgomalumClient) {
this.purgomalumClient = purgomalumClient;
}

@Override
public boolean containsProfanity(final String text) {
return purgomalumClient.containsProfanity(text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kitchenpos.menus.tobe.application;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

public record MenuCreateRequest(
BigDecimal price,
String name,
boolean displayed,
UUID menuGroupId,
List<MenuProductRequest> menuProducts
) {
public List<UUID> productIds() {
return menuProducts.stream()
.map(MenuProductRequest::productId)
.toList();
}
}
48 changes: 48 additions & 0 deletions src/main/java/kitchenpos/menus/tobe/application/MenuFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package kitchenpos.menus.tobe.application;

import kitchenpos.menus.tobe.domain.*;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Component
public class MenuFacade {
private final MenuService menuService;
private final MenuGroupService menuGroupService;
private final ProductClient productClient;
private final ProfanityChecker profanityChecker;


public MenuFacade(MenuService menuService, MenuGroupService menuGroupService, ProductClient productClient, ProfanityChecker profanityChecker) {
this.menuService = menuService;
this.menuGroupService = menuGroupService;
this.productClient = productClient;
this.profanityChecker = profanityChecker;
}

@Transactional
public Menu create(MenuCreateRequest request) {
final MenuName name = new MenuName(request.name(), profanityChecker);
final MenuPrice price = new MenuPrice(request.price());
final MenuGroup menuGroup = menuGroupService.findById(request.menuGroupId());
final ProductInfos productInfos = productClient.listByIds(request.productIds());
final MenuProductCatalog menuProductCatalog = new MenuProductCatalog(request.menuProducts(), productInfos);
return menuService.create(name, price, menuGroup, request.displayed(), menuProductCatalog);
}

@Transactional
public Menu changePrice(UUID menuId, MenuPriceChangeRequest request) {
final MenuPrice price = new MenuPrice(request.price());
final Menu menu = menuService.findById(menuId);
final ProductInfos productInfos = productClient.listByIds(menu.findProductIds());
return menuService.changePrice(menuId, price, productInfos);
}

@Transactional
public Menu display(UUID menuId) {
final Menu menu = menuService.findById(menuId);
final ProductInfos productInfos = productClient.listByIds(menu.findProductIds());
return menuService.display(menuId, productInfos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package kitchenpos.menus.tobe.application;

public record MenuGroupCreateRequest(String name) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kitchenpos.menus.tobe.application;

import kitchenpos.menus.tobe.domain.MenuGroup;
import kitchenpos.menus.tobe.domain.MenuGroupName;
import kitchenpos.menus.tobe.domain.MenuGroupRepository;
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,6 @@
package kitchenpos.menus.tobe.application;

import java.math.BigDecimal;

public record MenuPriceChangeRequest(BigDecimal price) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kitchenpos.menus.tobe.application;

import java.util.UUID;

public record MenuProductRequest(UUID productId, long quantity) {

}
60 changes: 60 additions & 0 deletions src/main/java/kitchenpos/menus/tobe/application/MenuService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package kitchenpos.menus.tobe.application;

import kitchenpos.menus.tobe.domain.*;
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;

public MenuService(final MenuRepository menuRepository) {
this.menuRepository = menuRepository;
}

@Transactional
public Menu create(final MenuName name, final MenuPrice price, final MenuGroup menuGroup, final boolean displayed, final MenuProductCatalog menuProductCatalog) {
final Menu menu = new Menu(UUID.randomUUID(), name, price, menuGroup, displayed, menuProductCatalog);
return menuRepository.save(menu);
}

@Transactional
public Menu changePrice(final UUID menuId, final MenuPrice price, final ProductInfos productInfos) {
final Menu menu = menuRepository.findById(menuId)
.orElseThrow(NoSuchElementException::new);
menu.changePrice(price, productInfos);
return menu;
}

@Transactional
public Menu display(final UUID menuId, final ProductInfos productInfos) {
final Menu menu = menuRepository.findById(menuId)
.orElseThrow(NoSuchElementException::new);
menu.display(productInfos);
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();
}

@Transactional(readOnly = true)
public Menu findById(UUID menuId) {
return menuRepository.findById(menuId)
.orElseThrow(NoSuchElementException::new);
}
}
10 changes: 10 additions & 0 deletions src/main/java/kitchenpos/menus/tobe/application/ProductClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kitchenpos.menus.tobe.application;

import kitchenpos.menus.tobe.domain.ProductInfos;

import java.util.List;
import java.util.UUID;

public interface ProductClient {
ProductInfos listByIds(List<UUID> productIds);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kitchenpos.menus.tobe.application;

public interface PurgomalumClient {

boolean containsProfanity(String text);
}
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> {
}
16 changes: 16 additions & 0 deletions src/main/java/kitchenpos/menus/tobe/domain/JpaMenuRepository.java
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);
}
83 changes: 83 additions & 0 deletions src/main/java/kitchenpos/menus/tobe/domain/Menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package kitchenpos.menus.tobe.domain;

import jakarta.persistence.*;

import java.math.BigDecimal;
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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MenuGroup์„ ์ง์ ‘ ์ฐธ์กฐ๋กœ ๊ตฌํ˜„ํ•ด์ฃผ์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜„

Copy link
Author

Choose a reason for hiding this comment

The 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, MenuProductCatalog menuProductCatalog) {
validatePrice(price, menuProductCatalog.calculateTotalPrice());
this.id = id;
this.name = name;
this.price = price;
this.menuGroup = menuGroup;
this.displayed = displayed;
this.menuProducts = new MenuProducts(menuProductCatalog);
}

private void validatePrice(MenuPrice price, BigDecimal total) {
if (!price.isGreaterThan(total)) {
throw new IllegalArgumentException("๋ฉ”๋‰ด ๊ฐ€๊ฒฉ์ด ์ „์ฒด ๋ฉ”๋‰ด ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ ํ•ฉ๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์•„์•ผ ํ•œ๋‹ค");
}
}

public UUID getId() {
return id;
}

public void changePrice(MenuPrice price, ProductInfos productInfos) {
validatePrice(price, menuProducts.calculateTotalPrice(productInfos));
this.price = price;
}

public List<UUID> findProductIds() {
return menuProducts.getProductIds();
}

public void display(ProductInfos productInfos) {
validatePrice(price, menuProducts.calculateTotalPrice(productInfos));
this.displayed = true;
}

public void hide() {
this.displayed = false;
}

public MenuPrice getPrice() {
return price;
}
}
28 changes: 28 additions & 0 deletions src/main/java/kitchenpos/menus/tobe/domain/MenuGroup.java
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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/kitchenpos/menus/tobe/domain/MenuGroupName.java
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;
}
}
Loading