Skip to content

Commit

Permalink
Merge pull request #25 from CoolPeace-yanolza/feature/coupon/query-se…
Browse files Browse the repository at this point in the history
…rvice

쿠폰 레포,서비스 추가  및 일간 리포트 구현
  • Loading branch information
KwonJuHwan authored Jan 4, 2024
2 parents 54a42ab + e026ec2 commit 88affd0
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 3 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ dependencies {
runtimeOnly "io.jsonwebtoken:jjwt-jackson:${JJWT_VERSION}"
runtimeOnly "io.jsonwebtoken:jjwt-impl:${JJWT_VERSION}"

// queryDsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

testImplementation "org.junit.jupiter:junit-jupiter:5.8.1"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}


tasks.named('bootBuildImage') {
builder = 'paketobuildpacks/builder-jammy-base:latest'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.coolpeace.domain.coupon.dto.request;


public record CouponDailyRequest(
Long memberId,
Long accommodationId
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.coolpeace.domain.coupon.dto.response;

import com.coolpeace.domain.coupon.entity.CouponDailyCondition;
import java.util.Collections;
import java.util.List;
import lombok.Builder;

@Builder
public record CouponDailyResponse(
CouponDailyCondition condition,
List<String> couponTitles
) {
public static CouponDailyResponse from(CouponDailyCondition condition,List<String> couponTitles) {
return new CouponDailyResponse(condition, couponTitles);
}
public static CouponDailyResponse from(CouponDailyCondition condition) {
return new CouponDailyResponse(condition, Collections.emptyList());
}
}
9 changes: 6 additions & 3 deletions src/main/java/com/coolpeace/domain/coupon/entity/Coupon.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coolpeace.domain.coupon.entity;

import com.coolpeace.domain.accommodation.entity.Accommodation;
import com.coolpeace.domain.member.entity.Member;
import com.coolpeace.domain.room.entity.Room;
import com.coolpeace.global.common.BaseTimeEntity;
Expand All @@ -25,6 +26,8 @@ public class Coupon extends BaseTimeEntity {

private String title;

private CouponStatus couponStatus;

private DiscountType discountType;

private int maxDiscount;
Expand All @@ -45,9 +48,9 @@ public class Coupon extends BaseTimeEntity {

private LocalDateTime expiration;

private Boolean isExposure;

private Boolean isDeleted;
@ManyToOne
@JoinColumn(name = "accommodation_id")
private Accommodation accommodation;

@ManyToOne
@JoinColumn(name = "room_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.coolpeace.domain.coupon.entity;

public enum CouponDailyCondition {
NO_REGISTER("등록된 쿠폰이 없음"),
NO_EXPOSURE("노출중인 쿠폰이 없음"),
EXPIRATION_3DAYS("곧 만료되는 쿠폰이 있음"),
NO_CONDITION("아무 조건에 해당하지 않음");

private final String value;

CouponDailyCondition(String value) {
this.value = value;
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/coolpeace/domain/coupon/entity/CouponStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.coolpeace.domain.coupon.entity;

public enum CouponStatus {
EXPOSURE_ON("노출 ON"),
EXPOSURE_OFF("노출 OFF"),
EXPOSURE_WAIT("노출 대기중"),
EXPOSURE_END("노출 종료"),
DELETED("논리 삭제");

private final String value;

CouponStatus(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.coolpeace.domain.coupon.repository;

import com.coolpeace.domain.accommodation.entity.Accommodation;
import com.coolpeace.domain.coupon.entity.Coupon;
import com.coolpeace.domain.member.entity.Member;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CouponRepository extends JpaRepository<Coupon,Long>,CouponRepositoryCustom {

List<Coupon> findAllByMemberAndAccommodation(Member member, Accommodation accommodation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.coolpeace.domain.coupon.repository;


import com.coolpeace.domain.coupon.entity.Coupon;
import java.util.List;

public interface CouponRepositoryCustom {

List<Coupon> exposureCoupons(Long memberId, Long accommodationId);

Boolean noRegister(Long memberId,Long accommodationId);

Boolean noExposure(Long memberId,Long accommodationId);

List<Coupon> expiration3days(Long memberId,Long accommodationId);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.coolpeace.domain.coupon.repository;

import static com.coolpeace.domain.coupon.entity.QCoupon.coupon;

import com.coolpeace.domain.accommodation.entity.Accommodation;
import com.coolpeace.domain.coupon.entity.Coupon;
import com.coolpeace.domain.coupon.entity.CouponStatus;
import com.coolpeace.domain.member.entity.Member;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import java.time.LocalDateTime;
import java.util.List;

public class CouponRepositoryImpl implements CouponRepositoryCustom{

private final JPAQueryFactory jpaQueryFactory;

public CouponRepositoryImpl(EntityManager entityManager) {
this.jpaQueryFactory = new JPAQueryFactory(entityManager);
}


@Override
public List<Coupon> exposureCoupons(Long memberId, Long accommodationId) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId))
.and(coupon.couponStatus.eq(CouponStatus.EXPOSURE_ON)))
.fetch();
}

@Override
public Boolean noRegister(Long memberId, Long accommodationId) {

return jpaQueryFactory.selectFrom(coupon)
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId))
.and(coupon.couponStatus.ne(CouponStatus.DELETED)))
.fetch().isEmpty();
}

@Override
public Boolean noExposure(Long memberId, Long accommodationId) {

return jpaQueryFactory.selectFrom(coupon)
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId))
.and(coupon.couponStatus.eq(CouponStatus.EXPOSURE_ON)))
.fetch().isEmpty();
}


@Override
public List<Coupon> expiration3days(Long memberId, Long accommodationId) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId))
.and(coupon.couponStatus.eq(CouponStatus.EXPOSURE_ON))
.and(coupon.endDate.before(LocalDateTime.now().plusDays(3))))
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.coolpeace.domain.coupon.service;

import com.coolpeace.domain.coupon.dto.request.CouponDailyRequest;
import com.coolpeace.domain.coupon.dto.response.CouponDailyResponse;
import com.coolpeace.domain.coupon.entity.Coupon;
import com.coolpeace.domain.coupon.entity.CouponDailyCondition;
import com.coolpeace.domain.coupon.repository.CouponRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class CouponQueryService {

private final CouponRepository couponRepository;

/* 메소드 복잡도를 낮추는 방법 고안 중 */
public CouponDailyResponse dailyReport(CouponDailyRequest request) {
Long memberId = request.memberId();
Long accommodationId = request.accommodationId();

if (couponRepository.noRegister(memberId, accommodationId)) {
return CouponDailyResponse.from(CouponDailyCondition.NO_REGISTER);
}
if (couponRepository.noExposure(memberId, accommodationId)) {
return CouponDailyResponse.from(CouponDailyCondition.NO_EXPOSURE);
}

List<Coupon> expiration3daysCoupons = couponRepository.expiration3days(memberId, accommodationId);
if (!expiration3daysCoupons.isEmpty()) {
return CouponDailyResponse.from(CouponDailyCondition.EXPIRATION_3DAYS,
expiration3daysCoupons.stream().map(Coupon::getTitle).toList());
}

List<Coupon> exposureCoupons = couponRepository.exposureCoupons(memberId, accommodationId);
return CouponDailyResponse.from(CouponDailyCondition.NO_CONDITION,
exposureCoupons.stream().map(Coupon::getTitle).toList());
}
}

0 comments on commit 88affd0

Please sign in to comment.