|
1 | 1 | package com.jajaja.domain.cart.service; |
2 | 2 |
|
| 3 | +import com.jajaja.domain.cart.converter.CartConverter; |
3 | 4 | import com.jajaja.domain.cart.dto.CartProductAddRequestDto; |
| 5 | +import com.jajaja.domain.cart.dto.CartProductResponseDto; |
| 6 | +import com.jajaja.domain.cart.dto.CartResponseDto; |
4 | 7 | import com.jajaja.domain.cart.entity.Cart; |
5 | 8 | import com.jajaja.domain.cart.entity.CartProduct; |
6 | 9 | import com.jajaja.domain.cart.repository.CartProductRepository; |
|
11 | 14 | import com.jajaja.domain.product.entity.ProductOption; |
12 | 15 | import com.jajaja.domain.product.repository.ProductOptionRepository; |
13 | 16 | import com.jajaja.domain.product.repository.ProductRepository; |
| 17 | +import com.jajaja.domain.product.service.ProductCommonService; |
| 18 | +import com.jajaja.domain.team.entity.enums.TeamStatus; |
| 19 | +import com.jajaja.domain.team.repository.TeamCommandRepository; |
14 | 20 | import com.jajaja.global.apiPayload.code.status.ErrorStatus; |
15 | 21 | import com.jajaja.global.apiPayload.exception.handler.CartHandler; |
| 22 | +import com.jajaja.global.common.dto.PriceInfoDto; |
16 | 23 | import lombok.RequiredArgsConstructor; |
17 | 24 | import lombok.extern.slf4j.Slf4j; |
18 | 25 | import org.springframework.stereotype.Service; |
|
27 | 34 | @Transactional |
28 | 35 | public class CartCommandServiceImpl implements CartCommandService { |
29 | 36 |
|
30 | | - private final CartCommonService cartCommonService; |
31 | | - private final CouponCommonService couponCommonService; |
32 | 37 | private final CartProductRepository cartProductRepository; |
33 | 38 | private final ProductRepository productRepository; |
| 39 | + private final TeamCommandRepository teamRepository; |
34 | 40 | private final ProductOptionRepository productOptionRepository; |
35 | 41 | private final MemberCouponRepository memberCouponRepository; |
| 42 | + private final CouponCommonService couponCommonService; |
| 43 | + private final ProductCommonService productCommonService; |
| 44 | + private final CartCommonService cartCommonService; |
36 | 45 |
|
37 | 46 | @Override |
38 | | - public void addOrUpdateCartProduct(Long memberId, List<CartProductAddRequestDto> request) { |
| 47 | + public CartResponseDto addOrUpdateCartProduct(Long memberId, List<CartProductAddRequestDto> request) { |
39 | 48 | Cart cart = cartCommonService.findCart(memberId); |
40 | | - request.forEach(req -> { |
| 49 | + List<CartProductResponseDto> items = request.stream().map(req -> { |
41 | 50 | log.info("[CartCommandService] 사용자 {}의 장바구니에 아이템 {} 추가/수정", memberId, req.productId()); |
42 | 51 |
|
43 | 52 | CartOpterationContext context = prepareCartOperationContext(req.productId(), req.optionId()); |
44 | 53 |
|
45 | 54 | Optional<CartProduct> existingItem = req.optionId() != null ? cartProductRepository.findByCartIdAndProductIdAndProductOptionId(cart.getId(), context.product().getId(), context.productOption.getId()) |
46 | 55 | : cartProductRepository.findByCartIdAndProductIdAndProductOptionIsNull(cart.getId(), context.product().getId()); |
47 | 56 |
|
48 | | - existingItem.ifPresentOrElse( |
49 | | - item -> { |
| 57 | + CartProduct cartProduct = existingItem |
| 58 | + .map(item -> { |
50 | 59 | log.info("[CartCommandService] 기존 아이템 {}의 옵션과 수량을 변경합니다.", item.getId()); |
51 | 60 | item.update(context.productOption(), req.quantity()); |
52 | | - }, |
53 | | - () -> { |
| 61 | + return item; |
| 62 | + }) |
| 63 | + .orElseGet(() -> { |
54 | 64 | log.info("[CartCommandService] 장바구니에 새로 아이템 {}를 추가합니다.", req.productId()); |
55 | 65 | CartProduct newCartProduct = CartProduct.create(cart, context.product(), context.productOption(), req.quantity()); |
56 | 66 | cart.addCartProduct(newCartProduct); |
57 | | - cartProductRepository.save( newCartProduct); |
58 | | - } |
59 | | - ); |
60 | | - }); |
| 67 | + cartProductRepository.save(newCartProduct); |
| 68 | + return newCartProduct; |
| 69 | + }); |
| 70 | + |
| 71 | + boolean isTeamAvailable = teamRepository.existsByProductIdAndStatus( |
| 72 | + cartProduct.getProduct().getId(), TeamStatus.MATCHING); |
| 73 | + return CartProductResponseDto.of(cartProduct, productCommonService.calculateDiscountedPrice(cartProduct.getUnitPrice(), cartProduct.getProduct().getDiscountRate()), isTeamAvailable); |
| 74 | + }).toList(); |
| 75 | + |
| 76 | + PriceInfoDto priceInfo = cart.getCoupon() != null ? |
| 77 | + couponCommonService.calculateDiscount(cart, cart.getCoupon()) : |
| 78 | + PriceInfoDto.noDiscount(cart.calculateTotalAmount()); |
| 79 | + |
61 | 80 | revalidateAppliedCouponIfExists(cart); |
| 81 | + return CartConverter.toCartResponseDto(cart, items, priceInfo); |
62 | 82 | } |
63 | 83 |
|
64 | 84 | @Override |
|
0 commit comments