211 lines
6.7 KiB
Kotlin
211 lines
6.7 KiB
Kotlin
package kr.co.vividnext.sodalive.event
|
|
|
|
import com.amazonaws.services.s3.model.ObjectMetadata
|
|
import kr.co.vividnext.sodalive.aws.s3.S3Uploader
|
|
import kr.co.vividnext.sodalive.common.SodaException
|
|
import kr.co.vividnext.sodalive.utils.generateFileName
|
|
import org.springframework.beans.factory.annotation.Value
|
|
import org.springframework.data.repository.findByIdOrNull
|
|
import org.springframework.stereotype.Service
|
|
import org.springframework.transaction.annotation.Transactional
|
|
import org.springframework.web.multipart.MultipartFile
|
|
|
|
@Service
|
|
class EventService(
|
|
private val repository: EventRepository,
|
|
private val s3Uploader: S3Uploader,
|
|
|
|
@Value("\${cloud.aws.s3.bucket}")
|
|
private val bucket: String,
|
|
@Value("\${cloud.aws.cloud-front.host}")
|
|
private val cloudFrontHost: String
|
|
) {
|
|
@Transactional(readOnly = true)
|
|
fun getEventList(isAdult: Boolean? = null): GetEventResponse {
|
|
val eventList = repository.getEventList(isAdult)
|
|
.asSequence()
|
|
.map {
|
|
if (!it.thumbnailImageUrl.startsWith("https://")) {
|
|
it.thumbnailImageUrl = "$cloudFrontHost/${it.thumbnailImageUrl}"
|
|
}
|
|
|
|
if (it.detailImageUrl != null && !it.detailImageUrl!!.startsWith("https://")) {
|
|
it.detailImageUrl = "$cloudFrontHost/${it.detailImageUrl}"
|
|
}
|
|
|
|
if (it.popupImageUrl != null && !it.popupImageUrl!!.startsWith("https://")) {
|
|
it.popupImageUrl = "$cloudFrontHost/${it.popupImageUrl}"
|
|
}
|
|
|
|
it
|
|
}
|
|
.toList()
|
|
|
|
return GetEventResponse(0, eventList)
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
fun getEventPopup(isAdult: Boolean): EventItem? {
|
|
val eventPopup = repository.getMainEventPopup(isAdult)
|
|
|
|
if (eventPopup != null) {
|
|
if (!eventPopup.thumbnailImageUrl.startsWith("https://")) {
|
|
eventPopup.thumbnailImageUrl = "$cloudFrontHost/${eventPopup.thumbnailImageUrl}"
|
|
}
|
|
|
|
if (eventPopup.detailImageUrl != null && !eventPopup.detailImageUrl!!.startsWith("https://")) {
|
|
eventPopup.detailImageUrl = "$cloudFrontHost/${eventPopup.detailImageUrl}"
|
|
}
|
|
|
|
if (eventPopup.popupImageUrl != null && !eventPopup.popupImageUrl!!.startsWith("https://")) {
|
|
eventPopup.popupImageUrl = "$cloudFrontHost/${eventPopup.popupImageUrl}"
|
|
}
|
|
}
|
|
|
|
return eventPopup
|
|
}
|
|
|
|
@Transactional
|
|
fun save(
|
|
thumbnail: MultipartFile,
|
|
detail: MultipartFile? = null,
|
|
popup: MultipartFile? = null,
|
|
link: String? = null,
|
|
title: String? = null,
|
|
isAdult: Boolean? = null,
|
|
isPopup: Boolean
|
|
): Long {
|
|
if (detail == null && link.isNullOrBlank()) throw SodaException("상세이미지 혹은 링크를 등록하세요")
|
|
|
|
val event = repository.save(
|
|
Event(
|
|
thumbnailImage = "",
|
|
detailImage = null,
|
|
popupImage = null,
|
|
link = link,
|
|
title = title,
|
|
isAdult = isAdult,
|
|
isPopup = isPopup
|
|
)
|
|
)
|
|
|
|
var metadata = ObjectMetadata()
|
|
metadata.contentLength = thumbnail.size
|
|
val thumbnailImagePath = s3Uploader.upload(
|
|
inputStream = thumbnail.inputStream,
|
|
bucket = bucket,
|
|
filePath = "event/${event.id}/${generateFileName()}",
|
|
metadata = metadata
|
|
)
|
|
|
|
val detailImagePath = if (detail != null) {
|
|
metadata = ObjectMetadata()
|
|
metadata.contentLength = detail.size
|
|
|
|
s3Uploader.upload(
|
|
inputStream = detail.inputStream,
|
|
bucket = bucket,
|
|
filePath = "event/${event.id}/${generateFileName()}",
|
|
metadata = metadata
|
|
)
|
|
} else {
|
|
null
|
|
}
|
|
|
|
val popupImagePath = if (popup != null) {
|
|
metadata = ObjectMetadata()
|
|
metadata.contentLength = popup.size
|
|
|
|
s3Uploader.upload(
|
|
inputStream = popup.inputStream,
|
|
bucket = bucket,
|
|
filePath = "event/${event.id}/${generateFileName()}",
|
|
metadata = metadata
|
|
)
|
|
} else {
|
|
null
|
|
}
|
|
|
|
event.thumbnailImage = thumbnailImagePath
|
|
event.detailImage = detailImagePath
|
|
event.popupImage = popupImagePath
|
|
|
|
return event.id ?: throw SodaException("이벤트 등록을 하지 못했습니다.")
|
|
}
|
|
|
|
@Transactional
|
|
fun update(
|
|
id: Long,
|
|
thumbnail: MultipartFile? = null,
|
|
detail: MultipartFile? = null,
|
|
popup: MultipartFile? = null,
|
|
link: String? = null,
|
|
title: String? = null,
|
|
isAdult: Boolean? = null,
|
|
isPopup: Boolean? = null
|
|
) {
|
|
if (id <= 0) throw SodaException("잘못된 요청입니다.")
|
|
|
|
val event = repository.findByIdOrNull(id)
|
|
?: throw SodaException("잘못된 요청입니다.")
|
|
|
|
if (thumbnail != null) {
|
|
val metadata = ObjectMetadata()
|
|
metadata.contentLength = thumbnail.size
|
|
|
|
event.thumbnailImage = s3Uploader.upload(
|
|
inputStream = thumbnail.inputStream,
|
|
bucket = bucket,
|
|
filePath = "event/${event.id}/${generateFileName()}"
|
|
)
|
|
}
|
|
|
|
if (detail != null) {
|
|
val metadata = ObjectMetadata()
|
|
metadata.contentLength = detail.size
|
|
|
|
event.detailImage = s3Uploader.upload(
|
|
inputStream = detail.inputStream,
|
|
bucket = bucket,
|
|
filePath = "event/${event.id}/${generateFileName()}"
|
|
)
|
|
}
|
|
|
|
if (popup != null) {
|
|
val metadata = ObjectMetadata()
|
|
metadata.contentLength = popup.size
|
|
|
|
event.popupImage = s3Uploader.upload(
|
|
inputStream = popup.inputStream,
|
|
bucket = bucket,
|
|
filePath = "event/${event.id}/${generateFileName()}"
|
|
)
|
|
}
|
|
|
|
if (!link.isNullOrBlank() && event.link != link) {
|
|
event.link = link
|
|
}
|
|
|
|
if (!title.isNullOrBlank() && event.title != title) {
|
|
event.title = title
|
|
}
|
|
|
|
if (isPopup != null) {
|
|
event.isPopup = isPopup
|
|
}
|
|
|
|
if (isAdult != event.isAdult) {
|
|
event.isAdult = isAdult
|
|
}
|
|
}
|
|
|
|
@Transactional
|
|
fun delete(id: Long) {
|
|
if (id <= 0) throw SodaException("잘못된 요청입니다.")
|
|
val event = repository.findByIdOrNull(id)
|
|
?: throw SodaException("잘못된 요청입니다.")
|
|
|
|
event.isActive = false
|
|
}
|
|
}
|