public static void FilterRateOutput(this ExchangeRateOutput rateOutput, string[] dates) { rateOutput.Rates = rateOutput.Rates.Where(x => dates.Contains(x.Key)) .ToDictionary(val => val.Key, val => val.Value); // Because it's always goding to be one value var rates = rateOutput.Rates.Values.Select(v => v.Values.FirstOrDefault()); rateOutput.MinRate = rateOutput.Rates.Where(x => x.Value.ContainsValue(rates.Min())) .ToDictionary(k => k.Key, val => val.Value.Values.First()); rateOutput.MaxRate = rateOutput.Rates.Where(x => x.Value.ContainsValue(rates.Max())) .ToDictionary(k => k.Key, val => val.Value.Values.First()); rateOutput.AvgRate = rates.Average(); }
public ActionResult Index() { string stringRates = ""; //Input values from User string currentBase = "SEK"; string currentTarget = "NOK"; string[] dates = new string[] { "2018-02-01", "2018-02-15", "2018-03-01" }; DateTime minDate = DateTime.MaxValue; DateTime maxDate = DateTime.MinValue; foreach (string dateString in dates) { DateTime date = DateTime.Parse(dateString); if (date < minDate) { minDate = date; } if (date > maxDate) { maxDate = date; } } string startDate = minDate.ToString("yyyy-MM-dd"); string endDate = maxDate.ToString("yyyy-MM-dd"); ExchangeRate exchangeRateModel = null; ExchangeRateAPI exchangeRateAPI = new ExchangeRateAPI(); ParseExchangeRateJson parseExchangeRateJSON = new ParseExchangeRateJson(); stringRates = exchangeRateAPI.ExchangeRateApi(startDate, endDate, currentBase, currentTarget); exchangeRateModel = JsonConvert.DeserializeObject <ExchangeRate>(stringRates); ExchangeRateOutput exchangeRateResult = parseExchangeRateJSON.ParseExchangeRateJSON(stringRates, currentBase, currentTarget); exchangeRateAPI = null; return(View(exchangeRateResult)); }
/// <summary> /// Method to fetch required exchange rate information based on input data /// </summary> /// <param name="exchangeRateInput">Exchange rate input model</param> /// <returns>Exchange rate information based on input data</returns> public ExchangeRateOutput GetExchangeRateInformation(ExchangeRateInput exchangeRateInput) { ExchangeRateOutput exchangeRateOutput = new ExchangeRateOutput(); try { if (_httpClient.BaseAddress == null) { ClientInitialSetup(); } List <DateTime> dates = ProcessDate(exchangeRateInput.Dates); string requestURL = PrepareRequestUrl(exchangeRateInput, dates); var response = _httpClient.GetAsync(requestURL); var dataString = response.Result.Content.ReadAsStringAsync(); response.Wait(); if (response.IsCompleted && response.Result.IsSuccessStatusCode) { List <Tuple <double, DateTime> > outputList = ProcessResponse(exchangeRateInput, dates, dataString); exchangeRateOutput = PrepareOutput(outputList); } else { exchangeRateOutput.Error = new ErrorModel { ErrorCode = response.Result.StatusCode, ErrorMessage = dataString.Result.Substring(10).TrimEnd(ExchangeRateConstant.charToRemove) }; } } catch (Exception) { throw; } return(exchangeRateOutput); }
public ExchangeRateOutput ParseExchangeRateJSON(string stringRates, string currentBase, string currentTarget) { int count = 0; decimal sumRate = 0; decimal avgRate = 0; dynamic rates = null; dynamic date = null; dynamic minDate = null; dynamic maxDate = null; dynamic value = null; dynamic min = 0; dynamic max = 0; dynamic data = JObject.Parse(stringRates); dynamic dataRates = data.rates; foreach (var rate in dataRates) { rates = rate.Value; date = rate.Name; foreach (var key in rates) { value = key.Value; if (count == 0) { min = value; max = value; } sumRate += (decimal)value; min = value < min ? value : min; max = value > max ? value : max; if (value == min) { minDate = date; } if (value == max) { maxDate = date; } count++; } } avgRate = sumRate / count; ExchangeRateOutput exchangeRateOutput = new ExchangeRateOutput(); exchangeRateOutput.Minimum = min; exchangeRateOutput.Maximum = max; exchangeRateOutput.Average = avgRate; exchangeRateOutput.BaseCurrency = currentBase; exchangeRateOutput.TargetCurrency = currentTarget; exchangeRateOutput.MinDate = minDate; exchangeRateOutput.MaxDate = maxDate; return(exchangeRateOutput); }