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(messageKey = "faq.question_required") if (request.answer.isBlank()) throw SodaException(messageKey = "faq.answer_required") if (request.category.isBlank()) throw SodaException(messageKey = "faq.category_required") val category = queryRepository.getCategory(request.category) ?: throw SodaException(messageKey = "faq.invalid_category") 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(messageKey = "common.error.invalid_request") if (request.question != null) { if (request.question.isBlank()) throw SodaException(messageKey = "faq.question_required") faq.question = request.question } if (request.answer != null) { if (request.answer.isBlank()) throw SodaException(messageKey = "faq.answer_required") faq.answer = request.answer } if (request.category != null) { if (request.category.isBlank()) throw SodaException(messageKey = "faq.category_required") val category = queryRepository.getCategory(request.category) ?: throw SodaException(messageKey = "faq.invalid_category") faq.category = category } } @Transactional fun delete(id: Long) { if (id <= 0) throw SodaException(messageKey = "common.error.invalid_request") val faq = repository.findByIdOrNull(id) ?: throw SodaException(messageKey = "common.error.invalid_request") faq.isActive = false } fun getFaqList(category: String) = queryRepository.getFaqList(category) fun getCategoryList() = queryRepository.getCategoryList() }