Skip to content

Commit

Permalink
refactor: ProductValidator 인터페이스 도입으로 DIP 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
koo995 committed Jul 29, 2024
1 parent a345677 commit 7e9aac8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
public interface ProductRepository {
Product save(Product product);

Boolean productDuplicatedCheckByNormalizedName(String normalizedName);
Boolean productDuplicatedCheck(String normalizedName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
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);
}
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);
}
}

0 comments on commit 7e9aac8

Please sign in to comment.