refactor: 테마 API 로깅 추가

This commit is contained in:
이상진 2025-07-27 23:09:33 +09:00
parent 1140c0f067
commit 351069a572

View File

@ -1,5 +1,6 @@
package roomescape.theme.business
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
@ -10,44 +11,71 @@ import roomescape.theme.infrastructure.persistence.ThemeRepository
import roomescape.theme.web.*
import java.time.LocalDate
private val log = KotlinLogging.logger {}
@Service
class ThemeService(
private val themeRepository: ThemeRepository
private val themeRepository: ThemeRepository,
) {
@Transactional(readOnly = true)
fun findById(id: Long): ThemeEntity = themeRepository.findByIdOrNull(id)
?: throw ThemeException(ThemeErrorCode.THEME_NOT_FOUND)
fun findById(id: Long): ThemeEntity {
log.debug { "[ThemeService.findById] 테마 조회 시작: themeId=$id" }
return themeRepository.findByIdOrNull(id)
?.also { log.info { "[ThemeService.findById] 테마 조회 완료: themeId=$id" } }
?: run {
log.warn { "[ThemeService.findById] 테마 조회 실패: themeId=$id" }
throw ThemeException(ThemeErrorCode.THEME_NOT_FOUND)
}
}
@Transactional(readOnly = true)
fun findThemes(): ThemeRetrieveListResponse = themeRepository.findAll()
fun findThemes(): ThemeRetrieveListResponse {
log.debug { "[ThemeService.findThemes] 모든 테마 조회 시작" }
return themeRepository.findAll()
.also { log.info { "[ThemeService.findThemes] ${it.size}개의 테마 조회 완료" } }
.toResponse()
}
@Transactional(readOnly = true)
fun findMostReservedThemes(count: Int): ThemeRetrieveListResponse {
log.debug { "[ThemeService.findMostReservedThemes] 인기 테마 조회 시작: count=$count" }
val today = LocalDate.now()
val startDate = today.minusDays(7)
val endDate = today.minusDays(1)
return themeRepository.findPopularThemes(startDate, endDate, count)
.toResponse()
.also { log.info { "[ThemeService.findMostReservedThemes] ${it.size} 개의 인기 테마 조회 완료" } }
.toResponse()
}
@Transactional
fun createTheme(request: ThemeCreateRequest): ThemeRetrieveResponse {
log.debug { "[ThemeService.createTheme] 테마 생성 시작: name=${request.name}" }
if (themeRepository.existsByName(request.name)) {
log.info { "[ThemeService.createTheme] 테마 생성 실패(이름 중복): name=${request.name}" }
throw ThemeException(ThemeErrorCode.THEME_NAME_DUPLICATED)
}
val theme: ThemeEntity = request.toEntity()
return themeRepository.save(theme).toResponse()
return themeRepository.save(theme)
.also { log.info { "[ThemeService.createTheme] 테마 생성 완료: themeId=${it.id}" } }
.toResponse()
}
@Transactional
fun deleteTheme(id: Long) {
log.debug { "[ThemeService.deleteTheme] 테마 삭제 시작: themeId=$id" }
if (themeRepository.isReservedTheme(id)) {
log.info { "[ThemeService.deleteTheme] 테마 삭제 실패(예약이 있는 테마): themeId=$id" }
throw ThemeException(ThemeErrorCode.THEME_ALREADY_RESERVED)
}
themeRepository.deleteById(id)
.also { log.info { "[ThemeService.deleteTheme] 테마 삭제 완료: themeId=$id" } }
}
}