From 351069a572742423e152cf58d611eaa6996a4bad Mon Sep 17 00:00:00 2001 From: pricelees Date: Sun, 27 Jul 2025 23:09:33 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=ED=85=8C=EB=A7=88=20API=20?= =?UTF-8?q?=EB=A1=9C=EA=B9=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roomescape/theme/business/ThemeService.kt | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/roomescape/theme/business/ThemeService.kt b/src/main/kotlin/roomescape/theme/business/ThemeService.kt index 0f968e65..ed8006c2 100644 --- a/src/main/kotlin/roomescape/theme/business/ThemeService.kt +++ b/src/main/kotlin/roomescape/theme/business/ThemeService.kt @@ -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" } } } }