-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from Team-BC-1/feat/admin-add-coupon
어드민 쿠폰 생성 API
- Loading branch information
Showing
14 changed files
with
387 additions
and
6 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
29 changes: 29 additions & 0 deletions
29
src/main/java/bc1/gream/domain/admin/dto/request/AdminCreateCouponRequestDto.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,29 @@ | ||
package bc1.gream.domain.admin.dto.request; | ||
|
||
import bc1.gream.domain.coupon.entity.DiscountType; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
import jakarta.validation.constraints.Positive; | ||
import lombok.Builder; | ||
|
||
@JsonIgnoreProperties | ||
@Builder | ||
public record AdminCreateCouponRequestDto( | ||
@NotBlank(message = "null 혹은 공백 입력은 불가합니다.") | ||
@Pattern( | ||
regexp = "^[a-zA-Z0-9가-힣]{4,30}$", | ||
message = "쿠폰이름은 영문자, 한글 및 숫자, 4이상 30이하 길이로 가능합니다." | ||
) | ||
String name, | ||
@NotNull(message = "null 입력은 불가합니다.") | ||
DiscountType discountType, | ||
@NotNull(message = "null 입력은 불가합니다.") | ||
@Positive(message = "음수 혹은 0 입력은 불가합니다.") | ||
Long discount, | ||
@NotBlank(message = "null 입력은 불가합니다.") | ||
String userLoginId | ||
) { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/bc1/gream/domain/admin/dto/response/AdminCreateCouponResponseDto.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,9 @@ | ||
package bc1.gream.domain.admin.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties | ||
public record AdminCreateCouponResponseDto( | ||
) { | ||
|
||
} |
15 changes: 14 additions & 1 deletion
15
src/main/java/bc1/gream/domain/coupon/entity/CouponStatus.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,7 +1,20 @@ | ||
package bc1.gream.domain.coupon.entity; | ||
|
||
import bc1.gream.global.common.ResultCase; | ||
import bc1.gream.global.exception.GlobalException; | ||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import java.util.stream.Stream; | ||
|
||
public enum CouponStatus { | ||
AVAILABLE, | ||
IN_USE, | ||
ALREADY_USED | ||
ALREADY_USED; | ||
|
||
@JsonCreator | ||
public static CouponStatus deserializeRequest(String couponStatusRequest) { | ||
return Stream.of(CouponStatus.values()) | ||
.filter(couponStatus -> couponStatus.toString().equals(couponStatusRequest.toUpperCase())) | ||
.findAny() | ||
.orElseThrow(() -> new GlobalException(ResultCase.INVALID_INPUT)); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/bc1/gream/domain/coupon/provider/CouponProvider.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,27 @@ | ||
package bc1.gream.domain.coupon.provider; | ||
|
||
import bc1.gream.domain.admin.dto.request.AdminCreateCouponRequestDto; | ||
import bc1.gream.domain.coupon.entity.Coupon; | ||
import bc1.gream.domain.coupon.service.command.CouponCommandService; | ||
import bc1.gream.domain.user.entity.User; | ||
import bc1.gream.domain.user.repository.UserRepository; | ||
import bc1.gream.global.common.ResultCase; | ||
import bc1.gream.global.exception.GlobalException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CouponProvider { | ||
|
||
private final UserRepository userRepository; | ||
private final CouponCommandService couponCommandService; | ||
|
||
@Transactional | ||
public Coupon createCoupon(AdminCreateCouponRequestDto adminCreateCouponRequestDto) { | ||
User user = userRepository.findByLoginId(adminCreateCouponRequestDto.userLoginId()) | ||
.orElseThrow(() -> new GlobalException(ResultCase.USER_NOT_FOUND)); | ||
return couponCommandService.createCoupon(user, adminCreateCouponRequestDto); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/bc1/gream/domain/coupon/service/command/CouponCommandService.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,15 +1,31 @@ | ||
package bc1.gream.domain.coupon.service.command; | ||
|
||
import bc1.gream.domain.admin.dto.request.AdminCreateCouponRequestDto; | ||
import bc1.gream.domain.coupon.entity.Coupon; | ||
import bc1.gream.domain.coupon.entity.CouponStatus; | ||
import bc1.gream.domain.coupon.repository.CouponRepository; | ||
import bc1.gream.domain.user.entity.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CouponCommandService { | ||
|
||
private final CouponRepository couponRepository; | ||
|
||
public void changeCouponStatus(Coupon coupon, CouponStatus couponStatus) { | ||
coupon.changeStatus(couponStatus); | ||
} | ||
|
||
public Coupon createCoupon(User user, AdminCreateCouponRequestDto adminCreateCouponRequestDto) { | ||
Coupon coupon = Coupon.builder() | ||
.name(adminCreateCouponRequestDto.name()) | ||
.discountType(adminCreateCouponRequestDto.discountType()) | ||
.discount(adminCreateCouponRequestDto.discount()) | ||
.status(CouponStatus.AVAILABLE) | ||
.user(user) | ||
.build(); | ||
return couponRepository.save(coupon); | ||
} | ||
} |
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
Oops, something went wrong.