generated from aura-ascend/template-service
- update balance in AccountController with onErrorResume - update AccountService - update getForUpdateById with switchIfEmpty - update getById with switchIfEmpty - add AccountNotFoundException
44 lines
1.4 KiB
Kotlin
44 lines
1.4 KiB
Kotlin
package ltd.lulz.service
|
|
|
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
|
import java.util.UUID
|
|
import ltd.lulz.exception.AccountNotFoundException
|
|
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 {}
|
|
|
|
@Service
|
|
class AccountService(
|
|
private val accountRepository: AccountRepository,
|
|
) {
|
|
|
|
fun create(
|
|
entity: AccountEntity,
|
|
): Mono<AccountEntity> = accountRepository
|
|
.save(entity)
|
|
.doOnNext { log.debug { "account created with id: ${it.id}" } }
|
|
|
|
fun getById(
|
|
id: UUID,
|
|
): Mono<AccountEntity> = accountRepository.findById(id)
|
|
.doOnNext { log.debug { "found account by id: ${it.id}" } }
|
|
.switchIfEmpty(Mono.error(AccountNotFoundException()))
|
|
|
|
@Transactional
|
|
fun getForUpdateById(
|
|
id: UUID,
|
|
): Mono<AccountEntity> = accountRepository.findByIdForUpdate(id)
|
|
.doOnNext { log.trace { "account with id: ${it.id} locked for update" } }
|
|
.switchIfEmpty(Mono.error(AccountNotFoundException()))
|
|
|
|
@Transactional
|
|
fun save(
|
|
entity: AccountEntity,
|
|
): Mono<AccountEntity> = accountRepository.save(entity)
|
|
.doOnNext { log.trace { "account with id: ${it.id} saved" } }
|
|
}
|