diff --git a/src/main/java/flab/nutridiary/product/repository/ProductRepository.java b/src/main/java/flab/nutridiary/product/repository/ProductRepository.java index 544e6eb..2e62b9e 100644 --- a/src/main/java/flab/nutridiary/product/repository/ProductRepository.java +++ b/src/main/java/flab/nutridiary/product/repository/ProductRepository.java @@ -5,5 +5,5 @@ public interface ProductRepository { Product save(Product product); - Boolean productDuplicatedCheckByNormalizedName(String normalizedName); + Boolean productDuplicatedCheck(String normalizedName); } diff --git a/src/main/java/flab/nutridiary/product/repository/ProductRepositoryImpl.java b/src/main/java/flab/nutridiary/product/repository/ProductRepositoryImpl.java index fe3bbb8..f5ad1a0 100644 --- a/src/main/java/flab/nutridiary/product/repository/ProductRepositoryImpl.java +++ b/src/main/java/flab/nutridiary/product/repository/ProductRepositoryImpl.java @@ -16,7 +16,7 @@ public Product save(Product product) { } @Override - public Boolean productDuplicatedCheckByNormalizedName(String normalizedName) { + public Boolean productDuplicatedCheck(String normalizedName) { return productCrudRepository.countByNormalizedName(normalizedName) > 0; } } diff --git a/src/main/java/flab/nutridiary/product/service/ProductValidator.java b/src/main/java/flab/nutridiary/product/service/ProductValidator.java index 7eb2de5..24dacbd 100644 --- a/src/main/java/flab/nutridiary/product/service/ProductValidator.java +++ b/src/main/java/flab/nutridiary/product/service/ProductValidator.java @@ -1,29 +1,7 @@ package flab.nutridiary.product.service; import flab.nutridiary.product.domain.Product; -import flab.nutridiary.product.repository.ProductRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; -@RequiredArgsConstructor -@Component -public class ProductValidator { - - private static final String WHITESPACE_REGEX = "\\s+"; - private static final String EMPTY = ""; - private final ProductRepository productRepository; - - public void validate(Product product) { - validate(getNormalizedName(product.getProductName(), product.getProductCorp())); - } - - private void validate(String normalizedName) { - if (productRepository.productDuplicatedCheckByNormalizedName(normalizedName)) { - throw new ProductDuplicatedException(); - } - } - - private String getNormalizedName(String productName, String productCorp) { - return productCorp.replaceAll(WHITESPACE_REGEX, EMPTY) + productName.replaceAll(WHITESPACE_REGEX, EMPTY); - } +public interface ProductValidator { + void validate(Product product); } diff --git a/src/main/java/flab/nutridiary/product/service/WhiteSpaceProductValidator.java b/src/main/java/flab/nutridiary/product/service/WhiteSpaceProductValidator.java new file mode 100644 index 0000000..7cf9a3b --- /dev/null +++ b/src/main/java/flab/nutridiary/product/service/WhiteSpaceProductValidator.java @@ -0,0 +1,30 @@ +package flab.nutridiary.product.service; + +import flab.nutridiary.product.domain.Product; +import flab.nutridiary.product.repository.ProductRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class WhiteSpaceProductValidator implements ProductValidator { + + private static final String WHITESPACE_REGEX = "\\s+"; + private static final String EMPTY = ""; + private final ProductRepository productRepository; + + @Override + public void validate(Product product) { + validate(getNormalizedName(product.getProductName(), product.getProductCorp())); + } + + private void validate(String normalizedName) { + if (productRepository.productDuplicatedCheck(normalizedName)) { + throw new ProductDuplicatedException(); + } + } + + private String getNormalizedName(String productName, String productCorp) { + return productCorp.replaceAll(WHITESPACE_REGEX, EMPTY) + productName.replaceAll(WHITESPACE_REGEX, EMPTY); + } +}