From 112498ca2b9d2a0ad6e2fb79747fb140ac7d4bbe Mon Sep 17 00:00:00 2001 From: HyuckJuneHong Date: Wed, 29 Nov 2023 02:00:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=BF=A0=ED=8F=B0=20=EB=B0=9C?= =?UTF-8?q?=EA=B8=89=20=ED=98=84=EC=9E=AC=20=EC=9C=84=EC=B9=98=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coupon/CouponManageService.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/moabam/api/application/coupon/CouponManageService.java b/src/main/java/com/moabam/api/application/coupon/CouponManageService.java index 38609013..1306f16b 100644 --- a/src/main/java/com/moabam/api/application/coupon/CouponManageService.java +++ b/src/main/java/com/moabam/api/application/coupon/CouponManageService.java @@ -26,10 +26,10 @@ @RequiredArgsConstructor public class CouponManageService { - private static final long ISSUE_SIZE = 10; - private static final long FIRST_INDEX = 0; private static final String SUCCESS_ISSUE_BODY = "%s 쿠폰 발행을 성공했습니다. 축하드립니다!"; private static final String FAIL_ISSUE_BODY = "%s 쿠폰 발행을 실패했습니다. 다음 기회에!"; + private static final long ISSUE_SIZE = 10; + private static final long ISSUE_FIRST = 0; private final ClockHolder clockHolder; private final NotificationService notificationService; @@ -38,13 +38,11 @@ public class CouponManageService { private final CouponManageRepository couponManageRepository; private final CouponWalletRepository couponWalletRepository; - private long start = FIRST_INDEX; - private long end = ISSUE_SIZE; + private long current = ISSUE_FIRST; @Scheduled(cron = "0 0 0 * * *") public void init() { - start = FIRST_INDEX; - end = ISSUE_SIZE; + current = ISSUE_FIRST; } @Scheduled(fixedDelay = 1000) @@ -58,23 +56,22 @@ public void issue() { Coupon coupon = optionalCoupon.get(); String couponName = coupon.getName(); - Set membersId = couponManageRepository.range(couponName, start, end); + int maxStock = coupon.getStock(); + + Set membersId = couponManageRepository.range(couponName, current, current + ISSUE_SIZE); for (Long memberId : membersId) { - int nextStock = couponManageRepository.increaseIssuedStock(coupon.getName()); + int nextStock = couponManageRepository.increaseIssuedStock(couponName); - if (coupon.getStock() < nextStock) { - notificationService.sendCouponIssueResult(memberId, coupon.getName(), FAIL_ISSUE_BODY); + if (maxStock < nextStock) { + notificationService.sendCouponIssueResult(memberId, couponName, FAIL_ISSUE_BODY); continue; } - CouponWallet couponWallet = CouponWallet.create(memberId, coupon); - couponWalletRepository.save(couponWallet); - notificationService.sendCouponIssueResult(memberId, coupon.getName(), SUCCESS_ISSUE_BODY); + couponWalletRepository.save(CouponWallet.create(memberId, coupon)); + notificationService.sendCouponIssueResult(memberId, couponName, SUCCESS_ISSUE_BODY); + current++; } - - start = end; - end = Math.min(couponManageRepository.queueSize(couponName), end + ISSUE_SIZE); } public void register(AuthMember authMember, String couponName) {