Implemented updating holdings
This commit is contained in:
parent
91d16b7e28
commit
5029a2c3cb
|
@ -238,7 +238,22 @@ open class SqliteBankingRepository(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updateHoldings(holdings: List<HoldingEntity>) {
|
override suspend fun updateHoldings(holdings: List<HoldingEntity>) {
|
||||||
// TODO
|
accountTransactionQueries.transaction {
|
||||||
|
holdings.onEach { holding ->
|
||||||
|
accountTransactionQueries.updateHolding(
|
||||||
|
holding.name, holding.isin, holding.wkn,
|
||||||
|
mapInt(holding.quantity), holding.currency,
|
||||||
|
|
||||||
|
mapAmount(holding.totalBalance), mapAmount(holding.marketValue),
|
||||||
|
holding.performancePercentage?.toDouble(),
|
||||||
|
mapAmount(holding.totalCostPrice), mapAmount(holding.averageCostPrice),
|
||||||
|
|
||||||
|
mapInstant(holding.pricingTime), mapDate(holding.buyingDate),
|
||||||
|
|
||||||
|
holding.id
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun deleteHoldings(holdings: List<HoldingEntity>) {
|
override suspend fun deleteHoldings(holdings: List<HoldingEntity>) {
|
||||||
|
|
|
@ -15,9 +15,7 @@ import net.codinux.banking.client.model.response.*
|
||||||
import net.codinux.banking.client.model.securitiesaccount.Holding
|
import net.codinux.banking.client.model.securitiesaccount.Holding
|
||||||
import net.codinux.banking.client.service.BankingModelService
|
import net.codinux.banking.client.service.BankingModelService
|
||||||
import net.codinux.banking.dataaccess.BankingRepository
|
import net.codinux.banking.dataaccess.BankingRepository
|
||||||
import net.codinux.banking.dataaccess.entities.AccountTransactionEntity
|
import net.codinux.banking.dataaccess.entities.*
|
||||||
import net.codinux.banking.dataaccess.entities.BankAccountEntity
|
|
||||||
import net.codinux.banking.dataaccess.entities.UserEntity
|
|
||||||
import net.codinux.banking.ui.IOorDefault
|
import net.codinux.banking.ui.IOorDefault
|
||||||
import net.codinux.banking.ui.model.AccountTransactionViewModel
|
import net.codinux.banking.ui.model.AccountTransactionViewModel
|
||||||
import net.codinux.banking.ui.model.BankInfo
|
import net.codinux.banking.ui.model.BankInfo
|
||||||
|
@ -50,6 +48,7 @@ class BankingService(
|
||||||
uiState.users.value = getAllUsers()
|
uiState.users.value = getAllUsers()
|
||||||
|
|
||||||
uiState.transactions.value = getAllAccountTransactionsAsViewModel()
|
uiState.transactions.value = getAllAccountTransactionsAsViewModel()
|
||||||
|
uiState.holdings.value = uiState.users.value.flatMap { it.accounts }.flatMap { it.holdings }
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
log.error(e) { "Could not read all user accounts and account transactions from repository" }
|
log.error(e) { "Could not read all user accounts and account transactions from repository" }
|
||||||
}
|
}
|
||||||
|
@ -114,7 +113,7 @@ class BankingService(
|
||||||
uiState.users.value = users
|
uiState.users.value = users
|
||||||
|
|
||||||
updateTransactionsInUi(newUserEntity.accounts.flatMap { it.bookedTransactionsEntities })
|
updateTransactionsInUi(newUserEntity.accounts.flatMap { it.bookedTransactionsEntities })
|
||||||
updateHoldingsInUi(newUserEntity.accounts.flatMap { it.holdings })
|
updateHoldingsInUi(newUserEntity.accounts.flatMap { it.holdings }, emptyList())
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
log.error(e) { "Could not save user account ${response.user}" }
|
log.error(e) { "Could not save user account ${response.user}" }
|
||||||
}
|
}
|
||||||
|
@ -179,7 +178,7 @@ class BankingService(
|
||||||
val (updatedExistingHoldings, deletedHoldings) = account.holdings.partition { retrievedHoldingsByIsin.keys.contains(it.identifier) }
|
val (updatedExistingHoldings, deletedHoldings) = account.holdings.partition { retrievedHoldingsByIsin.keys.contains(it.identifier) }
|
||||||
|
|
||||||
val persistedNewHoldings = bankingRepository.persistHoldings(account, newHoldings)
|
val persistedNewHoldings = bankingRepository.persistHoldings(account, newHoldings)
|
||||||
// bankingRepository.updateHoldings(updatedExistingHoldings) // TODO: map data of updatedRetrievedHoldings to updatedExistingHoldings
|
bankingRepository.updateHoldings(updateHoldings(updatedExistingHoldings, updatedRetrievedHoldings))
|
||||||
bankingRepository.deleteHoldings(deletedHoldings)
|
bankingRepository.deleteHoldings(deletedHoldings)
|
||||||
|
|
||||||
account.holdings = account.holdings.toMutableList().apply {
|
account.holdings = account.holdings.toMutableList().apply {
|
||||||
|
@ -187,8 +186,7 @@ class BankingService(
|
||||||
removeAll(deletedHoldings)
|
removeAll(deletedHoldings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateHoldingsInUi(persistedNewHoldings, deletedHoldings)
|
||||||
updateHoldingsInUi(response.holdings)
|
|
||||||
|
|
||||||
val transactionsViewModel = updateTransactionsInUi(newTransactionsEntities)
|
val transactionsViewModel = updateTransactionsInUi(newTransactionsEntities)
|
||||||
uiState.dispatchNewTransactionsRetrieved(AccountTransactionsRetrievedEvent(user, account, transactionsViewModel, response.holdings))
|
uiState.dispatchNewTransactionsRetrieved(AccountTransactionsRetrievedEvent(user, account, transactionsViewModel, response.holdings))
|
||||||
|
@ -198,6 +196,27 @@ class BankingService(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateHoldings(updatedExistingHoldings: List<HoldingEntity>, updatedRetrievedHoldings: List<Holding>): List<HoldingEntity> =
|
||||||
|
updatedExistingHoldings.onEach { holding ->
|
||||||
|
updatedRetrievedHoldings.firstOrNull { it.identifier == holding.identifier }?.let { retrievedHolding ->
|
||||||
|
holding.name = retrievedHolding.name
|
||||||
|
holding.isin = retrievedHolding.isin
|
||||||
|
holding.wkn = retrievedHolding.wkn
|
||||||
|
|
||||||
|
holding.quantity = retrievedHolding.quantity
|
||||||
|
holding.currency = retrievedHolding.currency
|
||||||
|
|
||||||
|
holding.totalBalance = retrievedHolding.totalBalance
|
||||||
|
holding.marketValue = retrievedHolding.marketValue
|
||||||
|
holding.performancePercentage = retrievedHolding.performancePercentage
|
||||||
|
holding.totalCostPrice = retrievedHolding.totalCostPrice
|
||||||
|
holding.averageCostPrice = retrievedHolding.averageCostPrice
|
||||||
|
|
||||||
|
holding.pricingTime = retrievedHolding.pricingTime
|
||||||
|
holding.buyingDate = retrievedHolding.buyingDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun updateTransactionsInUi(addedTransactions: List<AccountTransactionEntity>): List<AccountTransactionViewModel> {
|
private fun updateTransactionsInUi(addedTransactions: List<AccountTransactionEntity>): List<AccountTransactionViewModel> {
|
||||||
val transactionsViewModel = addedTransactions.map { AccountTransactionViewModel(it) }
|
val transactionsViewModel = addedTransactions.map { AccountTransactionViewModel(it) }
|
||||||
|
|
||||||
|
@ -208,9 +227,10 @@ class BankingService(
|
||||||
return transactionsViewModel
|
return transactionsViewModel
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateHoldingsInUi(holdings: List<Holding>) {
|
private fun updateHoldingsInUi(persistedNewHoldings: List<HoldingEntity>, deletedHoldings: List<HoldingEntity>) {
|
||||||
val allHoldings = uiState.holdings.value.toMutableList()
|
val allHoldings = uiState.holdings.value.toMutableList()
|
||||||
allHoldings.addAll(holdings)
|
allHoldings.removeAll(deletedHoldings)
|
||||||
|
allHoldings.addAll(persistedNewHoldings)
|
||||||
uiState.holdings.value = allHoldings.sortedByDescending { it.pricingTime }
|
uiState.holdings.value = allHoldings.sortedByDescending { it.pricingTime }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ import androidx.compose.material.DrawerValue
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import net.codinux.banking.client.model.securitiesaccount.Holding
|
|
||||||
import net.codinux.banking.client.model.tan.EnterTanResult
|
import net.codinux.banking.client.model.tan.EnterTanResult
|
||||||
import net.codinux.banking.client.model.tan.TanChallenge
|
import net.codinux.banking.client.model.tan.TanChallenge
|
||||||
|
import net.codinux.banking.dataaccess.entities.HoldingEntity
|
||||||
import net.codinux.banking.dataaccess.entities.UserEntity
|
import net.codinux.banking.dataaccess.entities.UserEntity
|
||||||
import net.codinux.banking.ui.model.*
|
import net.codinux.banking.ui.model.*
|
||||||
import net.codinux.banking.ui.model.error.ApplicationError
|
import net.codinux.banking.ui.model.error.ApplicationError
|
||||||
|
@ -22,7 +22,7 @@ class UiState : ViewModel() {
|
||||||
|
|
||||||
val transactions = MutableStateFlow<List<AccountTransactionViewModel>>(emptyList())
|
val transactions = MutableStateFlow<List<AccountTransactionViewModel>>(emptyList())
|
||||||
|
|
||||||
val holdings = MutableStateFlow<List<Holding>>(emptyList())
|
val holdings = MutableStateFlow<List<HoldingEntity>>(emptyList())
|
||||||
|
|
||||||
val transactionsRetrievedEvents = MutableSharedFlow<AccountTransactionsRetrievedEvent>()
|
val transactionsRetrievedEvents = MutableSharedFlow<AccountTransactionsRetrievedEvent>()
|
||||||
|
|
||||||
|
|
|
@ -194,6 +194,22 @@ VALUES(
|
||||||
?, ?
|
?, ?
|
||||||
);
|
);
|
||||||
|
|
||||||
|
updateHolding:
|
||||||
|
UPDATE Holding
|
||||||
|
SET
|
||||||
|
name = ?, isin = ?, wkn = ?,
|
||||||
|
|
||||||
|
quantity = ?, currency = ?,
|
||||||
|
|
||||||
|
totalBalance = ?, marketValue = ?,
|
||||||
|
performancePercentage = ?,
|
||||||
|
|
||||||
|
totalCostPrice = ?, averageCostPrice = ?,
|
||||||
|
|
||||||
|
pricingTime = ?, buyingDate = ?
|
||||||
|
|
||||||
|
WHERE id = ?;
|
||||||
|
|
||||||
|
|
||||||
selectAllHoldings:
|
selectAllHoldings:
|
||||||
SELECT Holding.*
|
SELECT Holding.*
|
||||||
|
|
Loading…
Reference in New Issue