Skip to content

Commit

Permalink
Merge pull request #161 from CoolPeace-yanolza/develop
Browse files Browse the repository at this point in the history
01-22 16시 배포
  • Loading branch information
tkddn204 authored Jan 22, 2024
2 parents b52d71d + 0d960f7 commit ee9df94
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
import com.coolpeace.domain.coupon.dto.request.CouponExposeRequest;
import com.coolpeace.domain.coupon.dto.request.CouponRegisterRequest;
import com.coolpeace.domain.coupon.dto.request.CouponUpdateRequest;
import com.coolpeace.domain.coupon.dto.request.SearchCouponParams;
import com.coolpeace.domain.coupon.dto.response.CouponCategoryResponse;
import com.coolpeace.domain.coupon.dto.response.CouponResponse;
import com.coolpeace.domain.coupon.dto.response.CouponSearchResponse;
import com.coolpeace.domain.coupon.service.CouponService;
import com.coolpeace.global.jwt.security.JwtPrincipal;
import com.coolpeace.global.resolver.AuthJwtPrincipal;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -27,17 +21,12 @@
public class CouponController {
private final CouponService couponService;

@GetMapping("/{accommodation_id}")
public ResponseEntity<CouponSearchResponse> searchCoupons(
@PathVariable("accommodation_id") Long accommodationId,
@Valid SearchCouponParams searchCouponParams,
@PageableDefault Pageable pageable,
@AuthJwtPrincipal JwtPrincipal jwtPrincipal
) {
Page<CouponResponse> couponResponses = couponService.searchCoupons(
Long.valueOf(jwtPrincipal.getMemberId()), accommodationId, searchCouponParams, pageable);
CouponCategoryResponse categoryResponse = couponService.getCouponCategories();
return ResponseEntity.ok(CouponSearchResponse.from(couponResponses, categoryResponse));
@GetMapping("/{coupon_number}")
public ResponseEntity<CouponResponse> getCouponByCouponNumber(
@PathVariable("coupon_number") String couponNumber,
@AuthJwtPrincipal JwtPrincipal jwtPrincipal) {
return ResponseEntity.ok(CouponResponse.from(couponService.getCouponByCouponNumber(
Long.valueOf(jwtPrincipal.getMemberId()), couponNumber)));
}

@GetMapping("/recent")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.coolpeace.domain.coupon.controller;

import com.coolpeace.domain.coupon.dto.request.SearchCouponParams;
import com.coolpeace.domain.coupon.dto.response.CouponCategoryResponse;
import com.coolpeace.domain.coupon.dto.response.CouponResponse;
import com.coolpeace.domain.coupon.dto.response.CouponSearchResponse;
import com.coolpeace.domain.coupon.service.CouponService;
import com.coolpeace.global.jwt.security.JwtPrincipal;
import com.coolpeace.global.resolver.AuthJwtPrincipal;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class CouponSearchController {
private final CouponService couponService;

@GetMapping("/v1/accommodations/{accommodation_id}/coupons")
public ResponseEntity<CouponSearchResponse> searchCoupons(
@PathVariable("accommodation_id") Long accommodationId,
@Valid SearchCouponParams searchCouponParams,
@PageableDefault Pageable pageable,
@AuthJwtPrincipal JwtPrincipal jwtPrincipal
) {
Page<CouponResponse> couponResponses = couponService.searchCoupons(
Long.valueOf(jwtPrincipal.getMemberId()), accommodationId, searchCouponParams, pageable);
CouponCategoryResponse categoryResponse = couponService.getCouponCategories();
return ResponseEntity.ok(CouponSearchResponse.from(couponResponses, categoryResponse));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
import com.coolpeace.global.common.validator.ValidEnum;
Expand Down Expand Up @@ -43,7 +44,8 @@ public record CouponRegisterRequest(

// 쿠폰 사용 조건
Integer minimumReservationPrice,
List<String> couponUseConditionDays,
@ValidEnum(enumClass = CouponUseDaysType.class, message = "올바르지 않은 사용 조건의 날짜 유형입니다.", required = false)
String couponUseConditionDays,

// 노출 날짜
@NotNull(message = "노출 시작 날짜를 입력해야 합니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
import com.coolpeace.global.common.validator.ValidEnum;
Expand All @@ -11,6 +12,7 @@
import java.util.List;

public record CouponUpdateRequest(
String title,
@NotNull(message = "숙박업체의 ID를 입력해야 합니다.")
Long accommodationId,
@ValidEnum(enumClass = CustomerType.class, message = "올바르지 않은 고객의 유형입니다.", required = false)
Expand All @@ -23,7 +25,8 @@ public record CouponUpdateRequest(
Boolean registerAllRoom,
List<String> registerRooms,
Integer minimumReservationPrice,
List<String> couponUseConditionDays,
@ValidEnum(enumClass = CouponUseDaysType.class, message = "올바르지 않은 사용 조건의 날짜 유형입니다.", required = false)
String couponUseConditionDays,
@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate exposureStartDate,
@JsonFormat(pattern = "yyyy-MM-dd")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.coolpeace.domain.accommodation.entity.Accommodation;
import com.coolpeace.domain.coupon.entity.Coupon;
import com.coolpeace.global.common.DayOfWeekUtil;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -18,7 +17,7 @@ public record CouponResponse(
String customerType,
String couponRoomType,
Integer minimumReservationPrice,
List<String> couponUseConditionDays,
String couponUseConditionDays,
LocalDate exposureStartDate,
LocalDate exposureEndDate,
Integer couponExpiration,
Expand All @@ -40,7 +39,7 @@ public static CouponResponse from(Coupon coupon) {
coupon.getCustomerType().getValue(),
coupon.getCouponRoomType().getValue(),
coupon.getMinimumReservationPrice(),
DayOfWeekUtil.fromDayOfWeeks(coupon.getCouponUseConditionDays()),
coupon.getCouponUseDays().getValue(),
coupon.getExposureStartDate(),
coupon.getExposureEndDate(),
coupon.getCouponExpiration(),
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/coolpeace/domain/coupon/entity/Coupon.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;

@Getter
@Entity
Expand Down Expand Up @@ -52,7 +49,10 @@ public class Coupon extends BaseTimeEntity {
private Integer minimumReservationPrice = 0;

@Convert(converter = CouponUseConditionDaysTypeConverter.class)
private List<DayOfWeek> couponUseConditionDays;
private final List<DayOfWeek> couponUseConditionDays = Collections.emptyList();

@Enumerated(EnumType.STRING)
private CouponUseDaysType couponUseDays = CouponUseDaysType.ALL;

@Column(nullable = false)
private LocalDate exposureStartDate;
Expand Down Expand Up @@ -86,7 +86,7 @@ public Coupon(String title,
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
List<DayOfWeek> couponUseConditionDays,
CouponUseDaysType couponUseDays,
LocalDate exposureStartDate,
LocalDate exposureEndDate,
Accommodation accommodation,
Expand All @@ -98,7 +98,7 @@ public Coupon(String title,
this.customerType = customerType;
this.couponRoomType = couponRoomType;
this.minimumReservationPrice = minimumReservationPrice;
this.couponUseConditionDays = couponUseConditionDays;
this.couponUseDays = couponUseDays;
this.exposureStartDate = exposureStartDate;
this.exposureEndDate = exposureEndDate;
this.accommodation = accommodation;
Expand All @@ -114,7 +114,7 @@ public static Coupon from(
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
List<DayOfWeek> couponUseConditionDays,
CouponUseDaysType couponUseDays,
LocalDate exposureStartDate,
LocalDate exposureEndDate,
Accommodation accommodation,
Expand All @@ -128,7 +128,7 @@ public static Coupon from(
customerType,
couponRoomType,
minimumReservationPrice,
couponUseConditionDays,
couponUseDays,
exposureStartDate,
exposureEndDate,
accommodation,
Expand All @@ -146,7 +146,7 @@ public String getCouponTitle() {

public void generateCouponNumber(CouponIssuerType couponIssuerType, Long id) {
this.couponNumber =
couponIssuerType.getValue() + String.format("%06d", Objects.requireNonNull(id));
couponIssuerType.getValue() + String.format("%07d", Objects.requireNonNull(id));
}

public void changeCouponStatus(CouponStatusType couponStatusType) {
Expand Down Expand Up @@ -175,17 +175,18 @@ public void updateCoupon(
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
List<DayOfWeek> couponUseConditionDays,
CouponUseDaysType couponUseDays,
List<Room> rooms,
LocalDate exposureStartDate,
LocalDate exposureEndDate
) {
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.customerType = Optional.ofNullable(customerType).orElse(this.customerType);
this.couponRoomType = Optional.ofNullable(couponRoomType).orElse(this.couponRoomType);
this.minimumReservationPrice = Optional.ofNullable(minimumReservationPrice).orElse(this.minimumReservationPrice);
this.couponUseConditionDays = Optional.ofNullable(couponUseConditionDays).orElse(this.couponUseConditionDays);
this.couponUseDays = Optional.ofNullable(couponUseDays).orElse(this.couponUseDays);
if (rooms != null) {
this.couponRooms = rooms.stream().map(room -> CouponRooms.from(this, room)).toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.coolpeace.domain.coupon.entity.type;

import com.coolpeace.global.common.ValuedEnum;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum CouponUseDaysType implements ValuedEnum {
MONDAY("월"),
TUESDAY("화"),
WEDNESDAY("수"),
THURSDAY("목"),
FRIDAY("금"),
SATURDAY("토"),
SUNDAY("일"),
WEEKDAY("평일"),
WEEKEND("주말"),
ALL("전체");

private final String value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public Page<Coupon> findAllCoupons(Long memberId, Long accommodationId, SearchCo
.or(coupon.couponStatus.eq(CouponStatusType.DELETED));
case All -> coupon.couponStatus.ne(CouponStatusType.DELETED);
});
} else {
searchCouponPredicate.and(coupon.couponStatus.ne(CouponStatusType.DELETED));
}

// 쿠폰 이름
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import com.coolpeace.domain.coupon.dto.response.CouponResponse;
import com.coolpeace.domain.coupon.entity.Coupon;
import com.coolpeace.domain.coupon.entity.type.*;
import com.coolpeace.domain.coupon.exception.*;
import com.coolpeace.domain.coupon.exception.CouponAccessDeniedException;
import com.coolpeace.domain.coupon.exception.CouponNotFoundException;
import com.coolpeace.domain.coupon.exception.CouponUpdateLimitExposureStateException;
import com.coolpeace.domain.coupon.exception.InvalidCouponStateOutsideExposureDateException;
import com.coolpeace.domain.coupon.repository.CouponRepository;
import com.coolpeace.domain.member.entity.Member;
import com.coolpeace.domain.member.exception.MemberNotFoundException;
Expand All @@ -20,7 +23,6 @@
import com.coolpeace.domain.room.exception.RegisterRoomsEmptyException;
import com.coolpeace.domain.room.exception.RoomNotFoundException;
import com.coolpeace.domain.room.repository.RoomRepository;
import com.coolpeace.global.common.DayOfWeekUtil;
import com.coolpeace.global.common.ValuedEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -31,7 +33,6 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -69,6 +70,13 @@ public CouponCategoryResponse getCouponCategories() {
return CouponCategoryResponse.from(all, exposureOn, exposureOff, expired);
}

@Transactional(readOnly = true)
public Coupon getCouponByCouponNumber(Long memberId, String couponNumber) {
validateMemberHasCoupon(memberId, couponNumber);
return couponRepository.findByCouponNumber(couponNumber)
.orElseThrow(CouponNotFoundException::new);
}

@Transactional(readOnly = true)
public List<CouponResponse> getRecentHistory(Long memberId) {
return couponRepository.findRecentCouponByMemberId(memberId)
Expand Down Expand Up @@ -101,8 +109,7 @@ public void register(Long memberId, CouponRegisterRequest couponRegisterRequest)
ValuedEnum.of(CustomerType.class, couponRegisterRequest.customerType()),
ValuedEnum.of(CouponRoomType.class, couponRegisterRequest.couponRoomType()),
couponRegisterRequest.minimumReservationPrice(),
DayOfWeekUtil.fromDayOfWeekStrings(couponRegisterRequest.couponUseConditionDays())
,
ValuedEnum.of(CouponUseDaysType.class, couponRegisterRequest.couponUseConditionDays()),
couponRegisterRequest.exposureStartDate(),
couponRegisterRequest.exposureEndDate(),
accommodation,
Expand Down Expand Up @@ -135,8 +142,8 @@ public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest
CouponRoomType couponRoomType = Optional.ofNullable(couponUpdateRequest.couponRoomType())
.map(str -> ValuedEnum.of(CouponRoomType.class, str))
.orElse(null);
List<DayOfWeek> dayOfWeeks = Optional.ofNullable(couponUpdateRequest.couponUseConditionDays())
.map(DayOfWeekUtil::fromDayOfWeekStrings)
CouponUseDaysType couponUseDays = Optional.ofNullable(couponUpdateRequest.couponUseConditionDays())
.map(str -> ValuedEnum.of(CouponUseDaysType.class, str))
.orElse(null);

storedCoupon.updateCoupon(
Expand All @@ -145,7 +152,7 @@ public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest
customerType,
couponRoomType,
couponUpdateRequest.minimumReservationPrice(),
dayOfWeeks,
couponUseDays,
rooms,
couponUpdateRequest.exposureStartDate(),
couponUpdateRequest.exposureEndDate()
Expand Down
Loading

0 comments on commit ee9df94

Please sign in to comment.