예제 #1
0
        public async Task <IActionResult> GetCalculatedExchangeForCurrencies(int amount, string fromCurrency, string toCurrency)
        {
            var    uriString = _apiConnections.UriString;
            string dataFromCurrency;
            string dataToCurrency;

            try
            {
                dataFromCurrency = await _exchange.GetExchangeRatesData(uriString, _apiConnections.RequestUriToSingleRate(fromCurrency));

                dataToCurrency = await _exchange.GetExchangeRatesData(uriString, _apiConnections.RequestUriToSingleRate(toCurrency));
            }
            catch (StatusCodeException e)
            {
                var codeNumber = e.CodeNumber;
                return(StatusCode(codeNumber, StatusCodeResponses.GetResponseMessage(codeNumber)));
            }
            var currencyFrom = _currencyDao.GetCurrency(fromCurrency.ToUpper(), _context, _codesForExchangeRates);
            var currencyTo   = _currencyDao.GetCurrency(toCurrency.ToUpper(), _context, _codesForExchangeRates);

            var conversions = _exchange.GetConversionsDetails(dataFromCurrency, dataToCurrency, amount, currencyFrom, currencyTo);
            await _conversionDao.AddConversions(conversions, _context);

            var actualConversion = $"{amount}{fromCurrency.ToUpper()}:{conversions.Result}{toCurrency.ToUpper()}";
            var currency         = JsonConvert.SerializeObject(actualConversion);

            return(Ok(currency));
        }
예제 #2
0
        public IActionResult GetCodesForCurrencies()
        {
            if (!_codesForExchangeRates.Any())
            {
                try
                {
                    _codesForExchangeRates = _exchangeHelper.LoadCodeCurrencies(_exchange, _apiConnections.UriString,
                                                                                _apiConnections.RequestUriAllRates);
                }
                catch (StatusCodeException e)
                {
                    var codeNumber = e.CodeNumber;
                    return(StatusCode(codeNumber, StatusCodeResponses.GetResponseMessage(codeNumber)));
                }
            }

            var          codes   = JsonConvert.SerializeObject(_codesForExchangeRates.Keys.ToList());
            const string message = "Available code currencies for conversions:\n";

            return(Ok(message + string.Join(",", codes)));
        }
예제 #3
0
        public async Task <IActionResult> GetRatesForCurrencies()
        {
            string exchangeRatesData;

            try
            {
                exchangeRatesData = await _exchange.GetExchangeRatesData(_apiConnections.UriString,
                                                                         _apiConnections.RequestUriAllRates);
            }
            catch (StatusCodeException e)
            {
                var codeNumber = e.CodeNumber;
                return(StatusCode(codeNumber, StatusCodeResponses.GetResponseMessage(codeNumber)));
            }
            var          exchangeRates = _exchange.GetExchangeRates(exchangeRatesData);
            const string message       = "Current exchange rates (currency to PLN):\n";

            var actualRates = _exchangeHelper.GetActualRates(exchangeRates);
            var codes       = JsonConvert.SerializeObject(actualRates);

            return(Ok(message + codes));
        }