관리자 - 충전이벤트
- 시간 계산을 Querydsl 코드에서 수행 - 등록/수정 시 이벤트 진행기간에 시간도 포함하도록 수정
This commit is contained in:
		| @@ -1,22 +1,51 @@ | ||||
| package kr.co.vividnext.sodalive.admin.event.charge | ||||
|  | ||||
| import com.querydsl.core.types.dsl.DateTimePath | ||||
| import com.querydsl.core.types.dsl.Expressions | ||||
| import com.querydsl.core.types.dsl.StringTemplate | ||||
| import com.querydsl.jpa.impl.JPAQueryFactory | ||||
| import kr.co.vividnext.sodalive.admin.event.charge.QChargeEvent.chargeEvent | ||||
| import org.springframework.data.jpa.repository.JpaRepository | ||||
| import org.springframework.stereotype.Repository | ||||
| import java.time.LocalDateTime | ||||
|  | ||||
| @Repository | ||||
| interface AdminChargeEventRepository : JpaRepository<ChargeEvent, Long>, AdminChargeEventQueryRepository | ||||
|  | ||||
| interface AdminChargeEventQueryRepository { | ||||
|     fun getChargeEventList(): List<ChargeEvent> | ||||
|     fun getChargeEventList(): List<GetChargeEventListResponse> | ||||
| } | ||||
|  | ||||
| class AdminChargeEventQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : AdminChargeEventQueryRepository { | ||||
|     override fun getChargeEventList(): List<ChargeEvent> { | ||||
|     override fun getChargeEventList(): List<GetChargeEventListResponse> { | ||||
|         return queryFactory | ||||
|             .selectFrom(chargeEvent) | ||||
|             .select( | ||||
|                 QGetChargeEventListResponse( | ||||
|                     chargeEvent.id, | ||||
|                     chargeEvent.title, | ||||
|                     getFormattedDate(chargeEvent.startDate), | ||||
|                     getFormattedDate(chargeEvent.endDate), | ||||
|                     chargeEvent.availableCount, | ||||
|                     chargeEvent.addPercent.multiply(100).castToNum(Int::class.java), | ||||
|                     chargeEvent.isActive | ||||
|                 ) | ||||
|             ) | ||||
|             .from(chargeEvent) | ||||
|             .orderBy(chargeEvent.createdAt.desc()) | ||||
|             .fetch() | ||||
|     } | ||||
|  | ||||
|     private fun getFormattedDate(dateTimePath: DateTimePath<LocalDateTime>): StringTemplate { | ||||
|         return Expressions.stringTemplate( | ||||
|             "DATE_FORMAT({0}, {1})", | ||||
|             Expressions.dateTimeTemplate( | ||||
|                 LocalDateTime::class.java, | ||||
|                 "CONVERT_TZ({0},{1},{2})", | ||||
|                 dateTimePath, | ||||
|                 "UTC", | ||||
|                 "Asia/Seoul" | ||||
|             ), | ||||
|             "%Y-%m-%d %H:%i" | ||||
|         ) | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,7 +4,7 @@ import kr.co.vividnext.sodalive.common.SodaException | ||||
| import org.springframework.data.repository.findByIdOrNull | ||||
| import org.springframework.stereotype.Service | ||||
| import org.springframework.transaction.annotation.Transactional | ||||
| import java.time.LocalDate | ||||
| import java.time.LocalDateTime | ||||
| import java.time.ZoneId | ||||
| import java.time.format.DateTimeFormatter | ||||
|  | ||||
| @@ -13,13 +13,13 @@ import java.time.format.DateTimeFormatter | ||||
| class AdminChargeEventService(private val repository: AdminChargeEventRepository) { | ||||
|     @Transactional | ||||
|     fun createChargeEvent(request: CreateChargeEventRequest): Long { | ||||
|         val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") | ||||
|         val startDate = LocalDate.parse(request.startDateString, dateTimeFormatter).atTime(0, 0) | ||||
|         val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm") | ||||
|         val startDate = LocalDateTime.parse(request.startDateString, dateTimeFormatter) | ||||
|             .atZone(ZoneId.of("Asia/Seoul")) | ||||
|             .withZoneSameInstant(ZoneId.of("UTC")) | ||||
|             .toLocalDateTime() | ||||
|  | ||||
|         val endDate = LocalDate.parse(request.endDateString, dateTimeFormatter).atTime(23, 59, 59) | ||||
|         val endDate = LocalDateTime.parse(request.endDateString, dateTimeFormatter).withSecond(59) | ||||
|             .atZone(ZoneId.of("Asia/Seoul")) | ||||
|             .withZoneSameInstant(ZoneId.of("UTC")) | ||||
|             .toLocalDateTime() | ||||
| @@ -44,16 +44,16 @@ class AdminChargeEventService(private val repository: AdminChargeEventRepository | ||||
|             chargeEvent.title = request.title | ||||
|         } | ||||
|  | ||||
|         val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") | ||||
|         val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm") | ||||
|         if (request.startDateString != null) { | ||||
|             chargeEvent.startDate = LocalDate.parse(request.startDateString, dateTimeFormatter).atTime(0, 0) | ||||
|             chargeEvent.startDate = LocalDateTime.parse(request.startDateString, dateTimeFormatter) | ||||
|                 .atZone(ZoneId.of("Asia/Seoul")) | ||||
|                 .withZoneSameInstant(ZoneId.of("UTC")) | ||||
|                 .toLocalDateTime() | ||||
|         } | ||||
|  | ||||
|         if (request.endDateString != null) { | ||||
|             chargeEvent.endDate = LocalDate.parse(request.endDateString, dateTimeFormatter).atTime(23, 59, 59) | ||||
|             chargeEvent.endDate = LocalDateTime.parse(request.endDateString, dateTimeFormatter).withSecond(59) | ||||
|                 .atZone(ZoneId.of("Asia/Seoul")) | ||||
|                 .withZoneSameInstant(ZoneId.of("UTC")) | ||||
|                 .toLocalDateTime() | ||||
| @@ -74,27 +74,5 @@ class AdminChargeEventService(private val repository: AdminChargeEventRepository | ||||
|  | ||||
|     fun getChargeEventList(): List<GetChargeEventListResponse> { | ||||
|         return repository.getChargeEventList() | ||||
|             .map { | ||||
|                 val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") | ||||
|                 val startDate = it.startDate | ||||
|                     .atZone(ZoneId.of("UTC")) | ||||
|                     .withZoneSameInstant(ZoneId.of("Asia/Seoul")) | ||||
|                     .format(dateTimeFormatter) | ||||
|  | ||||
|                 val endDate = it.endDate | ||||
|                     .atZone(ZoneId.of("UTC")) | ||||
|                     .withZoneSameInstant(ZoneId.of("Asia/Seoul")) | ||||
|                     .format(dateTimeFormatter) | ||||
|  | ||||
|                 GetChargeEventListResponse( | ||||
|                     id = it.id!!, | ||||
|                     title = it.title, | ||||
|                     startDate = startDate, | ||||
|                     endDate = endDate, | ||||
|                     availableCount = it.availableCount, | ||||
|                     addPercent = (it.addPercent * 100).toInt(), | ||||
|                     isActive = it.isActive | ||||
|                 ) | ||||
|             } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| package kr.co.vividnext.sodalive.admin.event.charge | ||||
|  | ||||
| data class GetChargeEventListResponse( | ||||
| import com.querydsl.core.annotations.QueryProjection | ||||
|  | ||||
| data class GetChargeEventListResponse @QueryProjection constructor( | ||||
|     val id: Long, | ||||
|     val title: String, | ||||
|     val startDate: String, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user