package roomescape.theme.business import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import roomescape.theme.implement.ThemeFinder import roomescape.theme.implement.ThemeWriter import roomescape.theme.infrastructure.persistence.ThemeEntity import roomescape.theme.web.* import java.time.LocalDate private val log = KotlinLogging.logger {} @Service class ThemeService( private val themeFinder: ThemeFinder, private val themeWriter: ThemeWriter, ) { @Transactional(readOnly = true) fun findById(id: Long): ThemeEntity { log.info { "[ThemeService.findById] 시작: themeId=$id" } return themeFinder.findById(id) .also { log.info { "[ThemeService.findById] 완료: themeId=$id, name=${it.name}" } } } @Transactional(readOnly = true) fun findThemes(): ThemeRetrieveListResponse { log.info { "[ThemeService.findThemes] 시작" } return themeFinder.findAll() .toRetrieveListResponse() .also { log.info { "[ThemeService.findThemes] 완료. ${it.themes.size}개 반환" } } } @Transactional(readOnly = true) fun findMostReservedThemes(count: Int): ThemeRetrieveListResponse { log.info { "[ThemeService.findMostReservedThemes] 시작: count=$count" } val today = LocalDate.now() val startFrom = today.minusDays(7) val endAt = today.minusDays(1) return themeFinder.findMostReservedThemes(count, startFrom, endAt) .toRetrieveListResponse() .also { log.info { "[ThemeService.findMostReservedThemes] ${it.themes.size}개 반환" } } } @Transactional fun createTheme(request: ThemeCreateRequest): ThemeCreateResponse { log.info { "[ThemeService.createTheme] 시작: name=${request.name}" } return themeWriter.create(request.name, request.description, request.thumbnail) .toCreateResponse() .also { log.info { "[ThemeService.createTheme] 테마 생성 완료: name=${it.name} themeId=${it.id}" } } } @Transactional fun deleteTheme(id: Long) { log.info { "[ThemeService.deleteTheme] 시작: themeId=$id" } val theme: ThemeEntity = themeFinder.findById(id) themeWriter.delete(theme) .also { log.info { "[ThemeService.deleteTheme] 완료: themeId=$id, name=${theme.name}" } } } }