Skip to content

Commit

Permalink
refactor: 두 개 이상의 일을 하는 전체 상품 조회 메소드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
HiiWee committed Mar 20, 2023
1 parent 446f649 commit 50cb1cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.jscode.spring.product.dto.ProductResponse;
import com.jscode.spring.product.dto.ProductSaveResponse;
import com.jscode.spring.product.service.ProductService;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -52,7 +53,10 @@ public ProductResponse findProductById(@PathVariable final Long productId,
@GetMapping("/products")
public ProductListResponse findProducts(@RequestParam @Nullable final String name,
@RequestParam @Nullable final String monetaryUnit) {
return productService.findAll(name, monetaryUnit);
if (Objects.isNull(name)) {
return productService.findAll(monetaryUnit);
}
return productService.findAllByName(name, monetaryUnit);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.jscode.spring.product.repository.ProductRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

Expand All @@ -34,21 +35,19 @@ public Long saveProduct(final NewProductRequest newProductRequest) {
return productRepository.save(product);
}

/**
* TODO: 테스트하기 어려운 코드
*/
public ProductListResponse findAll(@Nullable final String name, @Nullable final String monetaryUnit) {
if (name == null) {
List<Product> products = productRepository.findAll();
return new ProductListResponse(createConvertedPriceProducts(monetaryUnit, products));
}
public ProductListResponse findAllByName(@Nullable final String name, @Nullable final String monetaryUnit) {
List<Product> products = productRepository.findAllByName(name);
if (products.isEmpty()) {
throw new ProductNotFoundException();
}
return new ProductListResponse(createConvertedPriceProducts(monetaryUnit, products));
}

public ProductListResponse findAll(final String monetaryUnit) {
List<Product> products = productRepository.findAll();
return new ProductListResponse(createConvertedPriceProducts(monetaryUnit, products));
}

private List<ProductResponse> createConvertedPriceProducts(final String monetaryUnit,
final List<Product> products) {
List<ProductResponse> productResponses = new ArrayList<>();
Expand All @@ -68,7 +67,7 @@ public ProductResponse findProductById(final Long productId, final String moneta
}

private double convertPriceKrwTo(final String monetaryUnit, final Product product) {
if (monetaryUnit == null) {
if (Objects.isNull(monetaryUnit)) {
return product.getPrice();
}
return exchangeRatesService.convertKrwTo(MonetaryUnit.valueOf(monetaryUnit), product.getPrice());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void saveProduct_success() {
void findAll_fail_withInvalidName() {
String name = "nothing";

assertThatThrownBy(() -> productService.findAll(name, null))
assertThatThrownBy(() -> productService.findAllByName(name, null))
.isInstanceOf(ProductNotFoundException.class)
.hasMessageContaining("존재하지 않는 상품입니다.");
}
Expand All @@ -59,7 +59,7 @@ void findAll_success() {
ProductResponse productResponse2 = ProductResponse.of(new Product(2L, "키보드", 100_000), 100000);
ProductResponse productResponse3 = ProductResponse.of(new Product(3L, "마우스", 50_000), 50000);

ProductListResponse products = productService.findAll(null, null);
ProductListResponse products = productService.findAll( null);

Assertions.assertAll(
() -> assertThat(products.contains(productResponse1)).isTrue(),
Expand Down

0 comments on commit 50cb1cd

Please sign in to comment.