Example #1
0
 public UserTransactionsViewModel(TransactionListDTO transactionsList, Dictionary <string, decimal> amounts)
 {
     this.Transactions    = transactionsList.Transactions;
     this.HasNextPage     = transactionsList.HasNextPage;
     this.HasPreviousPage = transactionsList.HasPreviousPage;
     this.PageCount       = transactionsList.PageCount;
     this.PageNumber      = transactionsList.PageNumber;
     this.PageSize        = transactionsList.PageSize;
     this.TotalItemCount  = transactionsList.TotalItemCount;
     this.IsFirstPage     = transactionsList.IsFirstPage;
     this.IsLastPage      = transactionsList.IsLastPage;
     this.CurrencySymbol  = transactionsList.CurrencySymbol;
     SetDisplayPages();
     this.Amounts = amounts;
 }
        public async Task <TransactionListDTO> GetUserTransactions(string searchByUsername = null, int page = 1, int size = GlobalConstants.DefultPageSize, string sortOrder = GlobalConstants.DefaultTransactionSorting)
        {
            var transactions = this.context
                               .Transactions
                               .Include(transaction => transaction.User)
                               .AsQueryable();

            if (!string.IsNullOrWhiteSpace(searchByUsername))
            {
                transactions = transactions
                               .Where(transaction => transaction.User.UserName.Contains(searchByUsername ?? "", StringComparison.InvariantCultureIgnoreCase));
            }

            var          property = sortOrder.Remove(sortOrder.IndexOf("_"));
            PropertyInfo prop     = typeof(Transaction).GetProperty(property, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);

            if (!sortOrder.Contains("_desc"))
            {
                transactions = transactions
                               .OrderBy(transaction => prop.GetValue(transaction));
            }
            else
            {
                transactions = transactions
                               .OrderByDescending(transaction => prop.GetValue(transaction));
            }

            var currencies = await this.foreignExchangeService.GetConvertionRates();

            var transactionsDTO = await transactions
                                  .Select(transaction => new TransactionDTO()
            {
                Type        = transaction.Type,
                Username    = transaction.User.UserName,
                Amount      = Math.Round(transaction.Amount *currencies.Rates[transaction.Currency.ToString()], 2),
                Description = transaction.Description,
                CreatedOn   = transaction.CreatedOn,
                Currency    = transaction.Currency
            })
                                  .ToPagedListAsync(page, size);

            var transactionsCurrency = "";

            if (transactionsDTO.Count == 0)
            {
                var userWallet = await this.context.Wallets.Where(wallet => wallet.User.UserName == searchByUsername).FirstOrDefaultAsync();

                transactionsCurrency = userWallet.Currency.ToString();
            }
            else
            {
                transactionsCurrency = transactions.First().Currency.ToString();
            }

            var getCurrencySymbol = CultureReferences.CurrencySymbols.TryGetValue(transactionsCurrency, out string currencySymbol);

            if (!getCurrencySymbol)
            {
                throw new EntityNotFoundException("Currency with such ISOCurrencySymbol cannot be found");
            }

            var result = new TransactionListDTO(transactionsDTO);

            result.Currency = transactionsCurrency;

            result.CurrencySymbol = currencySymbol;

            return(result);
        }