Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

사용자 쿠폰 발급 로직 개선 #109

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -16,6 +19,9 @@ public interface CouponRepository extends JpaRepository<Coupon, Long> {
@Query("select c from Coupon c where c.couponId=:couponId")
Coupon findByIdWithPessimisticLock(Long couponId);

@Query("select c.usageHours from Coupon c where c.couponId=:couponId")
Long findCouponUsageHours(Long couponId);

@Query("select c from Coupon c " +
"where c.issuePeriod.startedAt <= :currentTime and :currentTime <= c.issuePeriod.finishedAt")
List<Coupon> findCouponsInProgress(LocalDateTime currentTime, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ public class IssueUserCouponModuleImpl implements IssueUserCouponModule {
@Override
@Transactional
public Long issue(IssueUserCouponCommand command) {
UserCoupon issuedUserCoupon = issueUserCoupon(command);
return saveCoupon(issuedUserCoupon);
}

private UserCoupon issueUserCoupon(IssueUserCouponCommand command) {
Coupon coupon = couponRepository.findByIdWithPessimisticLock(command.couponId());

LocalDateTime currentTime = LocalDateTime.now();
Long userCouponId = issueUserCoupon(command, currentTime);
decreaseCouponQuantity(command, currentTime);
return userCouponId;
}

coupon.decrease(currentTime);
return new UserCoupon(command.userId(), coupon.getCouponId(), coupon.getUsageHours(), currentTime);
private Long issueUserCoupon(IssueUserCouponCommand command, LocalDateTime currentTime) {
Long couponUsageHours = couponRepository.findCouponUsageHours(command.couponId());
UserCoupon issuedCoupon = new UserCoupon(command.userId(), command.couponId(), couponUsageHours, currentTime);
return userCouponRepository.save(issuedCoupon)
.getUserCouponId();
}

private Long saveCoupon(UserCoupon issuedCoupon) {
return userCouponRepository.save(issuedCoupon).getUserCouponId();
private void decreaseCouponQuantity(IssueUserCouponCommand command, LocalDateTime currentTime) {
Coupon coupon = couponRepository.findByIdWithPessimisticLock(command.couponId());
coupon.decrease(currentTime);
}

}