diff --git a/src/main/kotlin/com/mjucow/eatda/domain/notice/service/command/NoticeCommandService.kt b/src/main/kotlin/com/mjucow/eatda/domain/notice/service/command/NoticeCommandService.kt index 666ce3e..2028b42 100644 --- a/src/main/kotlin/com/mjucow/eatda/domain/notice/service/command/NoticeCommandService.kt +++ b/src/main/kotlin/com/mjucow/eatda/domain/notice/service/command/NoticeCommandService.kt @@ -19,10 +19,10 @@ class NoticeCommandService( fun update(noticeId: Long, command: UpdateNoticeCommand) { val (newTitle, newContent) = command - val updatedNotice = repository.findByIdOrNull(noticeId)?.apply { + val updatedNotice = repository.getReferenceById(noticeId).apply { title = newTitle content = newContent - } ?: throw IllegalArgumentException() + } repository.save(updatedNotice) } diff --git a/src/main/kotlin/com/mjucow/eatda/domain/notice/service/query/NoticeQueryService.kt b/src/main/kotlin/com/mjucow/eatda/domain/notice/service/query/NoticeQueryService.kt index 7c96a4a..f4a4b67 100644 --- a/src/main/kotlin/com/mjucow/eatda/domain/notice/service/query/NoticeQueryService.kt +++ b/src/main/kotlin/com/mjucow/eatda/domain/notice/service/query/NoticeQueryService.kt @@ -3,8 +3,6 @@ package com.mjucow.eatda.domain.notice.service.query import com.mjucow.eatda.domain.notice.service.query.dto.NoticeDto import com.mjucow.eatda.domain.notice.service.query.dto.Notices import com.mjucow.eatda.persistence.notice.NoticeRepository -import jakarta.persistence.EntityNotFoundException -import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -18,8 +16,6 @@ class NoticeQueryService( } fun findById(noticeId: Long): NoticeDto { - return repository.findByIdOrNull(noticeId)?.let { - NoticeDto.from(it) - } ?: throw EntityNotFoundException("공지사항이 존재하지 않습니다.") + return NoticeDto.from(repository.getReferenceById(noticeId)) } } diff --git a/src/main/kotlin/com/mjucow/eatda/domain/store/service/command/CategoryCommandService.kt b/src/main/kotlin/com/mjucow/eatda/domain/store/service/command/CategoryCommandService.kt index adcb029..c82c0b3 100644 --- a/src/main/kotlin/com/mjucow/eatda/domain/store/service/command/CategoryCommandService.kt +++ b/src/main/kotlin/com/mjucow/eatda/domain/store/service/command/CategoryCommandService.kt @@ -5,7 +5,6 @@ import com.mjucow.eatda.domain.store.service.command.dto.CreateCommand import com.mjucow.eatda.domain.store.service.command.dto.UpdateNameCommand import com.mjucow.eatda.persistence.store.CategoryRepository import jakarta.transaction.Transactional -import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service @Service @@ -21,7 +20,7 @@ class CategoryCommandService( } fun updateName(id: Long, command: UpdateNameCommand) { - val domain = getById(id) + val domain = repository.getReferenceById(id) val newName = command.name.trim() if (domain.name == newName) { @@ -37,9 +36,6 @@ class CategoryCommandService( repository.deleteById(id) } - private fun getById(id: Long): Category { - return repository.findByIdOrNull(id) ?: throw IllegalArgumentException() - } private fun checkDuplicatedName(name: String) { require(repository.existsByName(name).not()) } diff --git a/src/main/kotlin/com/mjucow/eatda/domain/store/service/command/StoreCommandService.kt b/src/main/kotlin/com/mjucow/eatda/domain/store/service/command/StoreCommandService.kt index 17ca139..95dcc30 100644 --- a/src/main/kotlin/com/mjucow/eatda/domain/store/service/command/StoreCommandService.kt +++ b/src/main/kotlin/com/mjucow/eatda/domain/store/service/command/StoreCommandService.kt @@ -4,8 +4,6 @@ import com.mjucow.eatda.domain.store.entity.Store import com.mjucow.eatda.domain.store.service.command.dto.StoreCreateCommand import com.mjucow.eatda.domain.store.service.command.dto.StoreUpdateCommand import com.mjucow.eatda.persistence.store.StoreRepository -import jakarta.persistence.EntityNotFoundException -import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -36,7 +34,7 @@ class StoreCommandService( } fun update(id: Long, command: StoreUpdateCommand) { - val store = repository.findByIdOrNull(id) ?: throw EntityNotFoundException() + val store = repository.getReferenceById(id) if (store.name != command.name) { checkDuplicateName(command.name) } diff --git a/src/main/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryService.kt b/src/main/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryService.kt index 9dc50eb..bf19563 100644 --- a/src/main/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryService.kt +++ b/src/main/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryService.kt @@ -3,7 +3,6 @@ package com.mjucow.eatda.domain.store.service.query import com.mjucow.eatda.domain.store.service.query.dto.Categories import com.mjucow.eatda.domain.store.service.query.dto.CategoryDto import com.mjucow.eatda.persistence.store.CategoryRepository -import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -16,7 +15,7 @@ class CategoryQueryService( return Categories(repository.findAll().map(CategoryDto::from)) } - fun findById(id: Long): CategoryDto? { - return repository.findByIdOrNull(id)?.let { CategoryDto.from(it) } + fun findById(id: Long): CategoryDto { + return CategoryDto.from(repository.getReferenceById(id)) } } diff --git a/src/main/kotlin/com/mjucow/eatda/domain/store/service/query/StoreQueryService.kt b/src/main/kotlin/com/mjucow/eatda/domain/store/service/query/StoreQueryService.kt index 53d87c3..6b67711 100644 --- a/src/main/kotlin/com/mjucow/eatda/domain/store/service/query/StoreQueryService.kt +++ b/src/main/kotlin/com/mjucow/eatda/domain/store/service/query/StoreQueryService.kt @@ -6,7 +6,6 @@ import com.mjucow.eatda.persistence.store.StoreRepository import org.springframework.data.domain.Pageable import org.springframework.data.domain.Slice import org.springframework.data.domain.SliceImpl -import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -26,7 +25,7 @@ class StoreQueryService( } } - fun findById(storeId: Long): StoreDetailDto? { - return repository.findByIdOrNull(storeId)?.let { StoreDetailDto.from(it) } + fun findById(storeId: Long): StoreDetailDto { + return StoreDetailDto.from(repository.getReferenceById(storeId)) } } diff --git a/src/test/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryServiceDataTest.kt b/src/test/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryServiceDataTest.kt index c7ee800..3718cca 100644 --- a/src/test/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryServiceDataTest.kt +++ b/src/test/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryServiceDataTest.kt @@ -5,6 +5,7 @@ import com.mjucow.eatda.domain.AbstractDataTest import com.mjucow.eatda.domain.store.entity.Category import com.mjucow.eatda.persistence.store.CategoryRepository import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.catchThrowable import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest @@ -19,17 +20,17 @@ class CategoryQueryServiceDataTest : AbstractDataTest() { @Autowired lateinit var repository: CategoryRepository - @DisplayName("id에 해당하는 카테고리가 없으면 null을 반환한다") + @DisplayName("id에 해당하는 카테고리가 없으면 예외를 던진다") @ParameterizedTest @AutoKotlinSource - fun returnNullWhenNotFoundId(id: Long) { + fun throwExceptionWhenNotFoundId(id: Long) { // given // when - val domain = categoryQueryService.findById(id) + val throwable = catchThrowable { categoryQueryService.findById(id) } // then - assertThat(domain).isNull() + assertThat(throwable).isNotNull() } @DisplayName("id에 해당하는 카테고리를 반환한다") diff --git a/src/test/kotlin/com/mjucow/eatda/domain/store/service/query/StoreQueryServiceDataTest.kt b/src/test/kotlin/com/mjucow/eatda/domain/store/service/query/StoreQueryServiceDataTest.kt index 4687a68..bcc9e8b 100644 --- a/src/test/kotlin/com/mjucow/eatda/domain/store/service/query/StoreQueryServiceDataTest.kt +++ b/src/test/kotlin/com/mjucow/eatda/domain/store/service/query/StoreQueryServiceDataTest.kt @@ -8,6 +8,7 @@ import com.mjucow.eatda.domain.store.entity.objectmother.StoreMother import com.mjucow.eatda.persistence.store.CategoryRepository import com.mjucow.eatda.persistence.store.StoreRepository import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.catchThrowable import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest @@ -27,17 +28,17 @@ class StoreQueryServiceDataTest : AbstractDataTest() { @Autowired lateinit var categoryRepository: CategoryRepository - @DisplayName("id에 해당하는 store가 없다면 null을 반환한다") + @DisplayName("id에 해당하는 store가 없다면 예외를 던진다") @ParameterizedTest @AutoKotlinSource - fun returnNullWhenNotExistEntity(id: Long) { + fun throwExceptionWhenNotExistEntity(id: Long) { // given // when - val dto = storeQueryService.findById(id) + val throwable = catchThrowable { storeQueryService.findById(id) } // then - assertThat(dto).isNull() + assertThat(throwable).isNotNull() } @DisplayName("id에 해당하는 store를 반환한다")