Skip to content

Commit

Permalink
refactor : 쿠폰 발행 기간 하루로 변경 및 쿠폰 정보 오픈 날짜 컬럼 추가 (#136)
Browse files Browse the repository at this point in the history
* style : Schedule 어노테이션 위치 변경

* refactor: 쿠폰 발행 기간 하루로 통일 및 쿠폰 정보 오픈 날짜 추가

* refactor: Sub module Update
  • Loading branch information
hongdosan authored Nov 23, 2023
1 parent f1e51bc commit 12275e0
Show file tree
Hide file tree
Showing 21 changed files with 270 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static Coupon toEntity(Long adminId, CreateCouponRequest coupon) {
.point(coupon.point())
.stock(coupon.stock())
.startAt(coupon.startAt())
.endAt(coupon.endAt())
.openAt(coupon.openAt())
.adminId(adminId)
.build();
}
Expand All @@ -35,7 +35,7 @@ public static CouponResponse toDto(Coupon coupon) {
.stock(coupon.getStock())
.type(coupon.getType())
.startAt(coupon.getStartAt())
.endAt(coupon.getEndAt())
.openAt(coupon.getOpenAt())
.build();
}
}
26 changes: 16 additions & 10 deletions src/main/java/com/moabam/api/application/coupon/CouponService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.moabam.api.application.coupon;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -35,7 +35,7 @@ public class CouponService {
public void create(AuthMember admin, CreateCouponRequest request) {
validateAdminRole(admin);
validateConflictName(request.name());
validatePeriod(request.startAt(), request.endAt());
validatePeriod(request.startAt(), request.openAt());

Coupon coupon = CouponMapper.toEntity(admin.id(), request);
couponRepository.save(coupon);
Expand All @@ -57,7 +57,7 @@ public CouponResponse getById(Long couponId) {
}

public List<CouponResponse> getAllByStatus(CouponStatusRequest request) {
LocalDateTime now = clockHolder.times();
LocalDate now = LocalDate.from(clockHolder.times());
List<Coupon> coupons = couponSearchRepository.findAllByStatus(now, request);

return coupons.stream()
Expand All @@ -66,20 +66,26 @@ public List<CouponResponse> getAllByStatus(CouponStatusRequest request) {
}

public Coupon validatePeriod(String couponName) {
LocalDateTime now = clockHolder.times();
LocalDate now = LocalDate.from(clockHolder.times());
Coupon coupon = couponRepository.findByName(couponName)
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_COUPON));

if (!now.isBefore(coupon.getStartAt()) && !now.isAfter(coupon.getEndAt())) {
return coupon;
if (!now.equals(coupon.getStartAt())) {
throw new BadRequestException(ErrorMessage.INVALID_COUPON_PERIOD);
}

throw new BadRequestException(ErrorMessage.INVALID_COUPON_PERIOD_END);
return coupon;
}

private void validatePeriod(LocalDateTime startAt, LocalDateTime endAt) {
if (startAt.isAfter(endAt)) {
throw new BadRequestException(ErrorMessage.INVALID_COUPON_PERIOD);
private void validatePeriod(LocalDate startAt, LocalDate openAt) {
LocalDate now = LocalDate.from(clockHolder.times());

if (!now.isBefore(startAt)) {
throw new BadRequestException(ErrorMessage.INVALID_COUPON_START_AT_PERIOD);
}

if (!openAt.isBefore(startAt)) {
throw new BadRequestException(ErrorMessage.INVALID_COUPON_OPEN_AT_PERIOD);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/moabam/api/domain/coupon/Coupon.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static com.moabam.global.error.model.ErrorMessage.*;
import static java.util.Objects.*;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.Optional;

import org.hibernate.annotations.ColumnDefault;
Expand Down Expand Up @@ -56,25 +56,25 @@ public class Coupon extends BaseTimeEntity {
private int stock;

@Column(name = "start_at", nullable = false)
private LocalDateTime startAt;
private LocalDate startAt;

@Column(name = "end_at", nullable = false)
private LocalDateTime endAt;
@Column(name = "open_at", nullable = false)
private LocalDate openAt;

// TODO : 관리자 테이블 생기면 관리자 테이블이랑 다대일 관계 맺을 예정
@Column(name = "admin_id", updatable = false, nullable = false)
private Long adminId;

@Builder
private Coupon(String name, int point, String description, CouponType type, int stock, LocalDateTime startAt,
LocalDateTime endAt, Long adminId) {
private Coupon(String name, String description, int point, int stock, CouponType type, LocalDate startAt,
LocalDate openAt, Long adminId) {
this.name = requireNonNull(name);
this.point = validatePoint(point);
this.description = Optional.ofNullable(description).orElse(BLANK);
this.type = requireNonNull(type);
this.stock = validateStock(stock);
this.startAt = requireNonNull(startAt);
this.endAt = requireNonNull(endAt);
this.openAt = requireNonNull(openAt);
this.adminId = requireNonNull(adminId);
}

Expand All @@ -96,6 +96,6 @@ private int validateStock(int stock) {

@Override
public String toString() {
return "Coupon{startAt=" + startAt + ", endAt=" + endAt + '}';
return String.format("Coupon{startAt=%s, openAt=%s}", startAt, openAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import static com.moabam.api.domain.coupon.QCoupon.*;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.moabam.api.domain.coupon.Coupon;
import com.moabam.api.dto.coupon.CouponStatusRequest;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand All @@ -21,51 +20,30 @@ public class CouponSearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<Coupon> findAllByStatus(LocalDateTime now, CouponStatusRequest couponStatus) {
public List<Coupon> findAllByStatus(LocalDate now, CouponStatusRequest couponStatus) {
return jpaQueryFactory.selectFrom(coupon)
.where(filterStatus(now, couponStatus))
.orderBy(coupon.startAt.asc())
.fetch();
}

private BooleanExpression filterStatus(LocalDateTime now, CouponStatusRequest couponStatus) {
if (couponStatus.ongoing() && couponStatus.notStarted() && couponStatus.ended()) {
private BooleanExpression filterStatus(LocalDate now, CouponStatusRequest couponStatus) {
// 모든 쿠폰 (금일 발급 가능한 쿠폰 포함)
if (couponStatus.opened() && couponStatus.ended()) {
return null;
}

// 시작 전이거나 진행 중인 쿠폰들을 조회하고 싶은 경우
if (couponStatus.ongoing() && couponStatus.notStarted()) {
return (coupon.startAt.gt(now))
.or(coupon.startAt.loe(now).and(coupon.endAt.goe(now)));
// 쿠폰 정보 오픈 중인 쿠폰들 (금일 발급 가능한 쿠폰 포함)
if (couponStatus.opened()) {
return coupon.openAt.loe(now).and(coupon.startAt.goe(now));
}

// 종료 됐거나 진행 중인 쿠폰들을 조회하고 싶은 경우
if (couponStatus.ongoing() && couponStatus.ended()) {
return (coupon.endAt.lt(now))
.or(coupon.startAt.loe(now).and(coupon.endAt.goe(now)));
}

// 진행 중이 아니고, 시작 전이거나, 종료된 쿠폰들을 조회하고 싶은 경우
if (couponStatus.notStarted() && couponStatus.ended()) {
return coupon.startAt.gt(now)
.or(coupon.endAt.lt(now));
}

// 진행 중인 쿠폰들을 조회하고 싶은 경우
if (couponStatus.ongoing()) {
return coupon.startAt.loe(now)
.and(coupon.endAt.goe(now));
}

// 시작 적인 쿠폰들을 조회하고 싶은 경우
if (couponStatus.notStarted()) {
return coupon.startAt.gt(now);
}

// 종료된 쿠폰들을 조회하고 싶은 경우
// 종료된 쿠폰들
if (couponStatus.ended()) {
return coupon.endAt.lt(now);
return coupon.startAt.lt(now);
}

return Expressions.FALSE;
// 금일 발급 가능한 쿠폰
return coupon.startAt.eq(now);
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/moabam/api/dto/coupon/CouponResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.moabam.api.dto.coupon;

import java.time.LocalDateTime;
import java.time.LocalDate;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.moabam.api.domain.coupon.CouponType;
Expand All @@ -16,10 +16,10 @@ public record CouponResponse(
int point,
int stock,
CouponType type,
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm")
LocalDateTime startAt,
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm")
LocalDateTime endAt
@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate startAt,
@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate openAt
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

@Builder
public record CouponStatusRequest(
boolean ongoing,
boolean notStarted,
boolean opened,
boolean ended
) {

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/moabam/api/dto/coupon/CreateCouponRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.moabam.api.dto.coupon;

import java.time.LocalDateTime;
import java.time.LocalDate;

import org.hibernate.validator.constraints.Length;

Expand All @@ -18,10 +18,10 @@ public record CreateCouponRequest(
@NotBlank(message = "쿠폰 종류를 입력해주세요.") String type,
@Min(value = 1, message = "벌레 수 혹은 할인 금액은 1 이상이어야 합니다.") int point,
@Min(value = 1, message = "쿠폰 재고는 1 이상이어야 합니다.") int stock,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm")
@NotNull(message = "쿠폰 발급 시작 시각을 입력해주세요.") LocalDateTime startAt,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm")
@NotNull(message = "쿠폰 발급 종료 시각을 입력해주세요.") LocalDateTime endAt
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
@NotNull(message = "쿠폰 발급이 가능한 날짜(년, 월, 일)를 입력해주세요.") LocalDate startAt,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
@NotNull(message = "쿠폰 정보창이 열리는 날짜(년, 월, 일)를 입력해주세요.") LocalDate openAt
) {

}
2 changes: 0 additions & 2 deletions src/main/java/com/moabam/global/config/FcmConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
Expand All @@ -19,7 +18,6 @@

@Slf4j
@Configuration
@EnableScheduling
public class FcmConfig {

private static final String FIREBASE_PATH = "config/moabam-firebase.json";
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/moabam/global/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand All @@ -15,6 +16,7 @@
import com.moabam.global.auth.handler.PathResolver;

@Configuration
@EnableScheduling
public class WebConfig implements WebMvcConfigurer {

private static final String ALLOWED_METHOD_NAMES = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,PATCH";
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/moabam/global/error/model/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public enum ErrorMessage {
INVALID_COUPON_POINT("쿠폰의 보너스 포인트는 0 이상이어야 합니다."),
INVALID_COUPON_STOCK("쿠폰의 재고는 0 이상이어야 합니다."),
INVALID_COUPON_STOCK_END("쿠폰 발급 선착순이 마감되었습니다."),
INVALID_COUPON_PERIOD("쿠폰 발급 종료 시각은 시작 시각보다 이후여야 합니다."),
INVALID_COUPON_PERIOD_END("쿠폰 발급 가능 기간이 아닙니다."),
INVALID_COUPON_START_AT_PERIOD("쿠폰 발급 시작 날짜는 현재 날짜보다 이전이거나 같을 수 없습니다."),
INVALID_COUPON_OPEN_AT_PERIOD("쿠폰 정보 오픈 날짜는 시작 날짜보다 이전이여야 합니다."),
INVALID_COUPON_PERIOD("쿠폰 발급 가능 기간이 아닙니다."),
CONFLICT_COUPON_NAME("쿠폰의 이름이 중복되었습니다."),
NOT_FOUND_COUPON_TYPE("존재하지 않는 쿠폰 종류입니다."),
NOT_FOUND_COUPON("존재하지 않는 쿠폰입니다."),
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config
Loading

0 comments on commit 12275e0

Please sign in to comment.