generated from pricelees/issue-pr-template
[#20] 도메인별 예외 분리 #21
@ -1,17 +1,13 @@
|
|||||||
package roomescape.theme.business
|
package roomescape.theme.business
|
||||||
|
|
||||||
import org.springframework.data.repository.findByIdOrNull
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
import org.springframework.http.HttpStatus
|
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import org.springframework.transaction.annotation.Transactional
|
import org.springframework.transaction.annotation.Transactional
|
||||||
import roomescape.common.exception.ErrorType
|
import roomescape.theme.exception.ThemeErrorCode
|
||||||
import roomescape.common.exception.RoomescapeException
|
import roomescape.theme.exception.ThemeException
|
||||||
import roomescape.theme.infrastructure.persistence.ThemeEntity
|
import roomescape.theme.infrastructure.persistence.ThemeEntity
|
||||||
import roomescape.theme.infrastructure.persistence.ThemeRepository
|
import roomescape.theme.infrastructure.persistence.ThemeRepository
|
||||||
import roomescape.theme.web.ThemeRequest
|
import roomescape.theme.web.*
|
||||||
import roomescape.theme.web.ThemeResponse
|
|
||||||
import roomescape.theme.web.ThemesResponse
|
|
||||||
import roomescape.theme.web.toResponse
|
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -20,11 +16,7 @@ class ThemeService(
|
|||||||
) {
|
) {
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
fun findById(id: Long): ThemeEntity = themeRepository.findByIdOrNull(id)
|
fun findById(id: Long): ThemeEntity = themeRepository.findByIdOrNull(id)
|
||||||
?: throw RoomescapeException(
|
?: throw ThemeException(ThemeErrorCode.THEME_NOT_FOUND)
|
||||||
ErrorType.THEME_NOT_FOUND,
|
|
||||||
"[themeId: $id]",
|
|
||||||
HttpStatus.BAD_REQUEST
|
|
||||||
)
|
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
fun findThemes(): ThemeRetrieveListResponse = themeRepository.findAll()
|
fun findThemes(): ThemeRetrieveListResponse = themeRepository.findAll()
|
||||||
@ -43,31 +35,19 @@ class ThemeService(
|
|||||||
@Transactional
|
@Transactional
|
||||||
fun createTheme(request: ThemeCreateRequest): ThemeRetrieveResponse {
|
fun createTheme(request: ThemeCreateRequest): ThemeRetrieveResponse {
|
||||||
if (themeRepository.existsByName(request.name)) {
|
if (themeRepository.existsByName(request.name)) {
|
||||||
throw RoomescapeException(
|
throw ThemeException(ThemeErrorCode.THEME_NAME_DUPLICATED)
|
||||||
ErrorType.THEME_DUPLICATED,
|
|
||||||
"[name: ${request.name}]",
|
|
||||||
HttpStatus.CONFLICT
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ThemeEntity(
|
val theme: ThemeEntity = request.toEntity()
|
||||||
name = request.name,
|
return themeRepository.save(theme).toResponse()
|
||||||
description = request.description,
|
|
||||||
thumbnail = request.thumbnail
|
|
||||||
).also {
|
|
||||||
themeRepository.save(it)
|
|
||||||
}.toResponse()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
fun deleteTheme(id: Long) {
|
fun deleteTheme(id: Long) {
|
||||||
if (themeRepository.isReservedTheme(id)) {
|
if (themeRepository.isReservedTheme(id)) {
|
||||||
throw RoomescapeException(
|
throw ThemeException(ThemeErrorCode.THEME_ALREADY_RESERVED)
|
||||||
ErrorType.THEME_IS_USED_CONFLICT,
|
|
||||||
"[themeId: %d]",
|
|
||||||
HttpStatus.CONFLICT
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
themeRepository.deleteById(id)
|
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