서비스 공지사항 API 추가
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
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
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
@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)
|
||||
.asSequence()
|
||||
.map {
|
||||
val createdAt = it.createdAt!!
|
||||
.atZone(ZoneId.of("UTC"))
|
||||
.withZoneSameInstant(ZoneId.of(timezone))
|
||||
|
||||
NoticeItem(
|
||||
it.id!!,
|
||||
it.title,
|
||||
it.content,
|
||||
createdAt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
|
||||
)
|
||||
}
|
||||
.toList()
|
||||
|
||||
return GetNoticeResponse(totalCount, noticeList)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user