-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ProductValidator 인터페이스 도입으로 DIP 적용
- Loading branch information
Showing
4 changed files
with
34 additions
and
26 deletions.
There are no files selected for viewing
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
26 changes: 2 additions & 24 deletions
26
src/main/java/flab/nutridiary/product/service/ProductValidator.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 |
---|---|---|
@@ -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); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/flab/nutridiary/product/service/WhiteSpaceProductValidator.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,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); | ||
} | ||
} |