-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
5,109 additions
and
1,016 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/com/moabam/api/domain/repository/CouponSearchRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.moabam.api.domain.repository; | ||
|
||
import static com.moabam.api.domain.entity.QCoupon.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import com.moabam.api.domain.entity.Coupon; | ||
import com.moabam.api.dto.CouponSearchRequest; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class CouponSearchRepository { | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
public Optional<Coupon> findById(Long couponId) { | ||
return Optional.ofNullable( | ||
jpaQueryFactory.selectFrom(coupon) | ||
.where(coupon.id.eq(couponId)) | ||
.fetchOne() | ||
); | ||
} | ||
|
||
public List<Coupon> findAllByStatus(LocalDateTime now, CouponSearchRequest request) { | ||
return jpaQueryFactory.selectFrom(coupon) | ||
.where(filterCouponStatus(now, request)) | ||
.fetch(); | ||
} | ||
|
||
private BooleanExpression filterCouponStatus(LocalDateTime now, CouponSearchRequest request) { | ||
if (request.couponOngoing() && request.couponNotStarted() && request.couponEnded()) { | ||
return null; | ||
} | ||
|
||
// 시작 전이거나 진행 중인 쿠폰들을 조회하고 싶은 경우 | ||
if (request.couponOngoing() && request.couponNotStarted()) { | ||
return (coupon.startAt.gt(now)) | ||
.or(coupon.startAt.loe(now).and(coupon.endAt.goe(now))); | ||
} | ||
|
||
// 종료 됐거나 진행 중인 쿠폰들을 조회하고 싶은 경우 | ||
if (request.couponOngoing() && request.couponEnded()) { | ||
return (coupon.endAt.lt(now)) | ||
.or(coupon.startAt.loe(now).and(coupon.endAt.goe(now))); | ||
} | ||
|
||
// 진행 중이 아니고, 시작 전이거나, 종료된 쿠폰들을 조회하고 싶은 경우 | ||
if (request.couponNotStarted() && request.couponEnded()) { | ||
return coupon.startAt.gt(now) | ||
.or(coupon.endAt.lt(now)); | ||
} | ||
|
||
// 진행 중인 쿠폰들을 조회하고 싶은 경우 | ||
if (request.couponOngoing()) { | ||
return coupon.startAt.loe(now) | ||
.and(coupon.endAt.goe(now)); | ||
} | ||
|
||
// 시작 적인 쿠폰들을 조회하고 싶은 경우 | ||
if (request.couponNotStarted()) { | ||
return coupon.startAt.gt(now); | ||
} | ||
|
||
// 종료된 쿠폰들을 조회하고 싶은 경우 | ||
if (request.couponEnded()) { | ||
return coupon.endAt.lt(now); | ||
} | ||
|
||
return Expressions.FALSE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.moabam.api.dto; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.moabam.api.domain.entity.enums.CouponType; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record CouponResponse( | ||
Long couponId, | ||
String couponAdminName, | ||
String name, | ||
String description, | ||
int point, | ||
int stock, | ||
CouponType couponType, | ||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm") | ||
LocalDateTime startAt, | ||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm") | ||
LocalDateTime endAt | ||
) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.moabam.api.dto; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record CouponSearchRequest( | ||
boolean couponOngoing, | ||
boolean couponNotStarted, | ||
boolean couponEnded | ||
) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 21 additions & 5 deletions
26
src/main/java/com/moabam/api/presentation/CouponController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,51 @@ | ||
package com.moabam.api.presentation; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.moabam.api.application.CouponService; | ||
import com.moabam.api.dto.CouponResponse; | ||
import com.moabam.api.dto.CouponSearchRequest; | ||
import com.moabam.api.dto.CreateCouponRequest; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/admins") | ||
public class CouponController { | ||
|
||
private final CouponService couponService; | ||
|
||
@PostMapping("/coupons") | ||
@PostMapping("/admins/coupons") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public void createCoupon(@RequestBody CreateCouponRequest request) { | ||
public void createCoupon(@Valid @RequestBody CreateCouponRequest request) { | ||
couponService.createCoupon(1L, request); | ||
} | ||
|
||
@DeleteMapping("/coupons/{couponId}") | ||
@DeleteMapping("/admins/coupons/{couponId}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public void deleteCoupon(@PathVariable Long couponId) { | ||
couponService.deleteCoupon(1L, couponId); | ||
} | ||
|
||
@GetMapping("/coupons/{couponId}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public CouponResponse getCouponById(@PathVariable Long couponId) { | ||
return couponService.getCouponById(couponId); | ||
} | ||
|
||
@PostMapping("/coupons/search") | ||
@ResponseStatus(HttpStatus.OK) | ||
public List<CouponResponse> getCoupons(@Valid @RequestBody CouponSearchRequest request) { | ||
return couponService.getCoupons(request); | ||
} | ||
} |
Oops, something went wrong.