| @@ -9,7 +9,7 @@ data class CanCoupon( | |||||||
|     val couponName: String, |     val couponName: String, | ||||||
|     val can: Int, |     val can: Int, | ||||||
|     val couponCount: Int, |     val couponCount: Int, | ||||||
|     val validity: LocalDateTime, |     var validity: LocalDateTime, | ||||||
|     val isActive: Boolean, |     var isActive: Boolean, | ||||||
|     val isMultipleUse: Boolean |     var isMultipleUse: Boolean | ||||||
| ) : BaseEntity() | ) : BaseEntity() | ||||||
|   | |||||||
| @@ -12,6 +12,7 @@ import org.springframework.security.access.prepost.PreAuthorize | |||||||
| import org.springframework.security.core.annotation.AuthenticationPrincipal | import org.springframework.security.core.annotation.AuthenticationPrincipal | ||||||
| import org.springframework.web.bind.annotation.GetMapping | import org.springframework.web.bind.annotation.GetMapping | ||||||
| import org.springframework.web.bind.annotation.PostMapping | import org.springframework.web.bind.annotation.PostMapping | ||||||
|  | import org.springframework.web.bind.annotation.PutMapping | ||||||
| import org.springframework.web.bind.annotation.RequestBody | import org.springframework.web.bind.annotation.RequestBody | ||||||
| import org.springframework.web.bind.annotation.RequestMapping | import org.springframework.web.bind.annotation.RequestMapping | ||||||
| import org.springframework.web.bind.annotation.RequestParam | import org.springframework.web.bind.annotation.RequestParam | ||||||
| @@ -33,6 +34,17 @@ class CanCouponController(private val service: CanCouponService) { | |||||||
|         ApiResponse.ok(service.generateCoupon(request)) |         ApiResponse.ok(service.generateCoupon(request)) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @PutMapping | ||||||
|  |     @PreAuthorize("hasRole('ADMIN')") | ||||||
|  |     fun modifyCoupon( | ||||||
|  |         @RequestBody request: ModifyCanCouponRequest, | ||||||
|  |         @AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member? | ||||||
|  |     ) = run { | ||||||
|  |         if (member == null) throw SodaException("로그인 정보를 확인해주세요.") | ||||||
|  |  | ||||||
|  |         ApiResponse.ok(service.modifyCoupon(request)) | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @GetMapping |     @GetMapping | ||||||
|     @PreAuthorize("hasRole('ADMIN')") |     @PreAuthorize("hasRole('ADMIN')") | ||||||
|     fun getCouponList( |     fun getCouponList( | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ package kr.co.vividnext.sodalive.can.coupon | |||||||
|  |  | ||||||
| import kr.co.vividnext.sodalive.common.SodaException | import kr.co.vividnext.sodalive.common.SodaException | ||||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||||
|  | import java.time.LocalDateTime | ||||||
|  |  | ||||||
| @Service | @Service | ||||||
| class CanCouponIssueService(private val couponNumberRepository: CanCouponNumberRepository) { | class CanCouponIssueService(private val couponNumberRepository: CanCouponNumberRepository) { | ||||||
| @@ -14,6 +15,8 @@ class CanCouponIssueService(private val couponNumberRepository: CanCouponNumberR | |||||||
|                 throw SodaException("해당 쿠폰은 1회만 충전이 가능합니다.") |                 throw SodaException("해당 쿠폰은 1회만 충전이 가능합니다.") | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         validateCoupon(canCouponNumber.canCoupon!!) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun checkCanCouponNumber(couponNumber: String): CanCouponNumber { |     private fun checkCanCouponNumber(couponNumber: String): CanCouponNumber { | ||||||
| @@ -28,4 +31,20 @@ class CanCouponIssueService(private val couponNumberRepository: CanCouponNumberR | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun isMultipleUse(canCouponNumber: CanCouponNumber) = canCouponNumber.canCoupon!!.isMultipleUse |     private fun isMultipleUse(canCouponNumber: CanCouponNumber) = canCouponNumber.canCoupon!!.isMultipleUse | ||||||
|  |  | ||||||
|  |     private fun validateCoupon(canCoupon: CanCoupon) { | ||||||
|  |         if (canCoupon.validity < LocalDateTime.now()) { | ||||||
|  |             throw SodaException("유효기간이 경과된 쿠폰입니다.") | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (!canCoupon.isActive) { | ||||||
|  |             throw SodaException("이용이 불가능한 쿠폰입니다.") | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fun checkAnyChanges(request: ModifyCanCouponRequest) { | ||||||
|  |         if (request.isMultipleUse == null && request.isActive == null && request.validity == null) { | ||||||
|  |             throw SodaException("변경사항이 없습니다.") | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -25,7 +25,10 @@ class CanCouponNumberQueryRepositoryImpl(private val queryFactory: JPAQueryFacto | |||||||
|         return queryFactory |         return queryFactory | ||||||
|             .select(canCouponNumber.id) |             .select(canCouponNumber.id) | ||||||
|             .from(canCouponNumber) |             .from(canCouponNumber) | ||||||
|             .where(canCouponNumber.member.isNotNull) |             .where( | ||||||
|  |                 canCouponNumber.member.isNotNull | ||||||
|  |                     .and(canCouponNumber.id.eq(id)) | ||||||
|  |             ) | ||||||
|             .fetch() |             .fetch() | ||||||
|             .size |             .size | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -10,9 +10,12 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook | |||||||
| import org.springframework.context.ApplicationEventPublisher | import org.springframework.context.ApplicationEventPublisher | ||||||
| import org.springframework.data.repository.findByIdOrNull | import org.springframework.data.repository.findByIdOrNull | ||||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||||
|  | import org.springframework.transaction.annotation.Transactional | ||||||
| import java.io.ByteArrayInputStream | import java.io.ByteArrayInputStream | ||||||
| import java.io.ByteArrayOutputStream | import java.io.ByteArrayOutputStream | ||||||
| import java.io.IOException | import java.io.IOException | ||||||
|  | import java.time.LocalDateTime | ||||||
|  | import java.time.ZoneId | ||||||
| import java.time.format.DateTimeFormatter | import java.time.format.DateTimeFormatter | ||||||
|  |  | ||||||
| @Service | @Service | ||||||
| @@ -33,6 +36,36 @@ class CanCouponService( | |||||||
|         applicationEventPublisher.publishEvent(SqsEvent(type = SqsEventType.GENERATE_COUPON, message = message)) |         applicationEventPublisher.publishEvent(SqsEvent(type = SqsEventType.GENERATE_COUPON, message = message)) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Transactional | ||||||
|  |     fun modifyCoupon(request: ModifyCanCouponRequest) { | ||||||
|  |         issueService.checkAnyChanges(request) | ||||||
|  |  | ||||||
|  |         val canCoupon = repository.findByIdOrNull(id = request.couponId) | ||||||
|  |             ?: throw SodaException("잘못된 쿠폰번호입니다.\n고객센터로 문의해 주시기 바랍니다.") | ||||||
|  |  | ||||||
|  |         if (request.validity != null) { | ||||||
|  |             val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") | ||||||
|  |             val validity = LocalDateTime.parse(request.validity, dateTimeFormatter) | ||||||
|  |                 .atZone(ZoneId.of("Asia/Seoul")) | ||||||
|  |                 .withZoneSameInstant(ZoneId.of("UTC")) | ||||||
|  |                 .toLocalDateTime() | ||||||
|  |  | ||||||
|  |             if (validity <= canCoupon.validity) { | ||||||
|  |                 throw SodaException("유효기간은 기존 유효기간 이후 날짜로 설정하실 수 있습니다.") | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             canCoupon.validity = validity | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (request.isActive != null) { | ||||||
|  |             canCoupon.isActive = request.isActive | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (request.isMultipleUse != null) { | ||||||
|  |             canCoupon.isMultipleUse = request.isMultipleUse | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     fun getCouponList(offset: Long, limit: Long): GetCouponListResponse { |     fun getCouponList(offset: Long, limit: Long): GetCouponListResponse { | ||||||
|         val totalCount = repository.getCouponTotalCount() |         val totalCount = repository.getCouponTotalCount() | ||||||
|  |  | ||||||
|   | |||||||
| @@ -0,0 +1,8 @@ | |||||||
|  | package kr.co.vividnext.sodalive.can.coupon | ||||||
|  |  | ||||||
|  | data class ModifyCanCouponRequest( | ||||||
|  |     val couponId: Long, | ||||||
|  |     val validity: String?, | ||||||
|  |     val isMultipleUse: Boolean?, | ||||||
|  |     val isActive: Boolean? | ||||||
|  | ) | ||||||
		Reference in New Issue
	
	Block a user