-
Notifications
You must be signed in to change notification settings - Fork 167
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
Step2 리펙터링(메뉴) 입니다. #103
Open
BaekSeungYeol
wants to merge
7
commits into
next-step:baekseungyeol
Choose a base branch
from
BaekSeungYeol:step2
base: baekseungyeol
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
Step2 리펙터링(메뉴) 입니다. #103
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
79ab5a3
test: Domain Menu 테스트 코드 추가
BaekSeungYeol 58c444b
feat: tobe Menu 원싯값 포장 및 분리
BaekSeungYeol cfead46
refactor: Menu 컨텍스트와 Product 컨텍스트 분리
BaekSeungYeol 80e18e5
feat(Value) : Value 클래스 추가
BaekSeungYeol a59b011
refactor: Menu 생성자 접근 제어자 변경
BaekSeungYeol 1b6038e
chore: 오타 제거
BaekSeungYeol 69f6c8d
feat: Value object 명시
BaekSeungYeol 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
25 changes: 25 additions & 0 deletions
25
src/main/java/kitchenpos/products/tobe/menu/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,25 @@ | ||
package kitchenpos.products.tobe.menu.domain; | ||
|
||
import kitchenpos.products.tobe.support.Value; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Embeddable; | ||
|
||
@Embeddable | ||
public class DisplayedName extends Value<DisplayedName> { | ||
|
||
@Column(name = "price", nullable = false) | ||
private String name; | ||
|
||
protected DisplayedName() { | ||
} | ||
|
||
public DisplayedName(String name, Profanities profanities) { | ||
if (profanities.contains(name)) { | ||
throw new IllegalArgumentException("이름에는 비속어가 포함될 수 없습니다."); | ||
} | ||
this.name = name; | ||
} | ||
|
||
} | ||
|
71 changes: 71 additions & 0 deletions
71
src/main/java/kitchenpos/products/tobe/menu/domain/Menu.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,71 @@ | ||
package kitchenpos.products.tobe.menu.domain; | ||
|
||
import kitchenpos.menus.domain.MenuGroup; | ||
|
||
import javax.persistence.*; | ||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
|
||
@Table(name = "menu") | ||
@Entity | ||
public class Menu { | ||
@Column(name = "id", columnDefinition = "varbinary(16)") | ||
@Id | ||
private UUID id; | ||
|
||
@Embedded | ||
private DisplayedName name; | ||
|
||
@Embedded | ||
private MenuPrice price; | ||
|
||
@Embedded | ||
private MenuProducts menuProducts; | ||
|
||
|
||
@ManyToOne(optional = false) | ||
@JoinColumn( | ||
name = "menu_group_id", | ||
columnDefinition = "varbinary(16)", | ||
foreignKey = @ForeignKey(name = "fk_menu_to_menu_group") | ||
) | ||
private MenuGroup menuGroup; | ||
|
||
@Column(name = "displayed", nullable = false) | ||
private boolean displayed; | ||
|
||
|
||
|
||
@Transient | ||
private UUID menuGroupId; | ||
|
||
protected Menu() { | ||
} | ||
|
||
private Menu(final DisplayedName name, final BigDecimal price, final List<MenuProduct> menuProducts) { | ||
this(null, name, new MenuPrice(price), menuProducts); | ||
} | ||
|
||
public Menu(final UUID id, final DisplayedName name, final BigDecimal price, final List<MenuProduct> menuProducts) { | ||
this(id, name, new MenuPrice(price), menuProducts); | ||
} | ||
|
||
public Menu(final UUID id, final DisplayedName name, final MenuPrice price, final List<MenuProduct> menuProducts) { | ||
validate(price, menuProducts); | ||
this.id = id; | ||
this.name = name; | ||
this.price = price; | ||
this.menuProducts = new MenuProducts(menuProducts); | ||
} | ||
|
||
private void validate(final MenuPrice price, final List<MenuProduct> menuProducts) { | ||
final int sum = menuProducts.stream().mapToInt(MenuProduct::amount).sum(); | ||
if (price.isGreaterThan(sum)) { | ||
throw new IllegalArgumentException("메뉴의 가격은 메뉴 상품 금액의 합보다 클 수 없습니다."); | ||
} | ||
} | ||
|
||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/kitchenpos/products/tobe/menu/domain/MenuPrice.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,34 @@ | ||
package kitchenpos.products.tobe.menu.domain; | ||
|
||
import kitchenpos.products.tobe.support.Value; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Embeddable; | ||
import java.math.BigDecimal; | ||
import java.util.Objects; | ||
|
||
@Embeddable | ||
public class MenuPrice extends Value<MenuPrice> { | ||
|
||
@Column(name = "price", nullable = false) | ||
private BigDecimal price; | ||
|
||
protected MenuPrice() { | ||
} | ||
|
||
public MenuPrice(final BigDecimal price) { | ||
validate(price); | ||
this.price = price; | ||
} | ||
|
||
private void validate(BigDecimal value) { | ||
if (Objects.isNull(value) || value.compareTo(BigDecimal.ZERO) < 0) { | ||
throw new IllegalArgumentException("가격은 비어있거나 음수가 될 수 없습니다."); | ||
} | ||
} | ||
|
||
|
||
public boolean isGreaterThan(final int price) { | ||
return this.price.intValue() > price; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/kitchenpos/products/tobe/menu/domain/MenuProduct.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,38 @@ | ||
package kitchenpos.products.tobe.menu.domain; | ||
|
||
import kitchenpos.products.domain.Product; | ||
|
||
import javax.persistence.*; | ||
import java.util.UUID; | ||
|
||
|
||
@Table(name = "menu_product") | ||
@Entity | ||
public class MenuProduct { | ||
@Column(name = "seq") | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Id | ||
private Long seq; | ||
@Transient | ||
private UUID productId; | ||
|
||
@Column(name = "price", nullable = false) | ||
private int price; | ||
|
||
@Column(name = "quantity", nullable = false) | ||
private int quantity; | ||
|
||
|
||
protected MenuProduct() { | ||
} | ||
|
||
public MenuProduct(UUID productId, int price, int quantity) { | ||
this.productId = productId; | ||
this.price = price; | ||
this.quantity = quantity; | ||
} | ||
|
||
public int amount() { | ||
return price * quantity; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/kitchenpos/products/tobe/menu/domain/MenuProducts.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,28 @@ | ||
package kitchenpos.products.tobe.menu.domain; | ||
|
||
import kitchenpos.products.tobe.support.Value; | ||
|
||
import javax.persistence.*; | ||
import java.util.List; | ||
|
||
@Embeddable | ||
public class MenuProducts extends Value<MenuProducts> { | ||
|
||
|
||
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) | ||
@JoinColumn( | ||
name = "menu_id", | ||
nullable = false, | ||
columnDefinition = "varbinary(16)", | ||
foreignKey = @ForeignKey(name = "fk_menu_product_to_menu") | ||
) | ||
private List<MenuProduct> menuProducts; | ||
|
||
|
||
protected MenuProducts() { | ||
} | ||
|
||
public MenuProducts(List<MenuProduct> menuProducts) { | ||
this.menuProducts = menuProducts; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/kitchenpos/products/tobe/menu/domain/Profanities.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,5 @@ | ||
package kitchenpos.products.tobe.menu.domain; | ||
|
||
interface Profanities { | ||
boolean contains(String text); | ||
} |
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
5 changes: 3 additions & 2 deletions
5
...pos/products/tobe/domain/ProductName.java → ...ucts/tobe/product/domain/ProductName.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
6 changes: 4 additions & 2 deletions
6
...os/products/tobe/domain/ProductPrice.java → ...cts/tobe/product/domain/ProductPrice.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
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,39 @@ | ||
package kitchenpos.products.tobe.support; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public abstract class Value<T extends Value<T>> { | ||
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. 추상화를 잘 표현해주셨어요 👍
|
||
|
||
public boolean equals(final Object o) { | ||
if(this == o) return true; | ||
if(o == null || getClass() != o.getClass()) return false; | ||
|
||
return Arrays.stream(getClass().getDeclaredFields()) | ||
.allMatch(field -> { | ||
field.setAccessible(true); | ||
try { | ||
return Objects.equals(field.get(this),field.get(o)); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
public int hashCode() { | ||
return Objects.hash( | ||
Arrays.stream(getClass().getDeclaredFields()) | ||
.map(field -> { | ||
field.setAccessible(true); | ||
try { | ||
return field.get(this); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
}).toArray() | ||
); | ||
|
||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/kitchenpos/products/tobe/domain/DisplayedNameTest.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,29 @@ | ||
package kitchenpos.products.tobe.domain; | ||
|
||
import kitchenpos.products.tobe.menu.domain.DisplayedName; | ||
import kitchenpos.products.tobe.domain.Menu.Profanities; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class DisplayedNameTest { | ||
private final Profanities profanities = new FakeProfanities(); | ||
|
||
@ValueSource(strings = {"욕설", "비속어"}) | ||
@ParameterizedTest | ||
void 이름에_욕설이_포함될_수_없다(final String value) { | ||
assertThatThrownBy(() -> new DisplayedName(value, profanities)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
void 동등성() { | ||
final DisplayedName name1 = new DisplayedName("치킨", profanities); | ||
final DisplayedName name2 = new DisplayedName("치킨", profanities); | ||
assertThat(name1).isEqualTo(name2); | ||
} | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
src/test/java/kitchenpos/products/tobe/domain/FakeProfanities.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,18 @@ | ||
package kitchenpos.products.tobe.domain; | ||
|
||
import kitchenpos.products.tobe.domain.Menu.Profanities; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
class FakeProfanities implements Profanities { | ||
private final List<String> values = Arrays.asList("욕설", "비속어"); | ||
|
||
@Override | ||
public boolean contains(final String text) { | ||
return values.stream() | ||
.anyMatch(text::contains); | ||
} | ||
} | ||
|
||
|
25 changes: 25 additions & 0 deletions
25
src/test/java/kitchenpos/products/tobe/domain/MenuPriceTest.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,25 @@ | ||
package kitchenpos.products.tobe.domain; | ||
|
||
import kitchenpos.products.tobe.menu.domain.MenuPrice; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class MenuPriceTest { | ||
|
||
@Test | ||
void 가격이_음수면_예외가_발생한다() { | ||
assertThatThrownBy(() -> new MenuPrice(BigDecimal.valueOf(-16_000))) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
void 동등성() { | ||
final MenuPrice price1 = new MenuPrice(BigDecimal.valueOf(16_000)); | ||
final MenuPrice price2 = new MenuPrice(BigDecimal.valueOf(16_000)); | ||
assertThat(price1).isEqualTo(price2); | ||
} | ||
} |
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
도메인이 별도의 서버로 분리가 된다면 Value 에 대한 의존성은 어떨까요?