Skip to content

Commit d080da2

Browse files
committed
feature: 쿠폰 사용 및 할인 계산 로직 추가
- 할인 정보를 담기 위한 `CouponDiscountResult` 레코드 도입 - 주문 시 쿠폰 사용을 위해 `OrderCommand`에 `couponId` 필드 추가 - 쿠폰 검증·사용·할인 금액 계산을 처리하는 `useCouponAndCalculateDiscount` 메서드를 `CouponService`에 추가
1 parent 007c152 commit d080da2

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.loopers.domain.coupon;
2+
3+
public record CouponDiscountResult(
4+
long discountAmount,
5+
Long usedCouponId
6+
) {
7+
8+
}

apps/commerce-api/src/main/java/com/loopers/domain/coupon/CouponService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.loopers.support.error.CoreException;
44
import com.loopers.support.error.ErrorType;
5+
import jakarta.transaction.Transactional;
56
import lombok.RequiredArgsConstructor;
67
import org.springframework.stereotype.Component;
78

@@ -16,4 +17,23 @@ public Coupon getCoupon(Long id) {
1617
return couponRepository.findById(id)
1718
.orElseThrow(() -> new CoreException(ErrorType.NOT_FOUND, "쿠폰을 찾을 수 없습니다."));
1819
}
20+
21+
@Transactional
22+
public CouponDiscountResult useCouponAndCalculateDiscount(
23+
Long couponId,
24+
long totalOrderAmount
25+
) {
26+
Coupon coupon = couponRepository.findById(couponId)
27+
.orElseThrow(() -> new CoreException(ErrorType.NOT_FOUND, "쿠폰을 찾을 수 없습니다."));
28+
29+
try {
30+
coupon.use();
31+
} catch (CoreException e) {
32+
throw new CoreException(ErrorType.BAD_REQUEST, "이미 사용되었거나 사용할 수 없는 쿠폰입니다.");
33+
}
34+
35+
long discountAmount = coupon.calculateDiscountAmount(totalOrderAmount);
36+
37+
return new CouponDiscountResult(discountAmount, couponId);
38+
}
1939
}

apps/commerce-api/src/main/java/com/loopers/domain/order/OrderCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class OrderCommand {
66

77
public record PlaceOrder(
88
Long userId,
9+
Long couponId,
910
List<Item> items
1011
) {
1112

0 commit comments

Comments
 (0)