Skip to content

Commit

Permalink
hotfix: 쿠폰큐 비어있을 시, 발생하는 버그 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hongdosan committed Dec 1, 2023
1 parent 7f9dc7c commit aa8c32f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ 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;
}

Set<Long> 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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public Set<Long> 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();
}

Expand All @@ -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);
}
}
8 changes: 4 additions & 4 deletions src/main/resources/static/docs/coupon.html
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ <h3 id="_쿠폰_삭제">쿠폰 삭제</h3>
<h4 id="_요청_2" class="discrete">요청</h4>
<div class="listingblock">
<div class="content">
<pre class="highlight nowrap"><code class="language-http" data-lang="http">DELETE /admins/coupons/34 HTTP/1.1
<pre class="highlight nowrap"><code class="language-http" data-lang="http">DELETE /admins/coupons/33 HTTP/1.1
Host: localhost:8080</code></pre>
</div>
</div>
Expand Down Expand Up @@ -526,7 +526,7 @@ <h3 id="_특정_쿠폰_조회">특정 쿠폰 조회</h3>
<h4 id="_요청_3">요청</h4>
<div class="listingblock">
<div class="content">
<pre class="highlight nowrap"><code class="language-http" data-lang="http">GET /coupons/22 HTTP/1.1
<pre class="highlight nowrap"><code class="language-http" data-lang="http">GET /coupons/21 HTTP/1.1
Host: localhost:8080</code></pre>
</div>
</div>
Expand All @@ -543,7 +543,7 @@ <h4 id="_응답_3" class="discrete">응답</h4>
Content-Length: 201

{
"id" : 22,
"id" : 21,
"adminName" : "ID : 1",
"name" : "couponName",
"description" : "",
Expand Down Expand Up @@ -593,7 +593,7 @@ <h4 id="_응답_4" class="discrete">응답</h4>
Content-Length: 202

[ {
"id" : 23,
"id" : 22,
"adminName" : "ID : 1",
"name" : "coupon1",
"description" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void issue_all_success(Set<Long> 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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa8c32f

Please sign in to comment.