[#20] 도메인별 예외 분리 #21

Merged
pricelees merged 37 commits from refactor/#20 into main 2025-07-24 02:48:53 +00:00
3 changed files with 31 additions and 29 deletions
Showing only changes of commit e3ba8f2108 - Show all commits

View File

@ -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)
}
}

View File

@ -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", "예약된 테마라 삭제할 수 없어요.")
}

View File

@ -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)