generated from aura-ascend/template-service
45 lines
1.4 KiB
Kotlin
45 lines
1.4 KiB
Kotlin
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" } }
|
|
}
|