stojan
06/20/2020, 2:41 PMoverride fun open(
no: String,
name: String,
rate: Option<BigDecimal>,
openingDate: Option<LocalDate>,
accountType: AccountType
): AccountOperation<Account> = Kleisli.invoke { repo ->
EitherT(
repo.query(no).flatMap { accountOptionOrError ->
accountOptionOrError.fold(
{ IO { MiscellaneousDomainExceptions(it).left() } },
{ accOpt ->
accOpt.fold(
{
when (accountType) {
AccountType.CHECKING -> createOrUpdate(
repo,
Account.checkingAccount(no, name, openingDate, None, Balance())
)
AccountType.SAVINGS -> rate.map { r ->
createOrUpdate(
repo,
Account.savingsAccount(no, name, r, openingDate, None, Balance())
)
}.getOrElse { IO { RateMissingForSavingsAccount.left() } }
}
},
{ IO { AlreadyExistingAccount(<http://it.no|it.no>).left() } }
)
}
)
}
)
}
the future:
override suspend fun AccountRepository.open(
no: String,
name: String,
rate: BigDecimal?,
openingDate: LocalDate?,
accountType: AccountType
): Either<AccountServiceException, Account> = either {
val existing = query(no).mapLeft { MiscellaneousDomainExceptions(it) }.bind()
existing.rightIfNull { AlreadyExistingAccount(no) }.bind()
val accountOrError = when (accountType) {
AccountType.CHECKING -> Account.checkingAccount(no, name, openingDate, null, Balance())
AccountType.SAVINGS -> {
val r = rate.rightIfNotNull { RateMissingForSavingsAccount }.bind()
Account.savingsAccount(no, name, r, openingDate, null, Balance())
}
}
createOrUpdate(this@open, accountOrError).bind()
}
the future is bright 😄stojan
06/20/2020, 3:44 PMraulraja
06/20/2020, 10:00 PMraulraja
06/20/2020, 10:00 PMstojan
06/20/2020, 10:05 PMstojan
06/20/2020, 10:06 PMraulraja
06/20/2020, 10:06 PMstojan
06/20/2020, 10:06 PMraulraja
06/20/2020, 10:06 PMstojan
06/20/2020, 10:07 PMstojan
06/20/2020, 10:07 PMraulraja
06/20/2020, 10:07 PMstojan
06/20/2020, 10:07 PMraulraja
06/20/2020, 10:08 PMraulraja
06/20/2020, 10:08 PMraulraja
06/20/2020, 10:09 PMraulraja
06/20/2020, 10:10 PMsuspend -> A?
that shortcircuits on null
parTraverse etc..stojan
06/20/2020, 10:10 PMstojan
06/20/2020, 10:11 PMstojan
06/20/2020, 10:11 PMraulraja
06/20/2020, 10:12 PMraulraja
06/20/2020, 10:14 PM