Skip to content

Commit 53fde2f

Browse files
committed
feat: 사용자가 사용한 쿠폰 리스트 조회
1 parent c61b1a9 commit 53fde2f

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

src/main/java/com/trashheroesbe/feature/coupon/api/CouponPartnerController.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.trashheroesbe.feature.coupon.dto.response.CouponCreateResponse;
99
import com.trashheroesbe.feature.coupon.dto.response.CouponUsageStatisticsResponse;
1010
import com.trashheroesbe.feature.coupon.dto.response.PartnerCouponResponse;
11+
import com.trashheroesbe.feature.coupon.dto.response.UsedCouponListResponse;
1112
import com.trashheroesbe.global.response.ApiResponse;
1213
import com.trashheroesbe.global.auth.security.CustomerDetails;
1314
import jakarta.validation.Valid;
@@ -83,8 +84,17 @@ public ApiResponse<Void> useCoupon(
8384
@AuthenticationPrincipal CustomerDetails customerDetails,
8485
@PathVariable Long userCouponId
8586
) {
86-
couponService.userCoupon(customerDetails, userCouponId);
87+
couponService.useCoupon(customerDetails, userCouponId);
8788
return ApiResponse.success(OK);
8889
}
8990

91+
@Override
92+
@GetMapping("/coupons/used")
93+
public ApiResponse<List<UsedCouponListResponse>> getUsedCoupons(
94+
@AuthenticationPrincipal CustomerDetails customerDetails
95+
) {
96+
List<UsedCouponListResponse> responses = couponService.getUsedCoupons(customerDetails);
97+
return ApiResponse.success(OK, responses);
98+
}
99+
90100
}

src/main/java/com/trashheroesbe/feature/coupon/api/CouponPartnerControllerApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.trashheroesbe.feature.coupon.dto.response.CouponCreateResponse;
66
import com.trashheroesbe.feature.coupon.dto.response.CouponUsageStatisticsResponse;
77
import com.trashheroesbe.feature.coupon.dto.response.PartnerCouponResponse;
8+
import com.trashheroesbe.feature.coupon.dto.response.UsedCouponListResponse;
89
import com.trashheroesbe.global.response.ApiResponse;
910
import io.swagger.v3.oas.annotations.Operation;
1011
import io.swagger.v3.oas.annotations.Parameter;
@@ -54,6 +55,10 @@ ApiResponse<Void> useCoupon(
5455
@Parameter(description = "유저쿠폰 ID", required = true) @PathVariable Long userCouponId
5556
);
5657

58+
@Operation(summary = "사용한 파트너 쿠폰 전체 조회", description = "파트너가 발급한 쿠폰 중 사용된 쿠폰 목록을 조회합니다.")
59+
ApiResponse<List<UsedCouponListResponse>> getUsedCoupons(
60+
@AuthenticationPrincipal CustomerDetails customerDetails
61+
);
5762

5863

5964

src/main/java/com/trashheroesbe/feature/coupon/application/CouponService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.trashheroesbe.feature.coupon.dto.response.CouponUsageStatisticsResponse.UsageSummary;
1111
import com.trashheroesbe.feature.coupon.dto.response.CouponUsageStatisticsResponse.UsageSummary.PeriodRange;
1212
import com.trashheroesbe.feature.coupon.dto.response.PartnerCouponResponse;
13+
import com.trashheroesbe.feature.coupon.dto.response.UsedCouponListResponse;
14+
import com.trashheroesbe.feature.coupon.dto.response.UserCouponResponse;
1315
import com.trashheroesbe.feature.coupon.infrastructure.CouponRepository;
1416
import com.trashheroesbe.feature.coupon.infrastructure.UserCouponRepository;
1517
import com.trashheroesbe.feature.partner.domain.entity.Partner;
@@ -33,6 +35,7 @@ public class CouponService {
3335
private final CouponRepository couponRepository;
3436
private final UserCouponRepository userCouponRepository;
3537

38+
3639
@Transactional(readOnly = true)
3740
public List<PartnerCouponResponse> getPartnerCoupons(CustomerDetails customerDetails) {
3841
Partner partner = extractPartner(customerDetails);
@@ -115,14 +118,23 @@ public CouponUsageStatisticsResponse getCouponUsage(CustomerDetails customerDeta
115118
);
116119
}
117120

118-
public void userCoupon(CustomerDetails customerDetails, Long userCouponId) {
121+
public void useCoupon(CustomerDetails customerDetails, Long userCouponId) {
119122
Partner partner = extractPartner(customerDetails);
120123

121124
UserCoupon userCoupon = userCouponRepository.findById(userCouponId)
122125
.orElseThrow(() -> new BusinessException(ErrorCode.ENTITY_NOT_FOUND));
123126
userCoupon.useCoupon();
124127
}
125128

129+
@Transactional(readOnly = true)
130+
public List<UsedCouponListResponse> getUsedCoupons(CustomerDetails customerDetails) {
131+
Partner partner = extractPartner(customerDetails);
132+
133+
return userCouponRepository.findUsedByPartnerId(partner.getId()).stream()
134+
.map(UsedCouponListResponse::from)
135+
.toList();
136+
}
137+
126138
private Partner extractPartner(CustomerDetails customerDetails) {
127139
if (customerDetails == null || customerDetails.getUser() == null) {
128140
throw new BusinessException(ErrorCode.ACCESS_DENIED_EXCEPTION);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.trashheroesbe.feature.coupon.dto.response;
2+
3+
import com.trashheroesbe.feature.coupon.domain.entity.UserCoupon;
4+
import java.time.LocalDateTime;
5+
6+
public record UsedCouponListResponse(
7+
Long UserCouponId,
8+
String CouponStatusDescription,
9+
LocalDateTime usedAt,
10+
11+
Long userId,
12+
String userNickName,
13+
14+
Long couponId,
15+
String couponTitle,
16+
String couponTypeDescription
17+
) {
18+
19+
public static UsedCouponListResponse from(UserCoupon userCoupon) {
20+
return new UsedCouponListResponse(
21+
userCoupon.getId(),
22+
userCoupon.getStatus().getDescription(),
23+
userCoupon.getUsedAt(),
24+
userCoupon.getUser().getId(),
25+
userCoupon.getUser().getNickname(),
26+
userCoupon.getCoupon().getId(),
27+
userCoupon.getCoupon().getTitle(),
28+
userCoupon.getCoupon().getType().getDescription()
29+
);
30+
}
31+
}

src/main/java/com/trashheroesbe/feature/coupon/infrastructure/UserCouponRepository.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ long countByCouponPartnerIdAndStatus(
4141
@Param("partnerId") Long partnerId,
4242
@Param("status") CouponStatus status
4343
);
44+
45+
@Query("SELECT uc FROM UserCoupon uc " +
46+
"JOIN FETCH uc.coupon c " +
47+
"JOIN FETCH uc.user u " +
48+
"WHERE c.partner.id = :partnerId " +
49+
"AND uc.status = 'USED' " +
50+
"ORDER BY uc.usedAt DESC")
51+
List<UserCoupon> findUsedByPartnerId(@Param("partnerId") Long partnerId);
4452
}

0 commit comments

Comments
 (0)