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 bf3d5934..67e3bac7 100644 --- a/src/main/java/com/moabam/api/application/coupon/CouponManageService.java +++ b/src/main/java/com/moabam/api/application/coupon/CouponManageService.java @@ -49,7 +49,7 @@ public void issue() { Coupon coupon = optionalCoupon.get(); String couponName = coupon.getName(); int maxCount = coupon.getMaxCount(); - int currentCount = couponManageRepository.getCouponCount(couponName); + int currentCount = couponManageRepository.getCount(couponName); if (maxCount <= currentCount) { return; @@ -57,6 +57,10 @@ public void issue() { Set membersId = couponManageRepository.rangeQueue(couponName, currentCount, currentCount + ISSUE_SIZE); + if (membersId.isEmpty()) { + return; + } + for (Long memberId : membersId) { couponWalletRepository.save(CouponWallet.create(memberId, coupon)); notificationService.sendCouponIssueResult(memberId, couponName, SUCCESS_ISSUE_BODY); @@ -73,6 +77,7 @@ public void registerQueue(String couponName, Long memberId) { public void deleteQueue(String couponName) { couponManageRepository.deleteQueue(couponName); + couponManageRepository.deleteCount(couponName); } private void validateRegisterQueue(String couponName, Long memberId) { diff --git a/src/main/java/com/moabam/api/domain/coupon/repository/CouponManageRepository.java b/src/main/java/com/moabam/api/domain/coupon/repository/CouponManageRepository.java index 62864dde..44e2c150 100644 --- a/src/main/java/com/moabam/api/domain/coupon/repository/CouponManageRepository.java +++ b/src/main/java/com/moabam/api/domain/coupon/repository/CouponManageRepository.java @@ -41,12 +41,12 @@ public Set rangeQueue(String couponName, long start, long end) { } public boolean hasValue(String couponName, Long memberId) { - return Objects.nonNull(zSetRedisRepository.score(couponName, memberId)); + return Objects.nonNull(zSetRedisRepository.score(requireNonNull(couponName), memberId)); } public int sizeQueue(String couponName) { return zSetRedisRepository - .size(couponName) + .size(requireNonNull(couponName)) .intValue(); } @@ -56,17 +56,22 @@ public int rankQueue(String couponName, Long memberId) { .intValue(); } - public int getCouponCount(String couponName) { - String couponCountKey = String.format(COUPON_COUNT_KEY, couponName); + public int getCount(String couponName) { + String couponCountKey = String.format(COUPON_COUNT_KEY, requireNonNull(couponName)); return Integer.parseInt(valueRedisRepository.get(couponCountKey)); } public void increase(String couponName, long count) { - String couponCountKey = String.format(COUPON_COUNT_KEY, couponName); + String couponCountKey = String.format(COUPON_COUNT_KEY, requireNonNull(couponName)); valueRedisRepository.increment(couponCountKey, count); } public void deleteQueue(String couponName) { valueRedisRepository.delete(requireNonNull(couponName)); } + + public void deleteCount(String couponName) { + String couponCountKey = String.format(COUPON_COUNT_KEY, requireNonNull(couponName)); + valueRedisRepository.delete(couponCountKey); + } } diff --git a/src/main/resources/static/docs/coupon.html b/src/main/resources/static/docs/coupon.html index f677031b..d7435ce2 100644 --- a/src/main/resources/static/docs/coupon.html +++ b/src/main/resources/static/docs/coupon.html @@ -498,7 +498,7 @@

쿠폰 삭제

요청

-
DELETE /admins/coupons/34 HTTP/1.1
+
DELETE /admins/coupons/33 HTTP/1.1
 Host: localhost:8080
@@ -526,7 +526,7 @@

특정 쿠폰 조회

요청

-
GET /coupons/22 HTTP/1.1
+
GET /coupons/21 HTTP/1.1
 Host: localhost:8080
@@ -543,7 +543,7 @@

응답

Content-Length: 201 { - "id" : 22, + "id" : 21, "adminName" : "ID : 1", "name" : "couponName", "description" : "", @@ -593,7 +593,7 @@

응답

Content-Length: 202 [ { - "id" : 23, + "id" : 22, "adminName" : "ID : 1", "name" : "coupon1", "description" : "", diff --git a/src/test/java/com/moabam/api/application/coupon/CouponManageServiceTest.java b/src/test/java/com/moabam/api/application/coupon/CouponManageServiceTest.java index bd739907..baa32d07 100644 --- a/src/test/java/com/moabam/api/application/coupon/CouponManageServiceTest.java +++ b/src/test/java/com/moabam/api/application/coupon/CouponManageServiceTest.java @@ -61,7 +61,7 @@ void issue_all_success(Set values) { given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon)); given(couponManageRepository.rangeQueue(any(String.class), any(long.class), any(long.class))) .willReturn(values); - given(couponManageRepository.getCouponCount(any(String.class))).willReturn(coupon.getMaxCount() - 1); + given(couponManageRepository.getCount(any(String.class))).willReturn(coupon.getMaxCount() - 1); // When couponManageService.issue(); @@ -99,7 +99,7 @@ void issue_stockEnd() { given(clockHolder.date()).willReturn(LocalDate.now()); given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon)); - given(couponManageRepository.getCouponCount(any(String.class))).willReturn(coupon.getMaxCount()); + given(couponManageRepository.getCount(any(String.class))).willReturn(coupon.getMaxCount()); // When couponManageService.issue(); diff --git a/src/test/java/com/moabam/api/presentation/CouponControllerTest.java b/src/test/java/com/moabam/api/presentation/CouponControllerTest.java index 1193a93f..fd968940 100644 --- a/src/test/java/com/moabam/api/presentation/CouponControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/CouponControllerTest.java @@ -447,9 +447,6 @@ void registerQueue_Zero_StartAt_BadRequestException() throws Exception { @Test void registerQueue_Not_StartAt_BadRequestException() throws Exception { // Given - Coupon couponFixture = CouponFixture.coupon(); - couponRepository.save(couponFixture); - given(clockHolder.date()).willReturn(LocalDate.of(2022, 2, 1)); // When & Then