sodalive-backend-spring-boot/src/main/kotlin/kr/co/vividnext/sodalive/faq/FaqService.kt

63 lines
2.2 KiB
Kotlin

package kr.co.vividnext.sodalive.faq
import kr.co.vividnext.sodalive.common.SodaException
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
@Service
class FaqService(
private val repository: FaqRepository,
private val queryRepository: FaqQueryRepository
) {
@Transactional
fun save(request: CreateFaqRequest): Long {
if (request.question.isBlank()) throw SodaException("질문을 입력하세요.")
if (request.answer.isBlank()) throw SodaException("답변을 입력하세요.")
if (request.category.isBlank()) throw SodaException("카테고리를 선택하세요.")
val category = queryRepository.getCategory(request.category)
?: throw SodaException("잘못된 카테고리 입니다.")
val faq = Faq(request.question, request.answer)
faq.category = category
return repository.save(faq).id!!
}
@Transactional
fun modify(request: ModifyFaqRequest) {
val faq = queryRepository.getFaq(request.id)
?: throw SodaException("잘못된 요청입니다.")
if (request.question != null) {
if (request.question.isBlank()) throw SodaException("질문을 입력하세요.")
faq.question = request.question
}
if (request.answer != null) {
if (request.answer.isBlank()) throw SodaException("답변을 입력하세요.")
faq.answer = request.answer
}
if (request.category != null) {
if (request.category.isBlank()) throw SodaException("카테고리를 선택하세요.")
val category = queryRepository.getCategory(request.category) ?: throw SodaException("잘못된 카테고리 입니다.")
faq.category = category
}
}
@Transactional
fun delete(id: Long) {
if (id <= 0) throw SodaException("잘못된 요청입니다.")
val faq = repository.findByIdOrNull(id)
?: throw SodaException("잘못된 요청입니다.")
faq.isActive = false
}
fun getFaqList(category: String) = queryRepository.getFaqList(category)
fun getCategoryList() = queryRepository.getCategoryList()
}