Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ 코드 수정: 직원 쿠폰 등록 시 couponCategory를 enum으로 사용하도록 로직 수정 #198

Merged
merged 6 commits into from
Nov 17, 2024
5 changes: 2 additions & 3 deletions LearnsMate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// Querydsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0'
compileOnly 'jakarta.persistence:jakarta.persistence-api:3.1.0'

// 엑셀 파일 처리를 위한 apache poi
implementation 'org.apache.poi:poi:5.2.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public ResponseEntity<List<CouponFilterResponseVO>> filterCoupons(@RequestBody C

@Operation(summary = "직원 - 쿠폰 등록")
@PostMapping("/admin/register")
public ResponseEntity<CouponRegisterResponseVO> createCoupon(@RequestBody AdminCouponRegisterRequestVO request, Admin admin, CouponCategory couponCategory) {
CouponDTO couponDTO = couponService.adminRegisterCoupon(request, admin, couponCategory);
public ResponseEntity<CouponRegisterResponseVO> createCoupon(@RequestBody AdminCouponRegisterRequestVO request, Admin admin) {
CouponDTO couponDTO = couponService.adminRegisterCoupon(request, admin);
return ResponseEntity.status(HttpStatus.CREATED).body(couponMapper.fromDTOToRegisterResponseVO(couponDTO));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package intbyte4.learnsmate.coupon.domain.vo.request;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.*;

import java.time.LocalDateTime;
Expand All @@ -9,19 +12,12 @@
@NoArgsConstructor
@Getter
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class AdminCouponRegisterRequestVO {
@JsonProperty("coupon_name")
private String couponName;

@JsonProperty("coupon_contents")
private String couponCategoryName;
private String couponContents;

@JsonProperty("coupon_discount_rate")
private Integer couponDiscountRate;

@JsonProperty("coupon_start_date")
private LocalDateTime couponStartDate;

@JsonProperty("coupon_expire_date")
private LocalDateTime couponExpireDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface CouponService {

List<CouponDTO> getCouponsByFilters(CouponFilterRequestVO request);

CouponDTO adminRegisterCoupon(AdminCouponRegisterRequestVO request, Admin admin, CouponCategory couponCategory);
CouponDTO adminRegisterCoupon(AdminCouponRegisterRequestVO request, Admin admin);

CouponDTO editAdminCoupon(CouponDTO couponDTO, Admin admin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import intbyte4.learnsmate.coupon.mapper.CouponMapper;
import intbyte4.learnsmate.coupon.repository.CouponRepository;
import intbyte4.learnsmate.coupon_category.domain.CouponCategory;
import intbyte4.learnsmate.coupon_category.domain.dto.CouponCategoryEnum;
import intbyte4.learnsmate.member.domain.entity.Member;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,6 +22,7 @@

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -65,13 +67,40 @@ public List<CouponDTO> getCouponsByFilters(CouponFilterRequestVO request) {

@Override
@Transactional
public CouponDTO adminRegisterCoupon(AdminCouponRegisterRequestVO request, Admin admin, CouponCategory couponCategory) {
CouponEntity newCoupon = getCoupon(request, admin, couponCategory);
couponRepository.save(newCoupon);
public CouponDTO adminRegisterCoupon(AdminCouponRegisterRequestVO request, Admin admin) {

CouponCategoryEnum categoryEnum = getCouponCategoryEnumByName(request.getCouponCategoryName());

CouponCategory couponCategory = CouponCategory.builder()
.couponCategoryCode(categoryEnum.getCategoryCode())
.couponCategoryName(categoryEnum.getCategoryName())
.build();

CouponEntity newCoupon = CouponEntity.builder()
.couponName(request.getCouponName())
.couponContents(request.getCouponContents())
.couponDiscountRate(request.getCouponDiscountRate())
.couponStartDate(request.getCouponStartDate())
.couponExpireDate(request.getCouponExpireDate())
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.couponFlag(true)
.activeState(true)
.couponCategory(couponCategory)
.admin(admin)
.build();

couponRepository.save(newCoupon);
return couponMapper.toDTO(newCoupon);
}

private CouponCategoryEnum getCouponCategoryEnumByName(String categoryName) {
return Arrays.stream(CouponCategoryEnum.values())
.filter(category -> category.getCategoryName().equals(categoryName))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리 이름이 존재하지 않습니다: " + categoryName));
}

@Override
@Transactional
public CouponDTO editAdminCoupon(CouponDTO couponDTO, Admin admin) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package intbyte4.learnsmate.coupon_category.domain.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;

@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum CouponCategoryEnum {
COMMON(1, "일반"),
ANNIVERSARY(2, "기념일"),
NEW(3, "신규가입"),
FIRSTPURCHASE(4, "첫구매"),
RETURN(5, "복귀"),
COMPLETE(6, "완료"),
EVENT(7, "이벤트");

private final Integer categoryCode;
private final String categoryName;

CouponCategoryEnum(Integer categoryCode, String categoryName) {
this.categoryCode = categoryCode;
this.categoryName = categoryName;
}

public Integer getCategoryCode() {
return categoryCode;
}

public String getCategoryName() {
return categoryName;
}

public static Integer getCodeByName(String categoryName) {
for(CouponCategoryEnum category : CouponCategoryEnum.values()) {
if (category.getCategoryName().equals(categoryName)) {
return category.getCategoryCode();
}
}
throw new IllegalArgumentException("해당 카테고리의 이름이 존재하지 않습니다: " + categoryName);
}
//
// public static String getNameByCode(Integer categoryCode) {
// for (CouponCategoryEnum category : CouponCategoryEnum.values()) {
// if (category.getCategoryCode().equals(categoryCode)) {
// return category.getCategoryName();
// }
// }
// throw new IllegalArgumentException("해당 카테고리 코드가 존재하지 않습니다: " + categoryCode);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum LectureCategoryEnum {
FRONTEND("FRONTEND"),
DEVOPS("DEVOPS"),
DATABASE("DATABASE"),
WEB_DEVELOPMENT("WEB_DEVELOPMENTt"),
WEB_DEVELOPMENT("WEB_DEVELOPMENT"),
MOBILE_APP_DEVELOPMENT("MOBILE_APP_DEVELOPMENT"),
FULL_STACK("FULL_STACK");

Expand Down
Loading