-
Notifications
You must be signed in to change notification settings - Fork 179
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
๐ 1๋จ๊ณ - ๋ฆฌํฉํฐ๋ง(์ํ) #328
base: sumiini
Are you sure you want to change the base?
Conversation
โฆ์ฑ ๋ฐ persistence ๊ณ์ธต ์ถ๊ฐ
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.
์๋ฏผ๋ ์๋
ํ์ธ์. ๋๋ฉ์ธ ์ฃผ๋ ์ค๊ณ ๊ธฐ๋ณธ ์์ ๋ฏธ์
์ ํจ๊ปํ๊ฒ๋ ๊น๊ฒฝ๋ก์
๋๋ค.
1๋จ๊ณ ๋ฏธ์
์ ๊ตฌํํด์ฃผ์
จ์ต๋๋ค๋ง ํ์ฌ ์ ํ๋ฆฌ์ผ์ด์
์ด ๊ธฐ๋ํ์ง ์๋ ๊ฒ์ผ๋ก ๋ณด์ด๋ค์. ๐ข
์ ํ๋ฆฌ์ผ์ด์
์ด ๊ธฐ๋๋๋ ์ํ๋ก ๋ณ๊ฒฝ ๋ถํ๋๋ฆด๊ฒ์.
์ถ๊ฐ์ ์ผ๋ก ๊ธฐ์กด์ API๋ฅผ ๋ฆฌํฉํฐ๋งํ์ ์ฝ๋๋ก ๋์ฒดํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์.
๋ ํ
์คํธ ์ฝ๋ ๋ํ ๊ธฐ์กด์ ์๋น์ค ๋ก์ง์ด ์๋ ๋ฆฌํฉํฐ๋งํ ์๋น์ค ๋ก์ง์ผ๋ก ๋์ฒดํ์ ๋, ๊ธฐ์กด์ ๋์์ ๋์ผํ๊ฒ ์ํํ๋์ง๋ฅผ ํ์ธํด์ฃผ์
จ์ผ๋ฉดํฉ๋๋ค. ๐
์ด ๋ฏธ์ ์ ์ค์ ๋ก ์๋น์ค์ค์ธ ์ฝ๋๋ฅผ ์์ ํ๋ค๊ณ ์๊ฐํ๊ณ ์ต๋ํ ๋ฆฌํฉํฐ๋ง์ ์งํํด์ฃผ์๋ฉด ์ข์ ๊ฒ ๊ฐ์์. ๐
๊ทธ ์ธ์๋ ๋ช๋ช ์ฝ๋ฉํธ ๋จ๊ฒจ๋์์ผ๋ ํ์ธ ๋ถํ๋๋ฆด๊ฒ์.
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
@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.
ํ์ฌ ์ ํ๋ฆฌ์ผ์ด์
๊ธฐ๋์ด ์๋๊ณ ์๋ ๊ฒ ๊ฐ์์.
๋ค์ ๋ฆฌ๋ทฐ ์์ฒญ ๋๋, ์ ํ๋ฆฌ์ผ์ด์
์ด ๊ธฐ๋๋๋ ์ํ๊ฐ ๋๋๋กํด์ฃผ์ธ์. ๐
} | ||
|
||
@Transactional | ||
public Product create(final Product request) { |
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.
๊ธฐ์กด์ ์๋น์ค ๋ก์ง์ ์ฌ์ฉํ๋ ์์น๋ค์ ๋ฆฌํฉํฐ๋งํ ๋ก์ง๋ค์ ๋์ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์.
์ง๊ธ์ ์ํ์์๋ ๋ฆฌํฉํฐ๋งํ ์ฝ๋๋ค์ด ์ด์ ์ฒ๋ผ ์ ๋์ํ ์ง์ ๋ํ ํ์ ์ด ๋ค์ง ์๋๋ฐ์. ๐ค
ํ
์คํธ ์ฝ๋ ๋ํ ๋ณ๊ฒฝ๋ ProductService๋ก ๋์ฒดํด์ฃผ์ ๋ค, ํ
์คํธ ์ฝ๋๊ฐ ์ ๋์๊ฐ๋์ง ํ์ธ์ด๋๋ฉด ์ข์ ๊ฒ ๊ฐ์์. ๐
throw new IllegalArgumentException(); | ||
} | ||
final Product product = productRepository.findById(productId) | ||
.orElseThrow(NoSuchElementException::new); |
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.
์์ธ๊ฐ ๋ฐ์ํ์ ๋, ์ ์ ํ ์๋ฌ ๋ฉ์์ง๋ฅผ ๋์ ธ์ค๋ค๋ฉด ์๋ฌ๋ฉ์์ง๋ฅผ ํตํด์ ๋ฌธ์ ์ ์์ธ์ ํ์ ํ๊ธฐ๊ฐ ๋ ์์ํด์ง์ง ์์๊น์? ๐
.orElseThrow(NoSuchElementException::new); | |
.orElseThrow(() -> new NoSuchElementException("์ํ์ด ์กด์ฌํ์ง ์์ต๋๋ค. productId: " + productId)); |
import jakarta.persistence.Embeddable; | ||
import kitchenpos.products.infra.PurgomalumClient; | ||
@Embeddable | ||
public class Name { |
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.
README.md์ ์ ํ ์ฉ์ด ์ฌ์ , ๋ชจ๋ธ๋ง์ ๋ดค์ ๋๋ Name์ด ์๋ DisplayedName
์ผ๋ก ์ ์ํด์ผํ ๊ฒ ๊ฐ์๋ฐ์. ๐ค
ํน์ ์ด์ ๋ฏธ์
์ฝ๋ or ๋ฌธ์๋ฅผ ๊ฐ์ง๊ณ ์์ผํ๋ ์ํฉ์ด๋ผ๋ฉด ํด๋น PR close ํ, 0๋จ๊ณ PR์ ์ฌ๋ ค์ฃผ์ธ์!
} | ||
final Product product = productRepository.findById(productId) | ||
.orElseThrow(NoSuchElementException::new); | ||
product.setPrice(price); |
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.
setter๋ฅผ ์ด์ฉํ๊ธฐ๋ณด๋ค๋, ๋ณ๊ฒฝ์ ์๋ฏธ๋ฅผ ๋๋ฌ๋ผ ์ ์๋๋ก ์ ์ ํ ๋ฉ์๋๋ช ์ ๋ค์ด๋ฐํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
product.setPrice(price); | |
product.changePrice(price); |
|
||
@Entity | ||
@Table(name = "product") | ||
public class ProductEntity { |
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.
์์ ๋๋ฉ์ธ ๊ฐ์ฒด์ JPA์ Entity๋ฅผ ๋ถ๋ฆฌํ์ จ๊ตฐ์? ๐
import jakarta.persistence.Embeddable; | ||
import kitchenpos.products.infra.PurgomalumClient; | ||
@Embeddable | ||
public class Name { |
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.
Name, Price์ ๊ฒฝ์ฐ VO๋ผ๊ณ ๋ณผ ์ ์์ง ์์๊น์? ๐
import java.math.BigDecimal; | ||
import java.util.Objects; | ||
@Embeddable | ||
public class Price { |
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.
์ถํ ๋ค๋ฅธ ์ปจํ
์คํธ์์๋ Price๊ฐ ๋ฑ์ฅํ ๊ฒ ๊ฐ์๋ฐ์.
๋ชจ๋ ๊ฐ์ Price๋ฅผ ์ฐ๋๊ฑธ๊น์? ๊ทธ๊ฒ ์๋๋ผ๋ฉด ๋ค์ด๋ฐ์ ์ผ๋ก ๊ตฌ๋ถ์ ํด๋๋๊ฒ ์ข์ง ์์๊น์? ex) ProductPrice
final BigDecimal price = request.getPrice(); | ||
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
final String name = request.getName(); | ||
if (Objects.isNull(name) || purgomalumClient.containsProfanity(name)) { | ||
throw new IllegalArgumentException(); | ||
} |
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.
Price, Name ๊ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ๋งํด๋ ์ด ๊ฒ์ฆ์ ์์ฐ์ค๋ฝ๊ฒ ์ํ๋ ๊ฒ ๊ฐ์๋ฐ์. ๐ค
๋ ๊ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ product์ ์์ฑ์ ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด ์ฃผ์
ํด์ Product ๊ฐ์ฒด๋ฅผ ์์ฑํ๋๋ก ๋ณ๊ฒฝํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. ๐
์๋
ํ์ธ์ @Rok93 ๋ฆฌ๋ทฐ์ด๋ ! ํน์ ์ด๋ฐ ์ํฉ์ผ ๋๋ |
@sumiini ๋ ์๋
ํ์ธ์. ๊ธฐ์กด Product์ ์์กดํ๋ ๋ก์ง๋ค์ด ๋ง๋ค๋ณด๋ ๋ง์ํ์ ๊ฒ์ฒ๋ผ ์ด๋์๋ถํฐ ์์ ํด์ผํ ์ง ๊ณ ๋ฏผ์ด๋์ค ์๋ ์์ ๊ฒ ๊ฐ์์. ๐ค ์ ๋ผ๋ฉด Product์ ๋ด๋ถ์ ์ํ ๊ฐ์ ํ์ ์ ๋ฐ๋๋๋ผ๋, ๊ธฐ์กด์ ๋์์ ์ ์งํ๋๋ก ํ ๊ฒ ๊ฐ์์. ์์ฑ์ ์ค๋ฒ๋ก๋ฉ, getter, setter์ ๋ฐํ ํ์
ํน์ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ธฐ์กด๊ณผ ๋์ผํ๊ฒ ๋ฐ๋, ๋ด๋ถ์ ์ผ๋ก๋ VO ๊ฐ์ฒด๋ก ๋ณํํด์ ํ ๋นํ๋๋ก ์์ ํ๋ค๊ฑฐ๋, (๋ฌผ๋ก ์ถํ setter๋ ๊ฐ๊ธ์ ์ ๊ฑฐ๋ฅผ ํ๋๋ก ๋ฆฌํฉํฐ๋งํ๊ณ ์ถ์ ๊ฒ ๊ฐ์์, ์๋ง Fixtureํจ์๋ ์ง๊ธ์ setter๋ฅผ ํตํด์ fixture๋ฅผ ์์ฑํ๊ณ ์์ ๊ฒ ๊ฐ์๋ฐ setter๊ฐ ๊ธฐ์กด์ฒ๋ผ ์ฌ์ฉํด๋ ๋์ผํ๊ฒ ๋์ํ๋๋ก ํธํ์ฑ๋ง ์ ์งํด์ค๋ค๋ฉด ๋น์ฅ์ ์ปดํ์ผ ์๋ฌ๊ฐ ๋๋ ๋ถ๋ถ์ ์ ์ ๊ฒ ๊ฐ์์. ๐ค ์ผ๋จ ์ง๊ธ์ฒ๋ผ ์ต๋ํ ๊ธฐ์กด์ ๋ก์ง๋ค์ด ๋์ํ๋๋ก ๋ณ๊ฒฝ์ ๊ฐํ๊ณ ๋ ๋ค์ ๋จ๊ณ๋ก ํ๋์ฉ ์์ ํด๋๊ฐ๋ ๋ฐฉํฅ์ผ๋ก ์ ์ง์ ์ธ ๋ฆฌํฉํฐ๋ง์ ํด๊ฐ๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํด์. ์ ๋ง์ ๋ง ์ตํ์ ์๋จ์ผ๋ก ๋๋ฌด ์์กด์ฑ์ด ๋ง์ด ์ฎ์ฌ์์ด ์ด๋ ต๋ค๋ฉด ๋ณ๋์ Product ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ ์์ ํ ์๊ฒ์ผ๋ก ๊ฐ์ ์์ ๋ค, ๋ณ๊ฒฝ์ ํด์ค ๊ฒ ๊ฐ๋ค์. |
์๋ ํ์ธ์ ๋ฆฌ๋ทฐ์ด๋.
products ํจํค์ง ํ์์ tobe.domain ํจํค์ง๋ฅผ ๋ง๋ค์์ต๋๋ค.
JpaProductRepository ๊ตฌํ์ฒด ์์ฑํ์์ต๋๋ค.
์ ๋ถํ๋๋ฆฝ๋๋ค ๐