File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
src/main/java/com/threestar/trainus/domain/coupon/user Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 11package com .threestar .trainus .domain .coupon .user .repository ;
22
33import java .util .List ;
4+ import java .util .Optional ;
45
56import org .springframework .data .jpa .repository .JpaRepository ;
67import org .springframework .data .jpa .repository .Query ;
1213public interface UserCouponRepository extends JpaRepository <UserCoupon , Long > {
1314 boolean existsByUserIdAndCouponId (Long userId , Long couponId );
1415
16+ Optional <UserCoupon > findByUserIdAndCouponId (Long userId , Long couponId );
17+
1518 @ Query ("SELECT uc FROM UserCoupon uc JOIN FETCH uc.coupon WHERE uc.user.id = :userId" )
1619 List <UserCoupon > findAllByUserIdWithCoupon (@ Param ("userId" ) Long userId );
1720
Original file line number Diff line number Diff line change @@ -94,4 +94,35 @@ public CouponPageResponseDto getCoupons(Long userId) {
9494 .coupons (dtoList )
9595 .build ();
9696 }
97+
98+ @ Transactional
99+ public UserCoupon getValidUserCoupon (Long userCouponId , Long userId ) {
100+ return userCouponRepository .findByUserIdAndCouponId (userId , userCouponId )
101+ .filter (c -> c .getStatus () == CouponStatus .ACTIVE )
102+ .orElseThrow (() -> new BusinessException (ErrorCode .COUPON_NOT_FOUND ));
103+ }
104+
105+ public int calculateDiscountedPrice (int originalPrice , UserCoupon coupon ) {
106+ String discountPrice = coupon .getCoupon ().getDiscountPrice ();
107+ if (discountPrice .contains ("%" )) {
108+ int discountPercentage = Integer .parseInt (discountPrice .substring (0 , discountPrice .indexOf ("%" )));
109+ return originalPrice * discountPercentage / 100 ;
110+ } else {
111+ return Integer .parseInt (discountPrice .replace ("원" , "" ));
112+ }
113+ }
114+
115+ public void useCoupon (UserCoupon coupon ) {
116+ if (coupon .getStatus () == CouponStatus .ACTIVE ) {
117+ coupon .use ();
118+ userCouponRepository .save (coupon );
119+ }
120+ }
121+
122+ public void restoreCoupon (UserCoupon coupon ) {
123+ if (coupon .getStatus () == CouponStatus .INACTIVE ) {
124+ coupon .restore ();
125+ userCouponRepository .save (coupon );
126+ }
127+ }
97128}
You can’t perform that action at this time.
0 commit comments