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