Skip to content

Commit ddc69bb

Browse files
committed
Merge branch 'develop' into feature/#182-feature-customer-order-get-api
2 parents 70e1ab1 + 4e09cc5 commit ddc69bb

57 files changed

Lines changed: 951 additions & 52 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ clean {
5151
delete querydslDirProperty
5252
}
5353

54+
//feign client를 위한 springcloud
55+
ext {
56+
set('springCloudVersion', "2025.0.0")
57+
}
58+
59+
dependencyManagement {
60+
imports {
61+
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
62+
}
63+
}
64+
5465
dependencies {
5566
implementation 'org.springframework.boot:spring-boot-starter-web'
5667

@@ -77,6 +88,10 @@ dependencies {
7788
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
7889

7990

91+
//feign client
92+
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
93+
implementation group: 'io.github.openfeign', name: 'feign-gson', version: '11.0'
94+
8095
compileOnly 'org.projectlombok:lombok'
8196
annotationProcessor 'org.projectlombok:lombok'
8297
testImplementation 'org.springframework.boot:spring-boot-starter-test'

src/main/java/com/irum/come2us/Come2usApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.openfeign.EnableFeignClients;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
57

68
@SpringBootApplication
9+
@EnableFeignClients
10+
@EnableScheduling
711
public class Come2usApplication {
812

913
public static void main(String[] args) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.irum.come2us.domain.coupon.application.service;
2+
3+
import com.irum.come2us.domain.coupon.domain.entity.AppliedCoupon;
4+
import com.irum.come2us.domain.coupon.domain.entity.Coupon;
5+
import com.irum.come2us.domain.coupon.domain.repository.AppliedCouponRepository;
6+
import com.irum.come2us.domain.coupon.domain.repository.CouponRepository;
7+
import com.irum.come2us.domain.payment.domain.entity.Payment;
8+
import java.util.List;
9+
import java.util.UUID;
10+
import lombok.RequiredArgsConstructor;
11+
import org.springframework.stereotype.Service;
12+
import org.springframework.transaction.annotation.Propagation;
13+
import org.springframework.transaction.annotation.Transactional;
14+
15+
@Service
16+
@RequiredArgsConstructor
17+
public class AppliedCouponService {
18+
private final AppliedCouponRepository appliedCouponRepository;
19+
private final CouponRepository couponRepository;
20+
21+
/** 쿠폰 사용 처리 */
22+
public void createAppliedCouponList(Payment payment, List<UUID> couponIdList) {
23+
List<Coupon> couponList = couponRepository.findAllById(couponIdList);
24+
25+
List<AppliedCoupon> appliedCouponList =
26+
couponList.stream()
27+
.map(
28+
coupon ->
29+
AppliedCoupon.builder()
30+
.payment(payment)
31+
.coupon(coupon)
32+
.build())
33+
.toList();
34+
35+
appliedCouponRepository.saveAll(appliedCouponList);
36+
}
37+
38+
/** 롤백 */
39+
@Transactional(propagation = Propagation.REQUIRES_NEW)
40+
public void rollbackAppliedCouponList(Payment payment) {
41+
appliedCouponRepository.deleteByPayment(payment);
42+
}
43+
}

src/main/java/com/irum/come2us/domain/coupon/application/service/CouponService.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.irum.come2us.domain.coupon.application.service;
22

33
import com.irum.come2us.domain.coupon.domain.entity.Coupon;
4+
import com.irum.come2us.domain.coupon.domain.repository.AppliedCouponRepository;
45
import com.irum.come2us.domain.coupon.domain.repository.CouponRepository;
56
import com.irum.come2us.domain.coupon.presentation.dto.request.CouponGenerateRequest;
67
import com.irum.come2us.domain.member.domain.entity.Member;
78
import com.irum.come2us.domain.member.domain.repository.MemberRepository;
89
import com.irum.come2us.global.presentation.advice.exception.CommonException;
910
import com.irum.come2us.global.presentation.advice.exception.errorcode.CouponErrorCode;
1011
import com.irum.come2us.global.presentation.advice.exception.errorcode.MemberErrorCode;
12+
import java.time.LocalDateTime;
1113
import java.util.List;
1214
import java.util.UUID;
1315
import lombok.RequiredArgsConstructor;
@@ -20,6 +22,7 @@
2022
public class CouponService {
2123
private final CouponRepository couponRepository;
2224
private final MemberRepository memberRepository;
25+
private final AppliedCouponRepository appliedCouponRepository;
2326

2427
public void createCoupon(CouponGenerateRequest request, Long memberId) {
2528

@@ -51,4 +54,36 @@ public void deleteCoupon(UUID couponId, Long memberId) {
5154
}
5255
couponRepository.delete(coupon);
5356
}
57+
58+
/** 쿠폰 유효성 검증 및 할인 금액 계산 */
59+
public int validAndCalCoupon(List<UUID> couponIdList, int calculatedTotalPrice, Member member) {
60+
if (couponIdList.isEmpty()) {
61+
return 0;
62+
}
63+
64+
int totalDiscount = 0;
65+
List<Coupon> couponList = couponRepository.findAllById(couponIdList);
66+
67+
for (Coupon coupon : couponList) {
68+
// 권한 검사
69+
if (!coupon.getMember().getMemberId().equals(member.getMemberId())) {
70+
throw new CommonException(CouponErrorCode.COUPON_NO_PERMISSION);
71+
}
72+
// 만료일 검사
73+
if (coupon.getExpiration().isBefore(LocalDateTime.now())) {
74+
throw new CommonException(CouponErrorCode.COUPON_EXPIRATION);
75+
}
76+
// 사용 여부 검사
77+
if (appliedCouponRepository.existsByCouponId(coupon.getId())) {
78+
throw new CommonException(CouponErrorCode.COUPON_ALREADY_USED);
79+
}
80+
totalDiscount += coupon.getDiscountAmount();
81+
}
82+
83+
if (totalDiscount > calculatedTotalPrice) {
84+
totalDiscount = calculatedTotalPrice;
85+
}
86+
87+
return totalDiscount;
88+
}
5489
}

src/main/java/com/irum/come2us/domain/coupon/domain/entity/AppliedCoupon.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import jakarta.persistence.*;
66
import java.util.UUID;
77
import lombok.AccessLevel;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Builder;
810
import lombok.Getter;
911
import lombok.NoArgsConstructor;
1012
import org.hibernate.annotations.SQLDelete;
@@ -16,6 +18,8 @@
1618
@Entity
1719
@Table(name = "p_applied_coupon")
1820
@Getter
21+
@Builder
22+
@AllArgsConstructor
1923
@SQLDelete(sql = "UPDATE p_applied_coupon SET deleted_at = NOW() WHERE applied_coupon_id = ?")
2024
@Where(clause = "deleted_at IS NULL")
2125
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.irum.come2us.domain.coupon.domain.repository;
22

33
import com.irum.come2us.domain.coupon.domain.entity.AppliedCoupon;
4+
import com.irum.come2us.domain.payment.domain.entity.Payment;
45
import java.util.List;
56
import java.util.UUID;
67
import org.springframework.data.jpa.repository.JpaRepository;
78

89
public interface AppliedCouponRepository extends JpaRepository<AppliedCoupon, UUID> {
910
List<AppliedCoupon> findByCouponId(UUID couponId);
11+
12+
boolean existsByCouponId(UUID couponId);
13+
14+
void deleteByPayment(Payment payment);
1015
}

src/main/java/com/irum/come2us/domain/order/application/mapper/CustomerOrderMapper.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.irum.come2us.domain.order.presentation.dto.response.OrderDetailResponse;
1010
import com.irum.come2us.domain.refund.domain.entity.Refund;
1111
import com.irum.come2us.domain.refund.domain.entity.enums.RefundStatus;
12+
import com.irum.come2us.domain.order.presentation.dto.response.AddressResponse;
13+
import com.irum.come2us.domain.order.presentation.dto.response.CustomerOrderResponse;
1214
import java.util.List;
1315
import org.springframework.stereotype.Component;
1416

@@ -56,6 +58,32 @@ private static OrderDetailResponse.ProductResponse toProductResponse(OrderDetail
5658
.build();
5759
}
5860

61+
public static CustomerOrderResponse toCustomerOrderResponse(
62+
Order order, List<OrderDetail> orderDetailList) {
63+
List<CustomerOrderResponse.ProductSummary> productSummaryList =
64+
orderDetailList.stream().map(CustomerOrderMapper::toProductSummary).toList();
65+
66+
return CustomerOrderResponse.builder()
67+
.orderId(order.getOrderId())
68+
.address(AddressResponse.from(order.getDeliveryAddress().getAddress()))
69+
.totalProductPrice(order.getTotalPrice())
70+
.totalDiscountAmount(order.getPayment().getTotalDiscountAmount())
71+
.totalPaymentAmount(order.getPayment().getAmount())
72+
.productList(productSummaryList)
73+
.build();
74+
}
75+
76+
private static CustomerOrderResponse.ProductSummary toProductSummary(OrderDetail orderDetail) {
77+
return CustomerOrderResponse.ProductSummary.builder()
78+
.orderDetailId(orderDetail.getOrderDetailId())
79+
.productName(orderDetail.getProductName())
80+
.optionName(orderDetail.getOptionName())
81+
.price(orderDetail.getPrice())
82+
.quantity(orderDetail.getQuantity())
83+
.build();
84+
}
85+
86+
5987
public static CustomerOrderListResponse.OrderResponse toOrderResponse(
6088
CustomerOrderSummaryRow or, List<CustomerOrderListResponse.ProductResponse> pList) {
6189

0 commit comments

Comments
 (0)