public async Task <decimal> ObterUltimaCotacaoMoeda(string siglaMoeda) { var chaveMoeda = ObterChaveMoedaAtual(siglaMoeda); var cache = _cacheManager.ObterChache <MoedaDto>(chaveMoeda); if (cache != null) { return(cache.ValorEmReais); } var client = _clientFactory.CreateClient("exchangeRates"); var response = await client.GetAsync($"latest?base={siglaMoeda}&symbols=BRL"); if (response.StatusCode != System.Net.HttpStatusCode.OK) { return(0M); } var responseBody = JsonSerializer.Deserialize <LatestResult>(await response.Content.ReadAsStringAsync()); var moedaDto = new MoedaDto(siglaMoeda, responseBody.Rates.BRL, responseBody.Date); _cacheManager.GravarCache(chaveMoeda, moedaDto); return(moedaDto.ValorEmReais); }
public Tuple <bool, string, MoedaDto> ConverterRealParaDolar(decimal valor) { if (valor <= 0) { return(new Tuple <bool, string, MoedaDto>(false, "Valor inválido.", null)); } decimal novoValor = valor / dolar; MoedaDto moedaDto = SetMoedaDto("Real", "Dolar", valor, novoValor, 0, dolar); return(new Tuple <bool, string, MoedaDto>(true, string.Empty, moedaDto)); }
public Tuple <bool, string, MoedaDto> ConverterDolarParaEuro(decimal valor) { if (valor <= 0) { return(new Tuple <bool, string, MoedaDto>(false, "Valor inválido.", null)); } decimal novoValor = valor * dolar / euro; MoedaDto moedaDto = SetMoedaDto("Dolar", "Euro", valor, novoValor, dolar, euro); return(new Tuple <bool, string, MoedaDto>(true, string.Empty, moedaDto)); }
public Tuple <bool, string, MoedaDto> ConverterEuroParaReal(decimal valor) { if (valor <= 0) { return(new Tuple <bool, string, MoedaDto>(false, "Valor inválido.", null)); } decimal novoValor = euro * valor; MoedaDto moedaDto = SetMoedaDto("Euro", "Real", valor, novoValor, euro, 0); return(new Tuple <bool, string, MoedaDto>(true, string.Empty, moedaDto)); }
private List <DadosMoedaDto> GetItensRangeDadosMoeda(MoedaDto moeda) { List <DadosMoedaDto> listaDefinitiva = new List <DadosMoedaDto>(); string diretorioArquivos = GetDiretorioArquivos(); using (var reader = new StreamReader(Path.Combine(diretorioArquivos, "DadosMoeda.csv"))) { using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Configuration.Delimiter = ";"; var dadosMoedas = csv.GetRecords <DadosMoedaDto>().ToList(); foreach (var dadoMoeda in dadosMoedas) { if ((dadoMoeda.DATA_REF.CompareTo(moeda.data_inicio) > 0) && (dadoMoeda.DATA_REF.CompareTo(moeda.data_fim) < 0)) { listaDefinitiva.Add(dadoMoeda); } } return(listaDefinitiva); } } }