private async Task FillRequiredViewBagData()
        {
            ViewBag.Transactions = await _transactions.GetByRange(User.Identity.Name, DateTime.Today, DateTime.Today.AddDays(1));

            ViewBag.Currencies = await _currencies.GetAll();

            ViewBag.Categories = await _categories.GetAll();

            ViewBag.TransactionTypes = await _transactionTypes.GetAll();
        }
예제 #2
0
        public async Task <Dictionary <string, ExchangeValue> > DownloadExchangeRatesAsync()
        {
            using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
            {
                Dictionary <string, ExchangeValue> exchangeRates = null;

                var url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms={0}&tsyms=USD";

                var supportedCurrenciesQueryable = _supporteCurrenciesRepository.GetAll();

                var currenciesQueryable = _currenciesRepository.GetAll();

                var queryable = supportedCurrenciesQueryable.Join(currenciesQueryable, sc => sc.CurrencyId, c => c.Id, (sc, c) =>
                                                                  new { SupportCurrency = sc, Currency = c }).Where(o => o.Currency.IsCrypto);

                var currencies = string.Join(",", await queryable.Select(o => o.Currency.Code).ToListAsync());

                if (string.IsNullOrEmpty(currencies))
                {
                    return(new Dictionary <string, ExchangeValue>());
                }

                try
                {
                    url = string.Format(url, currencies);

                    using (var httpClient = new HttpClient())
                    {
                        using (var httpResponse = await httpClient.GetAsync(url))
                        {
                            if (httpResponse.StatusCode != HttpStatusCode.OK)
                            {
                                throw new Exception($"API method: {url} returned status code: {httpResponse.StatusCode}.");
                            }

                            var jsonString = await httpResponse.Content.ReadAsStringAsync();

                            if (string.IsNullOrEmpty(jsonString))
                            {
                                throw new Exception($"API method: {url} returned empty json.");
                            }

                            exchangeRates = JsonConvert.DeserializeObject <Dictionary <string, ExchangeValue> >(jsonString);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(String.Empty, ex);
                }

                return(exchangeRates ?? new Dictionary <string, ExchangeValue>());
            }
        }
예제 #3
0
        public async Task <List <ExchageRateDto> > GetBtcEthValue()
        {
            //temp need remove
            var usdCurrency = await _currenciesRepository.FirstOrDefaultAsync(c => c.Code.Equals("USD"));

            var supportedCurrency = await _supportedCurrencyRepository.FirstOrDefaultAsync(c => c.CurrencyId.Equals(usdCurrency.Id));

            if (supportedCurrency == null)
            {
                supportedCurrency = new SupportedCurrency
                {
                    CreationTime = DateTime.Now.ToUniversalTime(),
                    CurrencyId   = usdCurrency.Id,
                    TenantId     = AbpSession.TenantId,
                };

                await _supportedCurrencyRepository.InsertAsync(supportedCurrency);

                var exchangeRate = await _exchangeRateRepository.FirstOrDefaultAsync(er => er.CurrencyId.Equals(supportedCurrency.CurrencyId));

                if (exchangeRate == null)
                {
                    exchangeRate = new ExchangeRate {
                        CurrencyId = usdCurrency.Id
                    };
                    exchangeRate.CreationTime      = DateTime.Now.ToUniversalTime();
                    exchangeRate.Price             = 0.25M;
                    exchangeRate.IsAutomaticUpdate = true;
                    exchangeRate.UpdatedBy         = null;

                    await _exchangeRateRepository.InsertAndGetIdAsync(exchangeRate);
                }
            }
            ////

            var supportedCurrenciesQueryable = _supportedCurrencyRepository.GetAll();

            var exchangeRatesQueryable = _exchangeRateRepository.GetAll();

            var currenciesQueryable = _currenciesRepository.GetAll();

            var queryable = supportedCurrenciesQueryable.Join(exchangeRatesQueryable, er => er.CurrencyId, sc => sc.CurrencyId, (sc, er) =>
                                                              new { ExchangeRate = er, SupportedCurrency = sc }).Join(currenciesQueryable, o => o.SupportedCurrency.CurrencyId, c => c.Id, (o, c) =>
                                                                                                                      new { ExchangeRate = o.ExchangeRate, Currency = c });

            return(await queryable.Select(o =>
                                          new ExchageRateDto
            {
                Id = o.ExchangeRate.Id,
                Currency = ObjectMapper.Map <CurrencyDto>(o.Currency),
                Price = o.ExchangeRate.Price,
                IsAutoWallet = o.ExchangeRate.IsAutoWallet
            }).ToListAsync());
        }
        public async Task UpdateCryptoCurrenciesAsync()
        {
            using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
            {
                try
                {
                    var supportedCurrenciesQueryable = _supportedCurrenciesRepository.GetAll();

                    var currenciesQueryable = _currenciesRepository.GetAll();

                    var supportedCurrencies = supportedCurrenciesQueryable.Join(currenciesQueryable, sc => sc.CurrencyId, c => c.Id, (sc, c) =>
                                                                                new { SupportCurrency = sc, Currency = c }).Where(o => o.Currency.IsCrypto).ToList();

                    var cryptoCurrencies = await DownloadCryptoCurrenciesAsync();

                    foreach (var obj in supportedCurrencies)
                    {
                        var cryptoCurrency = cryptoCurrencies.FirstOrDefault(cc => cc.Code.Equals(obj.Currency.Code));

                        if (cryptoCurrency != null)
                        {
                            cryptoCurrencies.Remove(cryptoCurrency);
                        }
                    }

                    if (cryptoCurrencies.Count == 0)//TODO: need ask about that case
                    {
                        return;
                    }

                    var currencies = cryptoCurrencies.Select(c => CurrencyMapper(c)).ToList();

                    //TODO: add symbols download

                    await _currenciesRepository.DeleteNotSupportedCryptoCurrenciesAsync();

                    await _currenciesRepository.InsertRangeAsync(currencies);
                }
                catch (Exception ex)
                {
                    Logger.Error(String.Empty, ex);
                }
            }
        }
예제 #5
0
        public async Task <PagedResultDto <TransactionDto> > GetAllTransactions(GetTransactionInput input)
        {
            try
            {
                var user = await _userRepository.FirstOrDefaultAsync(item => item.Id == AbpSession.UserId);

                var role = await _userManager.GetRolesAsync(user);

                IQueryable <Transaction> query;
                if (role.Contains("Admin"))
                {
                    query = _transactionRepository.GetAll()
                            .WhereIf(
                        !input.WalletId.IsNullOrWhiteSpace(),
                        t =>
                        t.WalletId.Contains(input.WalletId)
                        ).WhereIf(input.IsRescind == false, item => item.TransactionStatus != TransactionStatusEnum.Rescind);
                }
                else
                {
                    query = _transactionRepository.GetAll()
                            .WhereIf(
                        !input.WalletId.IsNullOrWhiteSpace(),
                        t =>
                        t.WalletId.Contains(input.WalletId)
                        )
                            .Where(item => item.UserId == user.Id)
                            .WhereIf(input.IsRescind == false, item => item.TransactionStatus != TransactionStatusEnum.Rescind);
                }

                var currenciesQueryable = _currenciesRepository.GetAll();

                var a = query.Join(currenciesQueryable, t => t.CurrencyId, c => c.Id, (t, c) =>
                                   new { Transaction = t, Currency = c }).Select(o => Mapper(o.Transaction, o.Currency));

                var transactionsCount = await a.CountAsync();

                var transactions = new List <TransactionDto>();
                if (input.Sorting == string.Empty)
                {
                    transactions = await a
                                   .OrderByDescending(item => item.Id)
                                   .PageBy(input)
                                   .ToListAsync();
                }
                else
                {
                    transactions = await a
                                   .OrderBy(input.Sorting)
                                   .PageBy(input)
                                   .ToListAsync();
                }

                //temp
                //var transactionListDtos = ObjectMapper.Map<List<TransactionDto>>(transactions);

                return(new PagedResultDto <TransactionDto>(
                           transactionsCount,
                           transactions
                           ));
            }
            catch (Exception ex)
            {
                throw new UserFriendlyException(ex.Message);
            }
        }
예제 #6
0
 public List <Currency> GetAll()
 {
     return(_currenciesRepository.GetAll(false).ToList());
 }
예제 #7
0
        public async Task <IActionResult> Index(DateTime startDate, DateTime endDate, int currencyId)
        {
            if (startDate == default(DateTime) || endDate == default(DateTime) || currencyId == default(int))
            {
                startDate  = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                endDate    = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
                currencyId = (int)Enums.Currency.BYN;
            }

            ViewBag.Currencies = await _currencies.GetAll();


            if (endDate < startDate ||
                endDate > startDate.AddDays(30))
            {
                ViewBag.HasRangeError = true;
                return(View("Index"));
            }
            else
            {
                ViewBag.HasRangeError = false;
            }

            ViewBag.Transactions = await _transactions.GetByRange(User.Identity.Name, startDate, endDate);

            var transactions    = (IEnumerable <Transactions>)ViewBag.Transactions;
            var currencies      = (IEnumerable <Currency>)ViewBag.Currencies;
            var currentCurrency = currencies.FirstOrDefault(c => c.Id == currencyId)?.Abbreviation;

            var dateRange = startDate.ListAllDates(endDate);

            ViewBag.Labels = dateRange.Select(d => d.ToShortDateString()).ToArray();

            var recalculatedTransactions = transactions.Select(t => {
                return(new Transactions
                {
                    Amount = (decimal)CurrencyConvertingHelper.Convert(t.Currency.Abbreviation.ToCurrency(), currentCurrency.ToCurrency(), (double)t.Amount),
                    Categories = t.Categories,
                    CurrencyId = currencyId,
                    Description = t.Description,
                    TransactionTypeId = t.TransactionTypeId,
                    TransactionType = t.TransactionType,
                    Date = t.Date,
                    Id = t.Id
                });
            }).OrderBy(tr => tr.Date).ToList();

            ViewBag.Transactions = recalculatedTransactions;

            var incomeTransactions  = recalculatedTransactions.Where(t => t.TransactionTypeId == 1);
            var outcomeTransactions = recalculatedTransactions.Where(t => t.TransactionTypeId == 2);

            List <double> incomeDataSet  = new List <double>();
            List <double> outcomeDataSet = new List <double>();

            foreach (var date in dateRange)
            {
                var incomeTransaction  = incomeTransactions.Where(t => t.Date == date);
                var outcomeTransaction = outcomeTransactions.Where(t => t.Date == date);
                if (incomeTransaction.Any())
                {
                    incomeDataSet.Add(decimal.ToDouble(incomeTransaction.Sum(t => t.Amount)));
                }
                else
                {
                    incomeDataSet.Add(0);
                }

                if (outcomeTransaction.Any())
                {
                    outcomeDataSet.Add(decimal.ToDouble(outcomeTransaction.Sum(t => t.Amount)));
                }
                else
                {
                    outcomeDataSet.Add(0);
                }
            }

            ViewBag.IncomeDataSet  = incomeDataSet;
            ViewBag.OutcomeDataSet = outcomeDataSet;

            ViewBag.CurrentCurrency = currentCurrency;
            ViewBag.TotalIncome     = incomeDataSet.Sum(x => x);
            ViewBag.TotalOutCome    = outcomeDataSet.Sum(x => x);
            return(View());
        }
 public IEnumerable <Currency> GetAllByMethod([FromRoute] string method = "none")
 {
     return(repository.GetAll(method));
 }