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

쿠폰 할인 유형 관련 싱크업 #172

Merged
merged 6 commits into from
Jan 23, 2024
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 @@ -25,8 +25,9 @@ public record CouponRegisterRequest(
@NotNull(message = "할인의 유형을 선택해야 합니다.")
@ValidEnum(enumClass = DiscountType.class, message = "올바르지 않은 할인의 유형입니다.")
String discountType,
@NotNull(message = "할인의 값을 입력해야 합니다.")
Integer discountValue,
Integer discountFlatValue,
Integer discountFlatRate,
Integer maximumDiscountPrice,

// 객실 유형
@NotNull(message = "객실의 유형을 선택해야 합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public record CouponUpdateRequest(
String customerType,
@ValidEnum(enumClass = DiscountType.class, message = "올바르지 않은 할인의 유형입니다.", required = false)
String discountType,
Integer discountValue,
Integer discountFlatValue,
Integer discountFlatRate,
Integer maximumDiscountPrice,
@ValidEnum(enumClass = CouponRoomType.class, message = "올바르지 않은 객실의 유형입니다.", required = false)
String couponRoomType,
Boolean registerAllRoom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.coolpeace.domain.coupon.dto.request.CouponRegisterRequest;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

@Component
public class CouponRegisterRequestValidator implements Validator {
public class CouponRegisterRequestValidator extends CouponRequestValidator {
@Override
public boolean supports(Class<?> clazz) {
return CouponRegisterRequest.class.equals(clazz);
Expand All @@ -17,12 +15,7 @@ public boolean supports(Class<?> clazz) {
public void validate(Object target, Errors errors) {
CouponRegisterRequest request = (CouponRegisterRequest) target;

// 방 등록
if (!request.registerAllRoom()) {
ValidationUtils.rejectIfEmpty(errors,
"registerRooms",
"registerRooms.empty",
"등록할 객실을 선택해주세요.");
}
validateRegisterRooms(errors, request.registerAllRoom());
validateDiscountValues(errors, request.discountType(), request.discountFlatValue(), request.discountFlatRate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.coolpeace.domain.coupon.dto.request.validator;

import com.coolpeace.domain.coupon.entity.type.DiscountType;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public abstract class CouponRequestValidator implements Validator {

// 방 등록 유효성 검사
protected static void validateRegisterRooms(Errors errors, boolean registerAllRoom) {
if (!registerAllRoom) {
ValidationUtils.rejectIfEmpty(errors,
"registerRooms",
"registerRooms.empty",
"등록할 객실을 선택해주세요.");
}
}

// 할인 유형 유효성 검사
protected static void validateDiscountValues(Errors errors,
String requestedDiscountTypeStr,
Integer requestedDiscountFlatValue,
Integer requestedDiscountFlatRate) {
if (requestedDiscountTypeStr.equals(DiscountType.FIXED_PRICE.getValue())) {
if (requestedDiscountFlatValue == null || requestedDiscountFlatValue <= 0) {
errors.reject("discountFlatValue.empty", "정액 할인의 경우 할인 금액를 입력해야 합니다.");
}
} else {
if (requestedDiscountFlatRate == null || requestedDiscountFlatRate <= 0) {
errors.reject("discountFlatValue.empty", "정률 할인의 경우 할인 비율을 입력해야 합니다.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
package com.coolpeace.domain.coupon.dto.request.validator;

import com.coolpeace.domain.coupon.dto.request.CouponRegisterRequest;
import com.coolpeace.domain.coupon.dto.request.CouponUpdateRequest;
import com.coolpeace.domain.coupon.entity.type.DiscountType;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class CouponUpdateRequestValidator implements Validator {
public class CouponUpdateRequestValidator extends CouponRequestValidator {
@Override
public boolean supports(Class<?> clazz) {
return CouponUpdateRequest.class.equals(clazz);
}

@Override
public void validate(Object target, Errors errors) {
CouponRegisterRequest request = (CouponRegisterRequest) target;

// 할인 유형
if (request.discountType().equals(DiscountType.FIXED_PRICE.getValue())) {
if (request.discountValue() < 100) {
errors.reject("discountValue",
"정액 할인일 경우 값을 100 이상으로 설정해야 합니다.");
}
} else {
if (request.discountValue() > 100) {
errors.reject("discountValue",
"정률 할인일 경우 값을 100 이하로 설정해야 합니다.");
}
}

// 특정 객실 등록
if (!request.registerAllRoom()) {
if (CollectionUtils.isEmpty(request.registerRooms())) {
errors.reject("registerRooms.empty",
"등록할 객실 리스트가 비어 있습니다.");
}
}
CouponUpdateRequest request = (CouponUpdateRequest) target;

validateRegisterRooms(errors, request.registerAllRoom());
validateDiscountValues(errors, request.discountType(), request.discountFlatValue(), request.discountFlatRate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.coolpeace.domain.accommodation.entity.Accommodation;
import com.coolpeace.domain.coupon.entity.Coupon;
import com.coolpeace.domain.coupon.entity.type.DiscountType;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -13,7 +14,9 @@ public record CouponResponse(
String couponStatus,
String couponConcatTitle,
String discountType,
Integer discountValue,
Integer discountFlatValue,
Integer discountFlatRate,
Integer maximumDiscountPrice,
String customerType,
String couponRoomType,
Integer minimumReservationPrice,
Expand All @@ -35,7 +38,9 @@ public static CouponResponse from(Coupon coupon) {
coupon.getCouponStatus().getValue(),
coupon.getCouponTitle(),
coupon.getDiscountType().getValue(),
coupon.getDiscountValue(),
coupon.getDiscountType().equals(DiscountType.FIXED_PRICE) ? coupon.getDiscountValue() : null,
coupon.getDiscountType().equals(DiscountType.FIXED_RATE) ? coupon.getDiscountValue() : null,
coupon.getMaximumDiscountPrice(),
coupon.getCustomerType().getValue(),
coupon.getCouponRoomType().getValue(),
coupon.getMinimumReservationPrice(),
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/coolpeace/domain/coupon/entity/Coupon.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class Coupon extends BaseTimeEntity {

private Integer discountValue;

private Integer maximumDiscountPrice;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private CustomerType customerType = CustomerType.ALL_CLIENT;
Expand Down Expand Up @@ -83,6 +85,7 @@ public class Coupon extends BaseTimeEntity {
public Coupon(String title,
DiscountType discountType,
Integer discountValue,
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
Expand All @@ -95,6 +98,7 @@ public Coupon(String title,
this.title = title;
this.discountType = discountType;
this.discountValue = discountValue;
this.maximumDiscountPrice = maximumDiscountPrice;
this.customerType = customerType;
this.couponRoomType = couponRoomType;
this.minimumReservationPrice = minimumReservationPrice;
Expand All @@ -111,6 +115,7 @@ public static Coupon from(
String title,
DiscountType discountType,
Integer discountValue,
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
Expand All @@ -125,6 +130,7 @@ public static Coupon from(
title,
discountType,
discountValue,
maximumDiscountPrice,
customerType,
couponRoomType,
minimumReservationPrice,
Expand Down Expand Up @@ -172,6 +178,7 @@ private void updateCouponStatusByExposureDate() {
public void updateCoupon(
DiscountType discountType,
Integer discountValue,
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
Expand All @@ -183,6 +190,7 @@ public void updateCoupon(
this.title = Optional.ofNullable(title).orElse(this.title);
this.discountType = Optional.ofNullable(discountType).orElse(this.discountType);
this.discountValue = Optional.ofNullable(discountValue).orElse(this.discountValue);
this.maximumDiscountPrice = Optional.ofNullable(maximumDiscountPrice).orElse(this.maximumDiscountPrice);
this.customerType = Optional.ofNullable(customerType).orElse(this.customerType);
this.couponRoomType = Optional.ofNullable(couponRoomType).orElse(this.couponRoomType);
this.minimumReservationPrice = Optional.ofNullable(minimumReservationPrice).orElse(this.minimumReservationPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,17 @@ public void register(Long memberId, CouponRegisterRequest couponRegisterRequest)
rooms = Collections.emptyList();
}

DiscountType discountType = ValuedEnum.of(DiscountType.class, couponRegisterRequest.discountType());
Integer discountValue = switch (discountType) {
case FIXED_RATE -> couponRegisterRequest.discountFlatRate();
case FIXED_PRICE -> couponRegisterRequest.discountFlatValue();
};

Coupon savedCoupon = couponRepository.save(Coupon.from(
couponRegisterRequest.title(),
ValuedEnum.of(DiscountType.class, couponRegisterRequest.discountType()),
couponRegisterRequest.discountValue(),
discountType,
discountValue,
couponRegisterRequest.maximumDiscountPrice(),
ValuedEnum.of(CustomerType.class, couponRegisterRequest.customerType()),
ValuedEnum.of(CouponRoomType.class, couponRegisterRequest.couponRoomType()),
couponRegisterRequest.minimumReservationPrice(),
Expand Down Expand Up @@ -134,9 +141,19 @@ public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest
} else {
rooms = Collections.emptyList();
}

DiscountType discountType = Optional.ofNullable(couponUpdateRequest.discountType())
.map(str -> ValuedEnum.of(DiscountType.class, str))
.orElse(null);

Integer discountValue = null;
if (discountType != null) {
discountValue = switch (discountType) {
case FIXED_RATE -> couponUpdateRequest.discountFlatRate();
case FIXED_PRICE -> couponUpdateRequest.discountFlatValue();
};
}

CustomerType customerType = Optional.ofNullable(couponUpdateRequest.customerType())
.map(str -> ValuedEnum.of(CustomerType.class, str))
.orElse(null);
Expand All @@ -149,7 +166,8 @@ public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest

storedCoupon.updateCoupon(
discountType,
couponUpdateRequest.discountValue(),
discountValue,
couponUpdateRequest.maximumDiscountPrice(),
customerType,
couponRoomType,
couponUpdateRequest.minimumReservationPrice(),
Expand Down
Loading