generated from pricelees/issue-pr-template
feat: 테마 도메인에서 사용하는 커스텀 예외 정의 및 Service 적용
This commit is contained in:
parent
821f379e78
commit
e3ba8f2108
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
14
src/main/kotlin/roomescape/theme/exception/ThemeErrorCode.kt
Normal file
14
src/main/kotlin/roomescape/theme/exception/ThemeErrorCode.kt
Normal 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", "예약된 테마라 삭제할 수 없어요.")
|
||||
}
|
||||
@ -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)
|
||||
Loading…
x
Reference in New Issue
Block a user