Exemplo n.º 1
0
        public async Task <BaseResponse <List <ExchangeDto> > > Convert(decimal sum, string date)
        {
            _logger.LogInformation($"ConvertSum is invoked.");
            try
            {
                if (!DateTime.TryParseExact(date, "dd.MM.yyyy", null, System.Globalization.DateTimeStyles.None, out DateTime dateValue))
                {
                    throw new ArgumentException($"Параметр '{nameof(date)}' не содержит дату. Значение параметра: '{date}'", nameof(date));
                }

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

                foreach (var currency in _currencies)
                {
                    ExchangeDto exchangeDto = new ExchangeDto()
                    {
                        CurrencyCode  = currency.Code,
                        CurrencyTitle = currency.Title,
                    };
                    result.Add(exchangeDto);

                    try
                    {
                        ICurrencyRateService service = _currencyServices[currency.ServiceName];
                        decimal convertedSum         = await service.ConvertSum(sum, dateValue);

                        exchangeDto.Sum = convertedSum;
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.ToString());
                        exchangeDto.IsSuccess = false;
                        exchangeDto.Error     = ex.Message;
                    }
                }
                return(new BaseResponse <List <ExchangeDto> >(result));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(new BaseResponse <List <ExchangeDto> >(ex));
            }
        }