예제 #1
0
        public async Task <BankAccountModel> UpdateBankAccountForCurrentUser(BankAccountModel model)
        {
            var userId = await _authenticationService.GetCurrentUserId();

            using var unitOfWork = _unitOfWork();

            var bankAccount = await unitOfWork.BankAccountRepository.FindSingleTracked(x => x.UserId == userId && x.Id == model.Id);

            if (bankAccount != null)
            {
                var recalculateCurrentBalance = bankAccount.StartBalance != model.StartBalance;

                bankAccount.UpdateFromModel(model);
                unitOfWork.BankAccountRepository.Update(bankAccount);

                if (recalculateCurrentBalance)
                {
                    bankAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, bankAccount);
                }
                await unitOfWork.SaveAsync();

                return(bankAccount.ToDto());
            }
            else
            {
                throw new NotFoundException(nameof(BankAccountEntity));
            }
        }
예제 #2
0
        public async Task DeleteTransactionForCurrentUser(int transactionId)
        {
            var userId = await _authenticationService.GetCurrentUserId();

            using var unitOfWork = _unitOfWork();
            var transaction = await unitOfWork.TransactionRepository.FindSingleTracked(item =>
                                                                                       item.Id == transactionId && item.UserId == userId,
                                                                                       includeProperties : new string[] {
                nameof(TransactionEntity.BankAccount)
            });

            if (transaction == null)
            {
                throw new NotFoundException(nameof(TransactionEntity));
            }

            if (transaction.TransactionCategories != null)
            {
                unitOfWork.TransactionCategoryRepository.DeleteRange(transaction.TransactionCategories);
            }

            unitOfWork.TransactionRepository.Delete(transaction);
            await unitOfWork.SaveAsync();

            transaction.BankAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, transaction.BankAccount);

            await unitOfWork.SaveAsync();
        }
예제 #3
0
        public async Task <TransactionModel> CreateTransactionForCurrentUser(TransactionModel transactionModel)
        {
            var userId = await _authenticationService.GetCurrentUserId();

            using var unitOfWork = _unitOfWork();

            var bankAccount = await unitOfWork.BankAccountRepository.FindSingleTracked(x => x.UserId == userId && x.Id == transactionModel.BankAccountId);

            if (bankAccount == null)
            {
                throw new NotFoundException(nameof(BankAccountEntity));
            }

            var transactionEntity = transactionModel.ToNewEntity(userId);

            unitOfWork.TransactionRepository.Insert(transactionEntity);
            await unitOfWork.SaveAsync();

            // First save the transaction, then recalculate the balance.
            bankAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, bankAccount);

            await unitOfWork.SaveAsync();

            return(transactionEntity.ToDto());
        }
예제 #4
0
        public async Task SeedData(bool overwrite)
        {
            using var unitOfWork = _unitOfWork();

            const string demoUserName = "******";
            var          user         = await unitOfWork.UserRepository.FindSingleTracked(x => x.Username == demoUserName);

            if (user != null)
            {
                if (overwrite)
                {
                    _logger.Information("DemoUser already exists, will delete all existing data for demo user");
                }
                else
                {
                    _logger.Information("DemoUser already exists, overwrite is disabled. Not seeding new data");
                    return;
                }
            }
            else
            {
                _logger.Information("Creating demo user");
                await _authenticationService.CreateUser(demoUserName, demoUserName);

                user = await unitOfWork.UserRepository.FindSingleTracked(x => x.Username == demoUserName);

                await _categorySeed.Value.SeedStandardCategoriesForUser(user.Id);
            }

            await DeleteExistingBankAccounts(unitOfWork, user);

            _logger.Information("Creating demo bank accounts");
            var mainBankAccount      = InsertBankAccount(unitOfWork, user, "Checking 1", BankAccountType.Checking);
            var secondaryBankAccount = InsertBankAccount(unitOfWork, user, "Checking 2", BankAccountType.Checking);
            var savingsAccount       = InsertBankAccount(unitOfWork, user, "Savings", BankAccountType.Savings);
            var investmentAccount    = InsertBankAccount(unitOfWork, user, "Investments", BankAccountType.Investment);

            await DeleteCategoriesAndTransactions(unitOfWork, user);
            await InsertCategoriesAndTransactions(unitOfWork, user, mainBankAccount, savingsAccount);

            await unitOfWork.SaveAsync();

            mainBankAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, mainBankAccount);

            secondaryBankAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, secondaryBankAccount);

            savingsAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, savingsAccount);

            investmentAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, investmentAccount);

            await unitOfWork.SaveAsync();

            _logger.Information("Data seed completed, login with DemoUser/DemoUser");
        }
예제 #5
0
        public async Task <(int skippedTransactions, int savedTransactions)> SaveImport(ImportModel model)
        {
            var userId = await _authenticationService.GetCurrentUserId();

            var importCacheKey = CreateImportCacheKey(userId);
            var cachedModel    = SinanceCacheHandler.RetrieveCache <ImportModel>(importCacheKey);

            if (cachedModel == null)
            {
                throw new NotFoundException(nameof(ImportModel));
            }

            using var unitOfWork = _unitOfWork();
            var bankAccount = await unitOfWork.BankAccountRepository.FindSingleTracked(x => x.Id == model.BankAccountId && x.UserId == userId);

            if (bankAccount == null)
            {
                throw new NotFoundException(nameof(BankAccountEntity));
            }

            var skippedTransactions = model.ImportRows.Count(item => item.ExistsInDatabase || !item.Import);
            var savedTransactions   = await ImportHandler.SaveImportResultToDatabase(unitOfWork,
                                                                                     bankAccountId : bankAccount.Id,
                                                                                     importRows : model.ImportRows,
                                                                                     cachedImportRows : cachedModel.ImportRows,
                                                                                     userId : bankAccount.UserId);

            // Update the current balance of bank account and refresh them
            bankAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork,
                                                                                                        bankAccount);

            await unitOfWork.SaveAsync();

            // Clear the cache entry
            SinanceCacheHandler.ClearCache(importCacheKey);

            return(skippedTransactions, savedTransactions);
        }
예제 #6
0
        public async Task <TransactionModel> UpdateTransactionForCurrentUser(TransactionModel transactionModel)
        {
            var userId = await _authenticationService.GetCurrentUserId();

            using var unitOfWork = _unitOfWork();

            var existingTransaction = await FindTransaction(transactionModel.Id, userId, unitOfWork);

            if (existingTransaction == null)
            {
                throw new NotFoundException(nameof(TransactionEntity));
            }

            existingTransaction.UpdateFromModel(transactionModel);

            unitOfWork.TransactionRepository.Update(existingTransaction);
            await unitOfWork.SaveAsync();

            existingTransaction.BankAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, existingTransaction.BankAccount);

            await unitOfWork.SaveAsync();

            return(existingTransaction.ToDto());
        }