diff --git a/src/main/kotlin/roomescape/theme/business/ThemeService.kt b/src/main/kotlin/roomescape/theme/business/ThemeService.kt index 8a31b170..0f968e65 100644 --- a/src/main/kotlin/roomescape/theme/business/ThemeService.kt +++ b/src/main/kotlin/roomescape/theme/business/ThemeService.kt @@ -1,17 +1,13 @@ package roomescape.theme.business import org.springframework.data.repository.findByIdOrNull -import org.springframework.http.HttpStatus import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional -import roomescape.common.exception.ErrorType -import roomescape.common.exception.RoomescapeException +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.ThemeRequest -import roomescape.theme.web.ThemeResponse -import roomescape.theme.web.ThemesResponse -import roomescape.theme.web.toResponse +import roomescape.theme.web.* import java.time.LocalDate @Service @@ -20,11 +16,7 @@ class ThemeService( ) { @Transactional(readOnly = true) fun findById(id: Long): ThemeEntity = themeRepository.findByIdOrNull(id) - ?: throw RoomescapeException( - ErrorType.THEME_NOT_FOUND, - "[themeId: $id]", - HttpStatus.BAD_REQUEST - ) + ?: throw ThemeException(ThemeErrorCode.THEME_NOT_FOUND) @Transactional(readOnly = true) fun findThemes(): ThemeRetrieveListResponse = themeRepository.findAll() @@ -43,31 +35,19 @@ class ThemeService( @Transactional fun createTheme(request: ThemeCreateRequest): ThemeRetrieveResponse { if (themeRepository.existsByName(request.name)) { - throw RoomescapeException( - ErrorType.THEME_DUPLICATED, - "[name: ${request.name}]", - HttpStatus.CONFLICT - ) + throw ThemeException(ThemeErrorCode.THEME_NAME_DUPLICATED) } - return ThemeEntity( - name = request.name, - description = request.description, - thumbnail = request.thumbnail - ).also { - themeRepository.save(it) - }.toResponse() + val theme: ThemeEntity = request.toEntity() + return themeRepository.save(theme).toResponse() } @Transactional fun deleteTheme(id: Long) { if (themeRepository.isReservedTheme(id)) { - throw RoomescapeException( - ErrorType.THEME_IS_USED_CONFLICT, - "[themeId: %d]", - HttpStatus.CONFLICT - ) + throw ThemeException(ThemeErrorCode.THEME_ALREADY_RESERVED) } + themeRepository.deleteById(id) } } diff --git a/src/main/kotlin/roomescape/theme/exception/ThemeErrorCode.kt b/src/main/kotlin/roomescape/theme/exception/ThemeErrorCode.kt new file mode 100644 index 00000000..7f71250e --- /dev/null +++ b/src/main/kotlin/roomescape/theme/exception/ThemeErrorCode.kt @@ -0,0 +1,14 @@ +package roomescape.theme.exception + +import org.springframework.http.HttpStatus +import roomescape.common.exception.ErrorCode + +enum class ThemeErrorCode( + override val httpStatus: HttpStatus, + override val errorCode: String, + override val message: String +) : ErrorCode { + THEME_NOT_FOUND(HttpStatus.NOT_FOUND, "TH001", "테마를 찾을 수 없어요."), + THEME_NAME_DUPLICATED(HttpStatus.BAD_REQUEST, "TH002", "이미 같은 이름의 테마가 있어요."), + THEME_ALREADY_RESERVED(HttpStatus.CONFLICT, "TH003", "예약된 테마라 삭제할 수 없어요.") +} diff --git a/src/main/kotlin/roomescape/theme/exception/ThemeException.kt b/src/main/kotlin/roomescape/theme/exception/ThemeException.kt new file mode 100644 index 00000000..9129f93e --- /dev/null +++ b/src/main/kotlin/roomescape/theme/exception/ThemeException.kt @@ -0,0 +1,8 @@ +package roomescape.theme.exception + +import roomescape.common.exception.RoomescapeExceptionV2 + +class ThemeException( + override val errorCode: ThemeErrorCode, + override val message: String = errorCode.message +) : RoomescapeExceptionV2(errorCode, message)