Skip to content

Commit

Permalink
Merge pull request #229 from Team-BC-1/refactor/divide-sell-service-b…
Browse files Browse the repository at this point in the history
…y-command-query

SellService -> SellCommandService / SellQueryService 분리
  • Loading branch information
vanillacake369 authored Feb 4, 2024
2 parents f310c89 + 6b19bee commit d0204b5
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 48 deletions.
10 changes: 6 additions & 4 deletions src/main/java/bc1/gream/domain/buy/provider/BuyNowProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import bc1.gream.domain.order.mapper.OrderMapper;
import bc1.gream.domain.order.service.command.OrderCommandService;
import bc1.gream.domain.sell.entity.Sell;
import bc1.gream.domain.sell.service.SellService;
import bc1.gream.domain.sell.service.command.SellCommandService;
import bc1.gream.domain.sell.service.query.SellQueryService;
import bc1.gream.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -21,7 +22,8 @@
@RequiredArgsConstructor
public class BuyNowProvider {

private final SellService sellService;
private final SellQueryService sellQueryService;
private final SellCommandService sellCommandService;
private final BuyQueryService buyQueryService;
private final OrderCommandService orderCommandService;
private final CouponQueryService couponQueryService;
Expand All @@ -30,7 +32,7 @@ public class BuyNowProvider {
@Transactional
public BuyNowResponseDto buyNowProduct(User buyer, BuyNowRequestDto requestDto, Long productId) {
// 해당상품과 가격에 대한 판매입찰
Sell sell = sellService.getRecentSellBidof(productId, requestDto.price());
Sell sell = sellQueryService.getRecentSellBidof(productId, requestDto.price());
// 쿠폰 조회
Coupon coupon = getCoupon(requestDto.couponId(), buyer);

Expand All @@ -39,7 +41,7 @@ public BuyNowResponseDto buyNowProduct(User buyer, BuyNowRequestDto requestDto,
// 판매입찰 기프티콘에 주문 저장
sell.getGifticon().updateOrder(order);
// 해당 판매입찰 삭제
sellService.delete(sell);
sellCommandService.delete(sell);

// 구매가능검증 :: 사용자 포인트가 finalPrice 보다 작다면 예외처리
// << 이건 컨트롤러 단에서 검증 해줘야하 하지 않을까???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,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.sell.service.SellService;
import bc1.gream.domain.sell.service.query.SellQueryService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -15,8 +15,8 @@
@Transactional(readOnly = true)
public class SellOrderQueryProvider {

private final ProductService productService;
private final SellService sellService;
private final ProductService productQueryService;
private final SellQueryService sellQueryService;


/**
Expand All @@ -28,7 +28,7 @@ public class SellOrderQueryProvider {
* @author 임지훈
*/
public List<SellPriceToQuantityResponseDto> findAllSellBidsOf(Long productId, Pageable pageable) {
Product product = productService.findBy(productId);
return sellService.findAllSellBidsOf(product, pageable).getContent();
Product product = productQueryService.findBy(productId);
return sellQueryService.findAllSellBidsOf(product, pageable).getContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import bc1.gream.domain.sell.entity.Sell;
import bc1.gream.domain.sell.mapper.SellMapper;
import bc1.gream.domain.sell.provider.SellerOrderProvider;
import bc1.gream.domain.sell.service.SellService;
import bc1.gream.domain.sell.service.query.SellQueryService;
import bc1.gream.global.common.RestResponse;
import bc1.gream.global.security.UserDetailsImpl;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -25,7 +25,7 @@
public class UserSellHistoryController {

private final SellerOrderProvider sellerOrderProvider;
private final SellService sellService;
private final SellQueryService sellQueryService;

/**
* 판매자 판매내역 히스토리 조회 요청
Expand Down Expand Up @@ -56,7 +56,7 @@ public RestResponse<List<OrderAsSellerResponseDto>> getSoldOrderOf(
public RestResponse<List<UserSellOnProgressResponseDto>> getUserSellOnProgress(
@AuthenticationPrincipal UserDetailsImpl userDetails
) {
List<Sell> sellsOnProgress = sellService.getUserSellOnProgressOf(userDetails.getUser());
List<Sell> sellsOnProgress = sellQueryService.getUserSellOnProgressOf(userDetails.getUser());
List<UserSellOnProgressResponseDto> responseDtos = sellsOnProgress.stream()
.map(SellMapper.INSTANCE::toUserSellOnProgressResponseDto)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import bc1.gream.domain.sell.entity.Sell;
import bc1.gream.domain.sell.mapper.SellMapper;
import bc1.gream.domain.sell.repository.SellRepository;
import bc1.gream.domain.sell.service.SellService;
import bc1.gream.domain.sell.service.command.SellCommandService;
import bc1.gream.domain.sell.service.helper.deadline.Deadline;
import bc1.gream.domain.sell.service.helper.deadline.DeadlineCalculator;
import bc1.gream.domain.user.entity.User;
Expand All @@ -29,7 +29,7 @@ public class SellBidProvider {

private final SellRepository sellRepository;
private final GifticonCommandService gifticonCommandService;
private final SellService sellService;
private final SellCommandService sellCommandService;
private final S3ImageService s3ImageService;

@Transactional
Expand Down Expand Up @@ -61,7 +61,7 @@ public SellBidResponseDto createSellBid(User seller, SellBidRequestDto requestDt


public SellCancelBidResponseDto sellCancelBid(User seller, Long sellId) {
Sell deletedSell = sellService.deleteSellByIdAndUser(sellId, seller);
Sell deletedSell = sellCommandService.deleteSellByIdAndUser(sellId, seller);
gifticonCommandService.delete(deletedSell.getGifticon());

return new SellCancelBidResponseDto(sellId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bc1.gream.domain.sell.scheduler;

import bc1.gream.domain.sell.service.SellService;
import bc1.gream.domain.sell.service.command.SellCommandService;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
Expand All @@ -23,7 +23,7 @@ public class SellDeadlineScheduler {
private final static String REDIS_CLOSE_LOCK_VALUE = "LOCKED";
private final static int REDIS_DISTRIBUTED_LOCK_TIMEOUT = 2;

private final SellService sellService;
private final SellCommandService sellCommandService;
private final StringRedisTemplate redisTemplate;

@Retryable(
Expand All @@ -36,7 +36,7 @@ public void closeBuyOfPassedDealine() {
Boolean hasLock = redisTemplate.opsForValue()
.setIfAbsent(REDIS_CLOSE_LOCK, REDIS_CLOSE_LOCK_VALUE, REDIS_DISTRIBUTED_LOCK_TIMEOUT, TimeUnit.SECONDS);
if (hasLock) {
sellService.deleteSellsOfDeadlineBefore(LocalDateTime.now());
sellCommandService.deleteSellsOfDeadlineBefore(LocalDateTime.now());
redisTemplate.delete(REDIS_CLOSE_LOCK);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package bc1.gream.domain.sell.service.command;

import static bc1.gream.global.common.ResultCase.SELL_BID_PRODUCT_NOT_FOUND;

import bc1.gream.domain.sell.entity.Sell;
import bc1.gream.domain.sell.repository.SellRepository;
import bc1.gream.domain.user.entity.User;
import bc1.gream.global.exception.GlobalException;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class SellCommandService {

private final SellRepository sellRepository;

/**
* 판매입찰아이디와 판매입찰자에 대한 판매입찰을 삭제, 삭제된 판매입찰 반환
*
* @param sellId 판매입찰아이디
* @param seller 판매입찰자
* @return 삭제된 판매입찰
*/
public Sell deleteSellByIdAndUser(Long sellId, User seller) {
Sell sell = sellRepository.findByIdAndUser(sellId, seller).orElseThrow(
() -> new GlobalException(SELL_BID_PRODUCT_NOT_FOUND)
);
sellRepository.delete(sell);
return sell;
}

public void delete(Sell sell) {
sellRepository.delete(sell);
}

public void deleteSellsOfDeadlineBefore(LocalDateTime dateTime) {
sellRepository.deleteSellsOfDeadlineBefore(dateTime);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bc1.gream.domain.sell.service;
package bc1.gream.domain.sell.service.query;

import static bc1.gream.global.common.ResultCase.SELL_BID_PRODUCT_NOT_FOUND;

Expand All @@ -9,7 +9,6 @@
import bc1.gream.domain.user.entity.User;
import bc1.gream.global.exception.GlobalException;
import java.util.List;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -19,7 +18,7 @@
@Service
@RequiredArgsConstructor
@Transactional
public class SellService {
public class SellQueryService {

private final SellRepository sellRepository;

Expand All @@ -43,38 +42,14 @@ public Sell findByIdAndUser(Long sellId, User user) {
);
}

/**
* 판매입찰아이디와 판매입찰자에 대한 판매입찰을 삭제, 삭제된 판매입찰 반환
*
* @param sellId 판매입찰아이디
* @param seller 판매입찰자
* @return 삭제된 판매입찰
*/
@Transactional
public Sell deleteSellByIdAndUser(Long sellId, User seller) {
Sell sell = findByIdAndUser(sellId, seller);
sellRepository.delete(sell);
return sell;
}

@Transactional(readOnly = true)
public Sell getRecentSellBidof(Long productId, Long price) {
return sellRepository.findByProductIdAndPrice(productId, price).orElseThrow(
() -> new GlobalException(SELL_BID_PRODUCT_NOT_FOUND)
);
}

@Transactional
public void delete(Sell sell) {
sellRepository.delete(sell);
}

public List<Sell> getUserSellOnProgressOf(User seller) {
return sellRepository.findAllByUser(seller);
}

@Transactional
public void deleteSellsOfDeadlineBefore(LocalDateTime dateTime) {
sellRepository.deleteSellsOfDeadlineBefore(dateTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import bc1.gream.domain.sell.entity.Sell;
import bc1.gream.domain.sell.provider.SellBidProvider;
import bc1.gream.domain.sell.repository.SellRepository;
import bc1.gream.domain.sell.service.command.SellCommandService;
import bc1.gream.domain.user.entity.User;
import bc1.gream.infra.s3.S3ImageService;
import bc1.gream.test.GifticonTest;
Expand All @@ -34,7 +35,7 @@ class SellBidProviderTest implements GifticonTest {
@Mock
GifticonCommandService gifticonCommandService;
@Mock
SellService sellService;
SellCommandService sellCommandService;
@Mock
S3ImageService s3ImageService;

Expand Down Expand Up @@ -67,13 +68,13 @@ void sellBidProductTest() throws IOException {
@Test
void sellCancelBidTest() {
// given
given(sellService.deleteSellByIdAndUser(TEST_SELL_ID, TEST_USER)).willReturn(TEST_SELL);
given(sellCommandService.deleteSellByIdAndUser(TEST_SELL_ID, TEST_USER)).willReturn(TEST_SELL);

// when
sellBidProvider.sellCancelBid(TEST_USER, TEST_SELL_ID);

// then
verify(sellService, times(1)).deleteSellByIdAndUser(any(Long.class), any(User.class));
verify(sellCommandService, times(1)).deleteSellByIdAndUser(any(Long.class), any(User.class));
// verify(gifticonRepository, times(1)).delete(any(Gifticon.class));
}
}

0 comments on commit d0204b5

Please sign in to comment.