generated from aura-ascend/template-service
reactive
This commit is contained in:
44
src/main/kotlin/ltd/lulz/service/AccountService.kt
Normal file
44
src/main/kotlin/ltd/lulz/service/AccountService.kt
Normal file
@@ -0,0 +1,44 @@
|
||||
package ltd.lulz.service
|
||||
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import java.math.BigDecimal
|
||||
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(
|
||||
name: String,
|
||||
amount: BigDecimal
|
||||
): Mono<AccountEntity> = accountRepository.save(AccountEntity(name = name, amount = amount))
|
||||
.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" } }
|
||||
}
|
||||
Reference in New Issue
Block a user