-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 쿠폰 발급 요청 및 대기열 사용자 쿠폰 발급 처리 구현 (#146)
* style : Schedule 어노테이션 위치 변경 * refactor: 쿠폰 발행 기간 하루로 통일 및 쿠폰 정보 오픈 날짜 추가 * feat: 쿠폰 발행 가능 날짜 중복 체크 기능 추가 * refactor: Builder 삭제 * test: 쿠폰 관련 테스트 수정 * feat: 쿠폰 발행 관련 레포지토리 기능 구현 및 테스트 * test: 쿠폰 발행 관련 문자열 레디스 기능 구현 및 테스트 * feat: 쿠폰 발행 관련 ZSET 레디스 기능 구현 및 테스트 * test: 쿠폰 발행 컨트롤러 기능 테스트 * test: RestDoc 업데이트 * test: Github Actions 시, Redis ZSET 명령어 못찾는 테스트 Disable
- Loading branch information
Showing
29 changed files
with
797 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/moabam/api/application/coupon/CouponManageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.moabam.api.application.coupon; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.moabam.api.domain.coupon.Coupon; | ||
import com.moabam.api.domain.coupon.CouponWallet; | ||
import com.moabam.api.domain.coupon.repository.CouponManageRepository; | ||
import com.moabam.api.domain.coupon.repository.CouponRepository; | ||
import com.moabam.api.domain.coupon.repository.CouponWalletRepository; | ||
import com.moabam.global.auth.model.AuthMember; | ||
import com.moabam.global.common.util.ClockHolder; | ||
import com.moabam.global.error.exception.BadRequestException; | ||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class CouponManageService { | ||
|
||
private static final long ISSUE_SIZE = 10; | ||
|
||
private final ClockHolder clockHolder; | ||
|
||
private final CouponRepository couponRepository; | ||
private final CouponManageRepository couponManageRepository; | ||
private final CouponWalletRepository couponWalletRepository; | ||
|
||
@Scheduled(fixedDelay = 1000) | ||
public void issue() { | ||
LocalDate now = LocalDate.from(clockHolder.times()); | ||
Optional<Coupon> isCoupon = couponRepository.findByStartAt(now); | ||
|
||
if (!canIssue(isCoupon)) { | ||
return; | ||
} | ||
|
||
Coupon coupon = isCoupon.get(); | ||
Set<Long> membersId = couponManageRepository.popMinQueue(coupon.getName(), ISSUE_SIZE); | ||
|
||
membersId.forEach(memberId -> { | ||
int nextStock = couponManageRepository.increaseIssuedStock(coupon.getName()); | ||
|
||
if (coupon.getStock() < nextStock) { | ||
return; | ||
} | ||
|
||
CouponWallet couponWallet = CouponWallet.create(memberId, coupon); | ||
couponWalletRepository.save(couponWallet); | ||
}); | ||
} | ||
|
||
public void register(AuthMember authMember, String couponName) { | ||
double registerTime = System.currentTimeMillis(); | ||
validateRegister(couponName); | ||
couponManageRepository.addIfAbsentQueue(couponName, authMember.id(), registerTime); | ||
} | ||
|
||
public void deleteCouponManage(String couponName) { | ||
couponManageRepository.deleteQueue(couponName); | ||
couponManageRepository.deleteIssuedStock(couponName); | ||
} | ||
|
||
private void validateRegister(String couponName) { | ||
LocalDate now = LocalDate.from(clockHolder.times()); | ||
Optional<Coupon> coupon = couponRepository.findByStartAt(now); | ||
|
||
if (coupon.isEmpty() || !coupon.get().getName().equals(couponName)) { | ||
throw new BadRequestException(ErrorMessage.INVALID_COUPON_PERIOD); | ||
} | ||
} | ||
|
||
private boolean canIssue(Optional<Coupon> coupon) { | ||
if (coupon.isEmpty()) { | ||
return false; | ||
} | ||
|
||
Coupon currentCoupon = coupon.get(); | ||
int currentStock = couponManageRepository.getIssuedStock(currentCoupon.getName()); | ||
int maxStock = currentCoupon.getStock(); | ||
|
||
return currentStock < maxStock; | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
src/main/java/com/moabam/api/application/coupon/CouponQueueService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/moabam/api/domain/coupon/repository/CouponManageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.moabam.api.domain.coupon.repository; | ||
|
||
import static java.util.Objects.*; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import com.moabam.api.infrastructure.redis.StringRedisRepository; | ||
import com.moabam.api.infrastructure.redis.ZSetRedisRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class CouponManageRepository { | ||
|
||
private static final String STOCK_KEY = "%s_INCR"; | ||
|
||
private final ZSetRedisRepository zSetRedisRepository; | ||
private final StringRedisRepository stringRedisRepository; | ||
|
||
public void addIfAbsentQueue(String couponName, Long memberId, double registerTime) { | ||
zSetRedisRepository.addIfAbsent(requireNonNull(couponName), requireNonNull(memberId), registerTime); | ||
} | ||
|
||
public Set<Long> popMinQueue(String couponName, long count) { | ||
return zSetRedisRepository | ||
.popMin(requireNonNull(couponName), count) | ||
.stream() | ||
.map(tuple -> (Long)tuple.getValue()) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
public void deleteQueue(String couponName) { | ||
stringRedisRepository.delete(requireNonNull(couponName)); | ||
} | ||
|
||
public int increaseIssuedStock(String couponName) { | ||
String stockKey = String.format(STOCK_KEY, requireNonNull(couponName)); | ||
|
||
return stringRedisRepository | ||
.increment(requireNonNull(stockKey)) | ||
.intValue(); | ||
} | ||
|
||
public int getIssuedStock(String couponName) { | ||
String stockKey = String.format(STOCK_KEY, requireNonNull(couponName)); | ||
String stockValue = stringRedisRepository.get(requireNonNull(stockKey)); | ||
|
||
if (stockValue == null) { | ||
return 0; | ||
} | ||
|
||
return Integer.parseInt(stockValue); | ||
} | ||
|
||
public void deleteIssuedStock(String couponName) { | ||
String stockKey = String.format(STOCK_KEY, requireNonNull(couponName)); | ||
stringRedisRepository.delete(requireNonNull(stockKey)); | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
src/main/java/com/moabam/api/domain/coupon/repository/CouponQueueRepository.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.