55 lines
2.0 KiB
Kotlin
55 lines
2.0 KiB
Kotlin
package kr.co.vividnext.sodalive.notice
|
|
|
|
import kr.co.vividnext.sodalive.common.SodaException
|
|
import org.springframework.data.domain.Pageable
|
|
import org.springframework.data.repository.findByIdOrNull
|
|
import org.springframework.stereotype.Service
|
|
import org.springframework.transaction.annotation.Transactional
|
|
|
|
@Service
|
|
@Transactional(readOnly = true)
|
|
class ServiceNoticeService(private val repository: ServiceServiceNoticeRepository) {
|
|
@Transactional
|
|
fun save(request: CreateNoticeRequest): Long {
|
|
if (request.title.isBlank()) throw SodaException("제목을 입력하세요.")
|
|
if (request.content.isBlank()) throw SodaException("내용을 입력하세요.")
|
|
|
|
val notice = request.toEntity()
|
|
return repository.save(notice).id!!
|
|
}
|
|
|
|
@Transactional
|
|
fun update(request: UpdateNoticeRequest) {
|
|
if (request.id <= 0) throw SodaException("잘못된 요청입니다.")
|
|
if (request.title.isNullOrBlank() && request.content.isNullOrBlank()) {
|
|
throw SodaException("수정할 내용을 입력하세요.")
|
|
}
|
|
|
|
val notice = repository.findByIdOrNull(request.id)
|
|
?: throw SodaException("잘못된 요청입니다.")
|
|
|
|
if (!request.title.isNullOrBlank()) notice.title = request.title
|
|
if (!request.content.isNullOrBlank()) notice.content = request.content
|
|
}
|
|
|
|
@Transactional
|
|
fun delete(id: Long) {
|
|
if (id <= 0) throw SodaException("잘못된 요청입니다.")
|
|
val notice = repository.findByIdOrNull(id)
|
|
?: throw SodaException("잘못된 요청입니다.")
|
|
|
|
notice.isActive = false
|
|
}
|
|
|
|
fun getNoticeList(pageable: Pageable, timezone: String): GetNoticeResponse {
|
|
val totalCount = repository.getNoticeTotalCount()
|
|
val noticeList = repository.getNoticeList(pageable)
|
|
|
|
return GetNoticeResponse(totalCount, noticeList)
|
|
}
|
|
|
|
fun getLatestNotice(): NoticeItem? {
|
|
return repository.getLatestNotice()
|
|
}
|
|
}
|