Skip to content

Commit

Permalink
Merge pull request #181 from CoolPeace-yanolza/develop
Browse files Browse the repository at this point in the history
01.23 23시 배포
  • Loading branch information
tkddn204 authored Jan 23, 2024
2 parents ee81c59 + b5335f7 commit ba7441b
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.coolpeace.domain.coupon.dto.request;

import com.coolpeace.domain.coupon.entity.type.CouponRoomType;
import com.coolpeace.domain.coupon.entity.type.CouponUseDaysType;
import com.coolpeace.domain.coupon.entity.type.CustomerType;
import com.coolpeace.domain.coupon.entity.type.DiscountType;
Expand Down Expand Up @@ -30,9 +29,8 @@ public record CouponRegisterRequest(
Integer maximumDiscountPrice,

// 객실 유형
@NotNull(message = "객실의 유형을 선택해야 합니다.")
@ValidEnum(enumClass = CouponRoomType.class, message = "올바르지 않은 객실의 유형입니다.")
String couponRoomType,
List<String> couponRoomTypes,
Boolean couponRoomStayMore,

// 숙소 ID
@NotNull(message = "숙박업체의 ID를 입력해야 합니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.coolpeace.domain.coupon.dto.request;

import com.coolpeace.domain.coupon.entity.type.CouponRoomType;
import com.coolpeace.domain.coupon.entity.type.CouponUseDaysType;
import com.coolpeace.domain.coupon.entity.type.CustomerType;
import com.coolpeace.domain.coupon.entity.type.DiscountType;
Expand All @@ -22,8 +21,8 @@ public record CouponUpdateRequest(
Integer discountFlatValue,
Integer discountFlatRate,
Integer maximumDiscountPrice,
@ValidEnum(enumClass = CouponRoomType.class, message = "올바르지 않은 객실의 유형입니다.", required = false)
String couponRoomType,
List<String> couponRoomTypes,
Boolean couponRoomStayMore,
Boolean registerAllRoom,
List<String> registerRooms,
Integer minimumReservationPrice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public void validate(Object target, Errors errors) {

validateRegisterRooms(errors, request.registerAllRoom());
validateDiscountValues(errors, request.discountType(), request.discountFlatValue(), request.discountFlatRate());
validateCouponRoomTypes(errors, request.couponRoomTypes());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.coolpeace.domain.coupon.dto.request.validator;

import com.coolpeace.domain.coupon.entity.type.CouponRoomType;
import com.coolpeace.domain.coupon.entity.type.DiscountType;
import com.coolpeace.global.common.ValuedEnum;
import io.jsonwebtoken.lang.Collections;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import java.util.List;

public abstract class CouponRequestValidator implements Validator {

// 방 등록 유효성 검사
Expand Down Expand Up @@ -32,4 +37,21 @@ protected static void validateDiscountValues(Errors errors,
}
}
}

// 객실 유형 유효성 검사
protected static void validateCouponRoomTypes(Errors errors, List<String> couponRoomTypes) {
if (Collections.isEmpty(couponRoomTypes)) {
ValidationUtils.rejectIfEmpty(errors,
"couponRoomTypes",
"couponRoomTypes.empty",
"객실 유형을 선택해주세요.");
}
for (String couponRoomTypesStr : couponRoomTypes) {
try {
ValuedEnum.of(CouponRoomType.class, couponRoomTypesStr);
} catch (IllegalArgumentException e) {
errors.reject("couponRoomTypes.invalid", "올바르지 않은 객실 유형입니다.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public void validate(Object target, Errors errors) {

validateRegisterRooms(errors, request.registerAllRoom());
validateDiscountValues(errors, request.discountType(), request.discountFlatValue(), request.discountFlatRate());
validateCouponRoomTypes(errors, request.couponRoomTypes());
}
}
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.CouponRoomType;
import com.coolpeace.domain.coupon.entity.type.DiscountType;

import java.time.LocalDate;
Expand All @@ -18,7 +19,8 @@ public record CouponResponse(
Integer discountFlatRate,
Integer maximumDiscountPrice,
String customerType,
String couponRoomType,
List<String> couponRoomTypes,
Boolean couponRoomStayMore,
Integer minimumReservationPrice,
String couponUseConditionDays,
LocalDate exposureStartDate,
Expand All @@ -42,7 +44,8 @@ public static CouponResponse from(Coupon coupon) {
coupon.getDiscountType().equals(DiscountType.FIXED_RATE) ? coupon.getDiscountValue() : null,
coupon.getMaximumDiscountPrice(),
coupon.getCustomerType().getValue(),
coupon.getCouponRoomType().getValue(),
coupon.getCouponRoomTypeStringsExcludingTwoNight(),
coupon.getCouponRoomStayType() != null && coupon.getCouponRoomStayType().equals(CouponRoomType.TWO_NIGHT),
coupon.getMinimumReservationPrice(),
coupon.getCouponUseDays().getValue(),
coupon.getExposureStartDate(),
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/coolpeace/domain/coupon/entity/Coupon.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Stream;

@Getter
@Entity
Expand Down Expand Up @@ -45,9 +46,12 @@ public class Coupon extends BaseTimeEntity {
@Column(nullable = false)
private CustomerType customerType = CustomerType.ALL_CLIENT;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private CouponRoomType couponRoomType;

@Enumerated(EnumType.STRING)
private CouponRoomType couponRoomStayType;

private Integer minimumReservationPrice = 0;

@Convert(converter = CouponUseConditionDaysTypeConverter.class)
Expand Down Expand Up @@ -88,6 +92,7 @@ public Coupon(String title,
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
CouponRoomType couponRoomStayType,
Integer minimumReservationPrice,
CouponUseDaysType couponUseDays,
LocalDate exposureStartDate,
Expand All @@ -101,6 +106,7 @@ public Coupon(String title,
this.maximumDiscountPrice = maximumDiscountPrice;
this.customerType = customerType;
this.couponRoomType = couponRoomType;
this.couponRoomStayType = couponRoomStayType;
this.minimumReservationPrice = minimumReservationPrice;
this.couponUseDays = couponUseDays;
this.exposureStartDate = exposureStartDate;
Expand All @@ -118,6 +124,7 @@ public static Coupon from(
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
CouponRoomType couponRoomStayType,
Integer minimumReservationPrice,
CouponUseDaysType couponUseDays,
LocalDate exposureStartDate,
Expand All @@ -133,6 +140,7 @@ public static Coupon from(
maximumDiscountPrice,
customerType,
couponRoomType,
couponRoomStayType,
minimumReservationPrice,
couponUseDays,
exposureStartDate,
Expand All @@ -143,6 +151,13 @@ public static Coupon from(
);
}

public List<String> getCouponRoomTypeStringsExcludingTwoNight() {
return Stream.of(this.getCouponRoomType(), this.getCouponRoomStayType())
.filter(Objects::nonNull)
.map(roomType -> (roomType == CouponRoomType.TWO_NIGHT) ? CouponRoomType.LODGE : roomType)
.map(CouponRoomType::getValue).toList();
}

public String getCouponTitle() {
return switch (this.discountType) {
case FIXED_PRICE -> String.format("%s %d원 할인", customerType.getValue(), discountValue);
Expand Down Expand Up @@ -181,6 +196,7 @@ public void updateCoupon(
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
CouponRoomType couponRoomStayType,
Integer minimumReservationPrice,
CouponUseDaysType couponUseDays,
List<Room> rooms,
Expand All @@ -192,7 +208,8 @@ public void updateCoupon(
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.couponRoomType = couponRoomType;
this.couponRoomStayType = couponRoomStayType;
this.minimumReservationPrice = Optional.ofNullable(minimumReservationPrice).orElse(this.minimumReservationPrice);
this.couponUseDays = Optional.ofNullable(couponUseDays).orElse(this.couponUseDays);
if (rooms != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
import com.coolpeace.domain.coupon.dto.response.CouponCategoryResponse;
import com.coolpeace.domain.coupon.dto.response.CouponResponse;
import com.coolpeace.domain.coupon.entity.Coupon;
import com.coolpeace.domain.coupon.entity.type.CouponIssuerType;
import com.coolpeace.domain.coupon.entity.type.CouponRoomType;
import com.coolpeace.domain.coupon.entity.type.CouponStatusType;
import com.coolpeace.domain.coupon.entity.type.CouponUseDaysType;
import com.coolpeace.domain.coupon.entity.type.CustomerType;
import com.coolpeace.domain.coupon.entity.type.DiscountType;
import com.coolpeace.domain.coupon.entity.type.*;
import com.coolpeace.domain.coupon.exception.CouponAccessDeniedException;
import com.coolpeace.domain.coupon.exception.CouponNotFoundException;
import com.coolpeace.domain.coupon.exception.CouponUpdateLimitExposureStateException;
Expand All @@ -29,11 +24,6 @@
import com.coolpeace.domain.room.exception.RoomNotFoundException;
import com.coolpeace.domain.room.repository.RoomRepository;
import com.coolpeace.global.common.ValuedEnum;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
Expand All @@ -44,6 +34,12 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class CouponService {
Expand Down Expand Up @@ -90,7 +86,7 @@ public List<CouponResponse> getRecentHistory(Long memberId) {
}

@Transactional
@CacheEvict(value = "dailyReport", key = "#couponRegisterRequest.accommodationId()",cacheManager = "contentCacheManager")
@CacheEvict(value = "dailyReport", key = "#couponRegisterRequest.accommodationId()", cacheManager = "contentCacheManager")
public void register(Long memberId, CouponRegisterRequest couponRegisterRequest) {
Member storedMember = memberRepository.findById(memberId).orElseThrow(MemberNotFoundException::new);
Accommodation accommodation = accommodationRepository.findById(couponRegisterRequest.accommodationId())
Expand All @@ -115,13 +111,19 @@ public void register(Long memberId, CouponRegisterRequest couponRegisterRequest)
case FIXED_PRICE -> couponRegisterRequest.discountFlatValue();
};

CouponRoomType couponRoomTypeValue = getCouponRoomType(
couponRegisterRequest.couponRoomTypes(), CouponRoomType.RENTAL);
CouponRoomType couponRoomStayTypeValue = getCouponRoomStayTypeValue(couponRegisterRequest.couponRoomTypes(),
couponRoomTypeValue, couponRegisterRequest.couponRoomStayMore());

Coupon savedCoupon = couponRepository.save(Coupon.from(
couponRegisterRequest.title(),
discountType,
discountValue,
couponRegisterRequest.maximumDiscountPrice(),
ValuedEnum.of(CustomerType.class, couponRegisterRequest.customerType()),
ValuedEnum.of(CouponRoomType.class, couponRegisterRequest.couponRoomType()),
couponRoomTypeValue,
couponRoomStayTypeValue,
couponRegisterRequest.minimumReservationPrice(),
ValuedEnum.of(CouponUseDaysType.class, couponRegisterRequest.couponUseConditionDays()),
couponRegisterRequest.exposureStartDate(),
Expand Down Expand Up @@ -163,9 +165,12 @@ public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest
CustomerType customerType = Optional.ofNullable(couponUpdateRequest.customerType())
.map(str -> ValuedEnum.of(CustomerType.class, str))
.orElse(null);
CouponRoomType couponRoomType = Optional.ofNullable(couponUpdateRequest.couponRoomType())
.map(str -> ValuedEnum.of(CouponRoomType.class, str))
.orElse(null);

CouponRoomType couponRoomTypeValue = getCouponRoomType(
couponUpdateRequest.couponRoomTypes(), CouponRoomType.RENTAL);
CouponRoomType couponRoomStayTypeValue = getCouponRoomStayTypeValue(couponUpdateRequest.couponRoomTypes(),
couponRoomTypeValue, couponUpdateRequest.couponRoomStayMore());

CouponUseDaysType couponUseDays = Optional.ofNullable(couponUpdateRequest.couponUseConditionDays())
.map(str -> ValuedEnum.of(CouponUseDaysType.class, str))
.orElse(null);
Expand All @@ -175,7 +180,8 @@ public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest
discountValue,
couponUpdateRequest.maximumDiscountPrice(),
customerType,
couponRoomType,
couponRoomTypeValue,
couponRoomStayTypeValue,
couponUpdateRequest.minimumReservationPrice(),
couponUseDays,
rooms,
Expand All @@ -184,6 +190,22 @@ public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest
);
}

private CouponRoomType getCouponRoomStayTypeValue(List<String> couponRoomTypeStrings,
CouponRoomType couponRoomTypeValue,
boolean CouponRoomStayMore) {
if (CouponRoomStayMore && couponRoomTypeValue != null && couponRoomTypeValue.equals(CouponRoomType.LODGE)) {
return CouponRoomType.TWO_NIGHT;
}
return getCouponRoomType(couponRoomTypeStrings, CouponRoomType.LODGE);
}

private CouponRoomType getCouponRoomType(List<String> request, CouponRoomType couponRoomType) {
return request.stream()
.map(str -> ValuedEnum.of(CouponRoomType.class, str))
.filter(roomType -> roomType.equals(couponRoomType))
.findFirst().orElse(null);
}

@Transactional
public void exposeCoupon(Long memberId, String couponNumber, CouponExposeRequest couponExposeRequest) {
validateMemberHasCoupon(memberId, couponNumber);
Expand Down
Loading

0 comments on commit ba7441b

Please sign in to comment.