update AccountService with getForUpdateById and save

This commit is contained in:
2025-09-11 17:53:32 +02:00
parent c96604ae79
commit 25f534b6e3
3 changed files with 54 additions and 1 deletions

View File

@@ -2,8 +2,14 @@ package ltd.lulz.repository
import java.util.UUID import java.util.UUID
import ltd.lulz.model.AccountEntity import ltd.lulz.model.AccountEntity
import org.springframework.data.r2dbc.repository.Query
import org.springframework.data.repository.reactive.ReactiveCrudRepository import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import reactor.core.publisher.Mono
@Repository @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>
}

View File

@@ -5,6 +5,7 @@ import java.util.UUID
import ltd.lulz.model.AccountEntity import ltd.lulz.model.AccountEntity
import ltd.lulz.repository.AccountRepository import ltd.lulz.repository.AccountRepository
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
private val log = KotlinLogging.logger {} private val log = KotlinLogging.logger {}
@@ -20,4 +21,12 @@ class AccountService(
fun getById(id: UUID): Mono<AccountEntity> = accountRepository.findById(id) fun getById(id: UUID): Mono<AccountEntity> = accountRepository.findById(id)
.doOnNext { log.debug { "found account by id: ${it.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" } }
} }

View File

@@ -69,4 +69,42 @@ class AccountServiceTest {
verify { repository.findById(any(UUID::class)) } 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()) }
}
} }