From 5029a2c3cbeb36a46b34207af3470461eeb18952 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 12 Sep 2024 13:56:08 +0200 Subject: [PATCH] Implemented updating holdings --- .../dataaccess/SqliteBankingRepository.kt | 17 ++++++++- .../banking/ui/service/BankingService.kt | 38 ++++++++++++++----- .../net/codinux/banking/ui/state/UiState.kt | 4 +- .../codinux/banking/ui/AccountTransaction.sq | 16 ++++++++ 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt index a2fd7ad..da72ea3 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt @@ -238,7 +238,22 @@ open class SqliteBankingRepository( } override suspend fun updateHoldings(holdings: List) { - // 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) { diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt index 304ee0b..e968ff9 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt @@ -15,9 +15,7 @@ import net.codinux.banking.client.model.response.* import net.codinux.banking.client.model.securitiesaccount.Holding import net.codinux.banking.client.service.BankingModelService import net.codinux.banking.dataaccess.BankingRepository -import net.codinux.banking.dataaccess.entities.AccountTransactionEntity -import net.codinux.banking.dataaccess.entities.BankAccountEntity -import net.codinux.banking.dataaccess.entities.UserEntity +import net.codinux.banking.dataaccess.entities.* import net.codinux.banking.ui.IOorDefault import net.codinux.banking.ui.model.AccountTransactionViewModel import net.codinux.banking.ui.model.BankInfo @@ -50,6 +48,7 @@ class BankingService( uiState.users.value = getAllUsers() uiState.transactions.value = getAllAccountTransactionsAsViewModel() + uiState.holdings.value = uiState.users.value.flatMap { it.accounts }.flatMap { it.holdings } } catch (e: Throwable) { log.error(e) { "Could not read all user accounts and account transactions from repository" } } @@ -114,7 +113,7 @@ class BankingService( uiState.users.value = users updateTransactionsInUi(newUserEntity.accounts.flatMap { it.bookedTransactionsEntities }) - updateHoldingsInUi(newUserEntity.accounts.flatMap { it.holdings }) + updateHoldingsInUi(newUserEntity.accounts.flatMap { it.holdings }, emptyList()) } catch (e: Throwable) { 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 persistedNewHoldings = bankingRepository.persistHoldings(account, newHoldings) -// bankingRepository.updateHoldings(updatedExistingHoldings) // TODO: map data of updatedRetrievedHoldings to updatedExistingHoldings + bankingRepository.updateHoldings(updateHoldings(updatedExistingHoldings, updatedRetrievedHoldings)) bankingRepository.deleteHoldings(deletedHoldings) account.holdings = account.holdings.toMutableList().apply { @@ -187,8 +186,7 @@ class BankingService( removeAll(deletedHoldings) } - - updateHoldingsInUi(response.holdings) + updateHoldingsInUi(persistedNewHoldings, deletedHoldings) val transactionsViewModel = updateTransactionsInUi(newTransactionsEntities) uiState.dispatchNewTransactionsRetrieved(AccountTransactionsRetrievedEvent(user, account, transactionsViewModel, response.holdings)) @@ -198,6 +196,27 @@ class BankingService( } } + private fun updateHoldings(updatedExistingHoldings: List, updatedRetrievedHoldings: List): List = + 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): List { val transactionsViewModel = addedTransactions.map { AccountTransactionViewModel(it) } @@ -208,9 +227,10 @@ class BankingService( return transactionsViewModel } - private fun updateHoldingsInUi(holdings: List) { + private fun updateHoldingsInUi(persistedNewHoldings: List, deletedHoldings: List) { val allHoldings = uiState.holdings.value.toMutableList() - allHoldings.addAll(holdings) + allHoldings.removeAll(deletedHoldings) + allHoldings.addAll(persistedNewHoldings) uiState.holdings.value = allHoldings.sortedByDescending { it.pricingTime } } diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/state/UiState.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/state/UiState.kt index aa2f2ab..09c5931 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/state/UiState.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/state/UiState.kt @@ -5,9 +5,9 @@ import androidx.compose.material.DrawerValue import androidx.lifecycle.ViewModel import kotlinx.coroutines.flow.MutableSharedFlow 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.TanChallenge +import net.codinux.banking.dataaccess.entities.HoldingEntity import net.codinux.banking.dataaccess.entities.UserEntity import net.codinux.banking.ui.model.* import net.codinux.banking.ui.model.error.ApplicationError @@ -22,7 +22,7 @@ class UiState : ViewModel() { val transactions = MutableStateFlow>(emptyList()) - val holdings = MutableStateFlow>(emptyList()) + val holdings = MutableStateFlow>(emptyList()) val transactionsRetrievedEvents = MutableSharedFlow() diff --git a/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/AccountTransaction.sq b/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/AccountTransaction.sq index d6fe0f8..4ecffbe 100644 --- a/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/AccountTransaction.sq +++ b/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/AccountTransaction.sq @@ -194,6 +194,22 @@ VALUES( ?, ? ); +updateHolding: +UPDATE Holding +SET + name = ?, isin = ?, wkn = ?, + + quantity = ?, currency = ?, + + totalBalance = ?, marketValue = ?, + performancePercentage = ?, + + totalCostPrice = ?, averageCostPrice = ?, + + pricingTime = ?, buyingDate = ? + +WHERE id = ?; + selectAllHoldings: SELECT Holding.*