Skip to content

Commit

Permalink
[refactor] findByIdOrNull -> getReferenceById (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
dojinyou authored Oct 15, 2023
1 parent 3379581 commit 10142e8
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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에 해당하는 카테고리를 반환한다")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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를 반환한다")
Expand Down

0 comments on commit 10142e8

Please sign in to comment.