관리자 - 이벤트 API
This commit is contained in:
@@ -1,12 +1,22 @@
|
||||
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
|
||||
) {
|
||||
@@ -32,4 +42,164 @@ class EventService(
|
||||
|
||||
return GetEventResponse(0, eventList)
|
||||
}
|
||||
|
||||
fun getEventPopup(): EventItem? {
|
||||
val eventPopup = repository.getMainEventPopup()
|
||||
|
||||
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,
|
||||
isPopup: Boolean
|
||||
): Long {
|
||||
if (detail == null && link.isNullOrBlank()) throw SodaException("상세이미지 혹은 링크를 등록하세요")
|
||||
|
||||
val event = repository.save(
|
||||
Event(
|
||||
thumbnailImage = "",
|
||||
detailImage = null,
|
||||
popupImage = null,
|
||||
link = link,
|
||||
title = title,
|
||||
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,
|
||||
isPopup: Boolean? = null
|
||||
) {
|
||||
if (id <= 0) throw SodaException("잘못된 요청입니다.")
|
||||
|
||||
if (thumbnail == null && detail == null && link.isNullOrBlank() && title.isNullOrBlank()) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun delete(id: Long) {
|
||||
if (id <= 0) throw SodaException("잘못된 요청입니다.")
|
||||
val event = repository.findByIdOrNull(id)
|
||||
?: throw SodaException("잘못된 요청입니다.")
|
||||
|
||||
event.isActive = false
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user