Ejemplo n.º 1
0
        public async Task <List <BalancePrimaryCurrencyModel> > GetUserPrimaryCurrencyBalancesAsync(Guid userId)
        {
            UserCurrency userCurrency = await _userCurrencyRepository.GetPrimaryCurrencyAsync(userId);

            IEnumerable <Balance> balances = await _balanceRepository.GetLatestBalancesAsync(userId);

            List <BalancePrimaryCurrencyModel> result = new List <BalancePrimaryCurrencyModel>();

            if (balances.Count() == 0)
            {
                return(result);
            }

            DateTime effectiveDate = balances.First().EffectiveDate;
            List <ExchangeRateModel> exchangeRates = await _exchangeRateService.GetExchangeRatesAsync(userId, effectiveDate);

            foreach (Balance balance in balances)
            {
                ExchangeRateModel rate = exchangeRates.First(r =>
                                                             r.BaseCurrency.Equals(balance.Asset.Balance.Currency, StringComparison.OrdinalIgnoreCase) &&
                                                             r.CounterCurrency.Equals(userCurrency.Currency, StringComparison.OrdinalIgnoreCase));

                result.Add(new BalancePrimaryCurrencyModel(userCurrency.Currency, balance, rate));
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <BalanceTotalModel> CalculateAsync(
            Guid userId,
            IEnumerable <Balance> balances,
            string currency,
            string primaryCurrency,
            DateTime effectiveDate)
        {
            decimal total = 0.0M;
            List <ExchangeRateModel> rates = await _exchangeRateService.GetExchangeRatesAsync(userId, effectiveDate);

            var groups = balances.GroupBy(b => b.Asset.Balance.Currency);

            foreach (IGrouping <string, Balance> g in groups)
            {
                decimal currencyTotal = g.Sum(b => b.Value);

                if (string.Equals(currency, g.Key, StringComparison.OrdinalIgnoreCase))
                {
                    total += currencyTotal;
                }
                else
                {
                    decimal primaryCurrencyValue = BuyPrimaryCurrency(rates, g.Key, primaryCurrency, currencyTotal);
                    decimal currencyValue        = SellPrimaryCurrency(rates, currency, primaryCurrency, primaryCurrencyValue);

                    total += currencyValue;
                }
            }

            return(new BalanceTotalModel(effectiveDate, currency, total));
        }
        public async Task <CurrencyResponse> ConvertAsync(CurrencyRequest currencyRequest)
        {
            if (currencyRequest == null || string.IsNullOrEmpty(currencyRequest.SourceCurrency) ||
                string.IsNullOrEmpty(currencyRequest.TargetCurrency))
            {
                return(null);
            }

            currencyRequest.SourceCurrency = currencyRequest.SourceCurrency.ToUpper();
            currencyRequest.TargetCurrency = currencyRequest.TargetCurrency.ToUpper();

            if (CheckIfCurrenciesAreSupported(currencyRequest))
            {
                return(null);
            }

            var response = await _exchangeRateService.GetExchangeRatesAsync(currencyRequest.SourceCurrency);

            if (response == null)
            {
                return(null);
            }

            var targetRate = response.Rates[currencyRequest.TargetCurrency];

            var convertedAmount = currencyRequest.Amount * Math.Round(targetRate, 2);

            return(new CurrencyResponse
            {
                ConvertedAmount = convertedAmount,
                TargetCurrency = currencyRequest.TargetCurrency
            });
        }
Ejemplo n.º 4
0
        public async Task <IList <CurrencyReturnDTO> > GetCurrenciesAsync()
        {
            var exchanges = await _exchangeRateService.GetExchangeRatesAsync();

            var currencies =
                (await _currenciesRepository.GetAllAsync()).Select(currency =>
                                                                   currency.ToCurrencyReturnDTO(exchanges.CurrencyRates
                                                                                                .FirstOrDefault(rate => rate.Currency == currency).Rate));


            return(await currencies.ToArrayAsync());
        }
Ejemplo n.º 5
0
        public async Task<IActionResult> GetExchangeRates()
        {
            try
            {
                var exchangeRates = await _exchangeRateService.GetExchangeRatesAsync(new List<string>() { "USD", "JMD" });

                return Ok(exchangeRates);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <List <ExchangeRateModel> > > GetExchangeRates(DateTime effectiveDate)
        {
            UserViewModel user = await GetCurrentUserAsync();

            if (user == null)
            {
                return(HandleUserNotFoundResult());
            }

            List <ExchangeRateModel> result = await _exchangeRateService.GetExchangeRatesAsync(user.Id, effectiveDate);

            return(result);
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> GetExhangeRates()
        {
            try
            {
                var exchangeRates = await _exchangeRateService.GetExchangeRatesAsync();

                return(Ok(exchangeRates));
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex.Message);

                return(BadRequest(new ErrorViewModel {
                    Error = ErrorCode.ServerError, ErrorDescription = ex.Message
                }));
            }
        }