Skip to content

Commit

Permalink
Merge pull request #235 from Team-BC-1/fix/test/admin-create-coupon-p…
Browse files Browse the repository at this point in the history
…roduct

ProductService -> ProductCommandService, ProductQueryService 로 분리
  • Loading branch information
vanillacake369 authored Feb 5, 2024
2 parents d0204b5 + ac3c64a commit fcd5268
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import bc1.gream.domain.coupon.entity.Coupon;
import bc1.gream.domain.coupon.mapper.CouponMapper;
import bc1.gream.domain.coupon.provider.CouponProvider;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.product.service.command.ProductCommandService;
import bc1.gream.domain.user.mapper.RefundMapper;
import bc1.gream.domain.user.service.command.RefundCommandService;
import bc1.gream.domain.user.service.query.RefundQueryService;
Expand All @@ -32,7 +32,7 @@
@RequestMapping("/api/admin")
public class AdminController {

private final ProductService productService;
private final ProductCommandService productCommandService;
private final RefundQueryService refundQueryService;
private final RefundCommandService refundCommandService;
private final CouponProvider couponProvider;
Expand All @@ -53,7 +53,7 @@ public RestResponse<List<AdminGetRefundResponseDto>> getRefunds(AdminGetRefundRe
public RestResponse<AdminProductResponseDto> addProducts(
@RequestBody AdminProductRequestDto adminProductRequestDto
) {
productService.addProduct(adminProductRequestDto);
productCommandService.addProduct(adminProductRequestDto);

return RestResponse.success(new AdminProductResponseDto());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import bc1.gream.domain.product.mapper.ProductMapper;
import bc1.gream.domain.product.provider.BuyOrderQueryProvider;
import bc1.gream.domain.product.provider.SellOrderQueryProvider;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.product.service.query.ProductQueryService;
import bc1.gream.domain.sell.entity.Sell;
import bc1.gream.domain.sell.provider.ProductOrderQueryProvider;
import bc1.gream.global.common.RestResponse;
Expand All @@ -36,7 +36,7 @@
@SecurityRequirements() // Swagger Security 적용 X
public class ProductQueryController {

private final ProductService productService;
private final ProductQueryService productQueryService;
private final ProductOrderQueryProvider productOrderQueryProvider;
private final SellOrderQueryProvider sellOrderQueryProvider;
private final BuyOrderQueryProvider buyOrderQueryProvider;
Expand All @@ -51,7 +51,7 @@ public RestResponse<List<ProductPreviewResponseDto>> findAll(
@RequestParam(required = false, defaultValue = "") String name
) {
ProductCondition productCondition = ProductCondition.builder().brand(brand).name(name).build();
List<Product> products = productService.findAllBy(productCondition);
List<Product> products = productQueryService.findAllBy(productCondition);
List<ProductPreviewResponseDto> responseDtos = products.stream()
.map(ProductMapper.INSTANCE::toPreviewResponseDto)
.toList();
Expand All @@ -66,7 +66,7 @@ public RestResponse<List<ProductPreviewResponseDto>> findAll(
public RestResponse<ProductDetailsResponseDto> findAllBy(
@PathVariable("id") Long productId
) {
Product product = productService.findBy(productId);
Product product = productQueryService.findBy(productId);
ProductDetailsResponseDto responseDto = ProductMapper.INSTANCE.toDetailsResponseDto(product);
return RestResponse.success(responseDto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import bc1.gream.domain.buy.service.query.BuyQueryService;
import bc1.gream.domain.product.dto.response.BuyPriceToQuantityResponseDto;
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.product.service.query.ProductQueryService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -15,7 +15,7 @@
@Transactional(readOnly = true)
public class BuyOrderQueryProvider {

private final ProductService productQueryService;
private final ProductQueryService productQueryService;
private final BuyQueryService buyQueryService;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import bc1.gream.domain.product.dto.response.SellPriceToQuantityResponseDto;
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.product.service.query.ProductQueryService;
import bc1.gream.domain.sell.service.query.SellQueryService;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,7 +15,7 @@
@Transactional(readOnly = true)
public class SellOrderQueryProvider {

private final ProductService productQueryService;
private final ProductQueryService productQueryService;
private final SellQueryService sellQueryService;


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package bc1.gream.domain.product.service.command;

import bc1.gream.domain.admin.dto.request.AdminProductRequestDto;
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.repository.ProductRepository;
import bc1.gream.global.common.ResultCase;
import bc1.gream.global.exception.GlobalException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class ProductCommandService {

private final ProductRepository productRepository;

public void addProduct(AdminProductRequestDto adminProductRequestDto) {
String productName = adminProductRequestDto.name();

if (productRepository.existsByName(productName)) {
throw new GlobalException(ResultCase.DUPLICATED_PRODUCT_NAME);
}

Product product = buildProductFromRequest(adminProductRequestDto);
productRepository.save(product);
}

private Product buildProductFromRequest(AdminProductRequestDto adminProductRequestDto) {
return Product.builder()
.name(adminProductRequestDto.name())
.brand(adminProductRequestDto.brand())
.description(adminProductRequestDto.description())
.imageUrl(adminProductRequestDto.imageUrl())
.price(adminProductRequestDto.price())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bc1.gream.domain.product.service.query;

import bc1.gream.domain.admin.dto.request.AdminProductRequestDto;
import bc1.gream.domain.product.dto.unit.ProductCondition;
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.repository.ProductRepository;
Expand All @@ -16,7 +15,7 @@
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ProductService {
public class ProductQueryService {

private final ProductRepository productRepository;

Expand Down Expand Up @@ -45,27 +44,4 @@ public Product findBy(Long id) throws GlobalException {
return productRepository.findById(id)
.orElseThrow(() -> new GlobalException(ResultCase.PRODUCT_NOT_FOUND));
}


public void addProduct(AdminProductRequestDto adminProductRequestDto) {
String productName = adminProductRequestDto.name();

if (productRepository.existsByName(productName)) {
throw new GlobalException(ResultCase.DUPLICATED_PRODUCT_NAME);
}

Product product = buildProductFromRequest(adminProductRequestDto);
productRepository.save(product);
}

private Product buildProductFromRequest(AdminProductRequestDto adminProductRequestDto) {
return Product.builder()
.name(adminProductRequestDto.name())
.brand(adminProductRequestDto.brand())
.description(adminProductRequestDto.description())
.imageUrl(adminProductRequestDto.imageUrl())
.price(adminProductRequestDto.price())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import bc1.gream.domain.order.entity.Order;
import bc1.gream.domain.order.service.query.OrderQueryService;
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.product.service.query.ProductQueryService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -14,7 +14,7 @@
@Transactional(readOnly = true)
public class ProductOrderQueryProvider {

private final ProductService productService;
private final ProductQueryService productQueryService;
private final OrderQueryService orderQueryService;

/**
Expand All @@ -25,7 +25,7 @@ public class ProductOrderQueryProvider {
* @author 임지훈
*/
public List<Order> findAllTradesOf(Long productId) {
Product product = productService.findBy(productId);
Product product = productQueryService.findBy(productId);
return orderQueryService.findAllTradesOf(product);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import bc1.gream.domain.admin.dto.request.AdminCreateCouponRequestDto;
import bc1.gream.domain.coupon.entity.DiscountType;
import bc1.gream.domain.coupon.provider.CouponProvider;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.product.service.command.ProductCommandService;
import bc1.gream.domain.user.entity.UserRole;
import bc1.gream.domain.user.service.command.RefundCommandService;
import bc1.gream.domain.user.service.query.RefundQueryService;
Expand Down Expand Up @@ -37,7 +37,7 @@ class AdminControllerTest {
@Autowired
private ObjectMapper objectMapper;
@MockBean
private ProductService productService;
private ProductCommandService productCommandService;
@MockBean
private RefundQueryService refundQueryService;
@MockBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.provider.BuyOrderQueryProvider;
import bc1.gream.domain.product.provider.SellOrderQueryProvider;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.product.service.query.ProductQueryService;
import bc1.gream.domain.sell.provider.ProductOrderQueryProvider;
import bc1.gream.test.ProductTest;
import java.util.List;
Expand All @@ -30,7 +30,7 @@ class ProductQueryControllerTest implements ProductTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService;
private ProductQueryService productQueryService;
@MockBean
private ProductOrderQueryProvider productOrderQueryProvider;
@MockBean
Expand All @@ -42,7 +42,7 @@ class ProductQueryControllerTest implements ProductTest {
void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(
new ProductQueryController(
productService,
productQueryService,
productOrderQueryProvider,
sellOrderQueryProvider,
buyOrderQueryProvider
Expand All @@ -57,7 +57,7 @@ void setUp() {
public void 상품_전체조회() throws Exception {
// GIVEN
List<Product> products = List.of();
lenient().when(productService.findAll()).thenReturn(products);
lenient().when(productQueryService.findAll()).thenReturn(products);

// WHEN
// THEN
Expand All @@ -70,7 +70,7 @@ void setUp() {
public void 상품_전체조회_상세조건() throws Exception {
// GIVEN
List<Product> products = List.of();
lenient().when(productService.findAll()).thenReturn(products);
lenient().when(productQueryService.findAll()).thenReturn(products);

// WHEN
// THEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.springframework.data.domain.Sort;

@ExtendWith(MockitoExtension.class)
class ProductServiceTest implements ProductTest {
class ProductQueryServiceTest implements ProductTest {

@InjectMocks
ProductService productService;
ProductQueryService productQueryService;

@Mock
ProductRepository productRepository;
Expand All @@ -41,7 +41,7 @@ class ProductServiceTest implements ProductTest {
given(productRepository.findById(TEST_PRODUCT_ID)).willReturn(Optional.of(TEST_PRODUCT));

// WHEN
Product product = productService.findBy(TEST_PRODUCT_ID);
Product product = productQueryService.findBy(TEST_PRODUCT_ID);

// THEN
assertEquals(TEST_PRODUCT, product);
Expand All @@ -54,7 +54,7 @@ class ProductServiceTest implements ProductTest {
given(productRepository.findAll()).willReturn(productMocks);

// WHEN
List<Product> products = productService.findAll();
List<Product> products = productQueryService.findAll();

// THEN
boolean hasTestProduct = products.stream()
Expand All @@ -78,7 +78,7 @@ class ProductServiceTest implements ProductTest {
given(productRepositoryCustom.findAllBy(condition)).willReturn(List.of(TEST_PRODUCT));

// WHEN
List<Product> products = productService.findAllBy(condition);
List<Product> products = productQueryService.findAllBy(condition);

// THEN
System.out.println("products.size() = " + products.size());
Expand All @@ -95,11 +95,11 @@ class ProductServiceTest implements ProductTest {
.endPrice(TEST_PRODUCT_PRICE + 500L)
.build();
Pageable pageRequest = PageRequest.of(1, 10, Sort.by("name").descending());
PageImpl page = new PageImpl(List.of(TEST_PRODUCT));
PageImpl<Product> page = new PageImpl<>(List.of(TEST_PRODUCT));
given(productRepositoryCustom.findAllByPaging(condition, pageRequest)).willReturn(page);

// WHEN
Page<Product> products = productService.findAllByPaging(condition, pageRequest);
Page<Product> products = productQueryService.findAllByPaging(condition, pageRequest);

// THEN
boolean hasTestProduct = products.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import bc1.gream.domain.order.entity.Order;
import bc1.gream.domain.order.service.query.OrderQueryService;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.product.service.query.ProductQueryService;
import bc1.gream.domain.sell.provider.ProductOrderQueryProvider;
import bc1.gream.test.OrderTest;
import bc1.gream.test.ProductTest;
Expand All @@ -27,7 +27,7 @@ class ProductOrderQueryProviderImplTest implements UserTest, ProductTest, OrderT
ProductOrderQueryProvider productOrderQueryProvider;

@Mock
ProductService productService;
ProductQueryService productQueryService;

@Mock
OrderQueryService orderQueryService;
Expand All @@ -36,7 +36,7 @@ class ProductOrderQueryProviderImplTest implements UserTest, ProductTest, OrderT
@DisplayName("상품에 대한 모든 체결거래 내역을 조회합니다.")
public void 체결거래내역_조회() {
// GIVEN
given(productService.findBy(TEST_PRODUCT_ID)).willReturn(TEST_PRODUCT);
given(productQueryService.findBy(TEST_PRODUCT_ID)).willReturn(TEST_PRODUCT);
given(orderQueryService.findAllTradesOf(TEST_PRODUCT)).willReturn(List.of(TEST_ORDER));

// WHEN
Expand Down

0 comments on commit fcd5268

Please sign in to comment.