generated from pricelees/issue-pr-template
54 lines
1.8 KiB
Kotlin
54 lines
1.8 KiB
Kotlin
package roomescape.theme.business
|
|
|
|
import org.springframework.data.repository.findByIdOrNull
|
|
import org.springframework.stereotype.Service
|
|
import org.springframework.transaction.annotation.Transactional
|
|
import roomescape.theme.exception.ThemeErrorCode
|
|
import roomescape.theme.exception.ThemeException
|
|
import roomescape.theme.infrastructure.persistence.ThemeEntity
|
|
import roomescape.theme.infrastructure.persistence.ThemeRepository
|
|
import roomescape.theme.web.*
|
|
import java.time.LocalDate
|
|
|
|
@Service
|
|
class ThemeService(
|
|
private val themeRepository: ThemeRepository
|
|
) {
|
|
@Transactional(readOnly = true)
|
|
fun findById(id: Long): ThemeEntity = themeRepository.findByIdOrNull(id)
|
|
?: throw ThemeException(ThemeErrorCode.THEME_NOT_FOUND)
|
|
|
|
@Transactional(readOnly = true)
|
|
fun findThemes(): ThemeRetrieveListResponse = themeRepository.findAll()
|
|
.toResponse()
|
|
|
|
@Transactional(readOnly = true)
|
|
fun findMostReservedThemes(count: Int): ThemeRetrieveListResponse {
|
|
val today = LocalDate.now()
|
|
val startDate = today.minusDays(7)
|
|
val endDate = today.minusDays(1)
|
|
|
|
return themeRepository.findPopularThemes(startDate, endDate, count)
|
|
.toResponse()
|
|
}
|
|
|
|
@Transactional
|
|
fun createTheme(request: ThemeCreateRequest): ThemeRetrieveResponse {
|
|
if (themeRepository.existsByName(request.name)) {
|
|
throw ThemeException(ThemeErrorCode.THEME_NAME_DUPLICATED)
|
|
}
|
|
|
|
val theme: ThemeEntity = request.toEntity()
|
|
return themeRepository.save(theme).toResponse()
|
|
}
|
|
|
|
@Transactional
|
|
fun deleteTheme(id: Long) {
|
|
if (themeRepository.isReservedTheme(id)) {
|
|
throw ThemeException(ThemeErrorCode.THEME_ALREADY_RESERVED)
|
|
}
|
|
|
|
themeRepository.deleteById(id)
|
|
}
|
|
}
|