generated from aura-ascend/template-service
update AccountService with getForUpdateById and save
This commit is contained in:
@@ -2,8 +2,14 @@ package ltd.lulz.repository
|
||||
|
||||
import java.util.UUID
|
||||
import ltd.lulz.model.AccountEntity
|
||||
import org.springframework.data.r2dbc.repository.Query
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@Repository
|
||||
interface AccountRepository : ReactiveCrudRepository<AccountEntity, UUID>
|
||||
interface AccountRepository : ReactiveCrudRepository<AccountEntity, UUID> {
|
||||
|
||||
@Query("SELECT * FROM accounts WHERE id = :id FOR UPDATE NOWAIT")
|
||||
fun findByIdForUpdate(id: UUID): Mono<AccountEntity>
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.UUID
|
||||
import ltd.lulz.model.AccountEntity
|
||||
import ltd.lulz.repository.AccountRepository
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
@@ -20,4 +21,12 @@ class AccountService(
|
||||
|
||||
fun getById(id: UUID): Mono<AccountEntity> = accountRepository.findById(id)
|
||||
.doOnNext { log.debug { "found account by id: ${it.id}" } }
|
||||
|
||||
@Transactional
|
||||
fun getForUpdateById(id: UUID): Mono<AccountEntity> = accountRepository.findByIdForUpdate(id)
|
||||
.doOnNext { log.trace { "account with id: ${it.id} locked for update" } }
|
||||
|
||||
@Transactional
|
||||
fun save(entity: AccountEntity): Mono<AccountEntity> = accountRepository.save(entity)
|
||||
.doOnNext { log.trace { "account with id: ${it.id} saved" } }
|
||||
}
|
||||
|
||||
@@ -69,4 +69,42 @@ class AccountServiceTest {
|
||||
|
||||
verify { repository.findById(any(UUID::class)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get for update by id`() {
|
||||
// given
|
||||
val capture = slot<UUID>()
|
||||
every { repository.findByIdForUpdate(capture(capture)) }
|
||||
.answers { Mono.just(AccountEntity(capture.captured, name, amount)) }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.getForUpdateById(uuid))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount)
|
||||
}
|
||||
.verifyComplete()
|
||||
|
||||
verify { repository.findByIdForUpdate(any(UUID::class)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `save change`() {
|
||||
// given
|
||||
val entity = AccountEntity(name = name, amount = amount)
|
||||
|
||||
val capture = slot<AccountEntity>()
|
||||
every { repository.save(capture(capture)) }
|
||||
.answers { Mono.just(capture.captured) }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.save(entity))
|
||||
.assertNext { result ->
|
||||
assertThat(result).isEqualTo(entity)
|
||||
}
|
||||
.verifyComplete()
|
||||
|
||||
verify { repository.save(any()) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user