/// <summary>
        /// Fetches the rates based on historical date
        /// </summary>
        /// <param name="baseCur"></param>
        /// <param name="toCur"></param>
        /// <param name="baseAmount"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public async Task <ExchangeAPIResponse> GetHistoricalRates(string baseCur, string toCur, string baseAmount, string date)
        {
            ExchangeAPIResponse exchangeResponse = new ExchangeAPIResponse();
            CurrencyBase        currencyBase     = new CurrencyBase();

            currencyBase.BaseAmount = _calculationHelper.CheckIfDouble(baseAmount);
            DateTime?checkDate = _calculationHelper.CheckIfDatetime(date);

            if (checkDate != null && !double.IsNaN(currencyBase.BaseAmount))
            {
                currencyBase.BaseCurrency     = baseCur;
                currencyBase.ConvertCurrency  = toCur;
                currencyBase.ExchangeRateDate = Convert.ToDateTime(checkDate);
                APIResponse aPIResponse =
                    await _aPIInteraction.GetCurrencyConversionDataByDate(currencyBase, _configuration.GetValue <string>("URL"),
                                                                          _configuration.GetValue <string>("AccessKey"));

                return(SendLatestRatesResponse(aPIResponse, currencyBase));
            }
            else
            {
                exchangeResponse.IsSuccess   = false;
                exchangeResponse.Description = "Parameters passed are not correct";
                exchangeResponse.StatusCode  = HttpStatusCode.BadRequest;
                return(exchangeResponse);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Method to retrieve data from the WebAPI for Historical Converter and show in graph
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult GetData(HistoricalTimeFrameModel model)
        {
            string uRl = _configuration.GetValue <string>("URL") + "GetHistoricalDataByTimeFrame?toCur=" + model.toCurrency + "&startDate="
                         + model.startDate.ToString("yyyy-MM-dd") + "&endDate=" + model.endDate.ToString("yyyy-MM-dd");
            ExchangeAPIResponse exchangeAPIResponse = GetExchangeAPIResponse(uRl);

            if (exchangeAPIResponse != null && exchangeAPIResponse.IsSuccess)
            {
                var queryGraph = exchangeAPIResponse.ExchangeRatesByDate.Select(a => new { date = a.Key, rate = Convert.ToDouble(a.Value) }).ToList();
                return(Json(queryGraph));
            }
            return(RedirectToAction("Index"));
        }
Beispiel #3
0
        public async Task <ActionResult> GetHistoricalDataByTimeFrame([FromQuery] string toCur, [FromQuery] string startDate, [FromQuery] string endDate)
        {
            if (!string.IsNullOrEmpty(toCur) && !string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                ExchangeAPIResponse exchangeAPIResponse = await _aPIHelperService.GetHistoricalDataByTimeFrame(toCur, startDate, endDate);

                if (exchangeAPIResponse.IsSuccess)
                {
                    return(Ok(exchangeAPIResponse));
                }
            }
            return(BadRequest());
        }
        public async Task <ExchangeAPIResponse> GetHistoricalDataByTimeFrame(string toCur, string startDate, string endDate)
        {
            ExchangeAPIResponse exchangeAPIResponse = new ExchangeAPIResponse();
            DateTime?           checkStartDate      = _calculationHelper.CheckIfDatetime(startDate);
            DateTime?           checkEndDate        = _calculationHelper.CheckIfDatetime(endDate);

            if (checkStartDate != null && checkEndDate != null)
            {
                exchangeAPIResponse.IsSuccess           = true;
                exchangeAPIResponse.StatusCode          = HttpStatusCode.OK;
                exchangeAPIResponse.ExchangeRatesByDate =
                    _exchangeRatesService.GetExchangeRatesByTimeFrame((DateTime)checkStartDate, (DateTime)checkEndDate, toCur);
            }
            return(exchangeAPIResponse);
        }
Beispiel #5
0
 public IActionResult LatestConverter(AppModel appModel)
 {
     if (ModelState.IsValid)
     {
         string uRl = _configuration.GetValue <string>("URL") + "GetLatestData?baseCur=" + appModel.baseCurrency + "&toCur="
                      + appModel.toCurrency + "&baseAmount=" + appModel.baseAmount;
         ExchangeAPIResponse exchangeAPIResponse = GetExchangeAPIResponse(uRl);
         if (exchangeAPIResponse != null && exchangeAPIResponse.IsSuccess)
         {
             appModel.convertedAmount = exchangeAPIResponse.ConvertedAmount;
             TempData["AppModel"]     = JsonConvert.SerializeObject(appModel);
             return(RedirectToAction("ShowConvertedData"));
         }
     }
     return(RedirectToAction("Index"));
 }
        /// <summary>
        /// This method sends the response with the new calculation
        /// </summary>
        /// <param name="aPIResponse"></param>
        /// <param name="currencyBase"></param>
        /// <returns></returns>
        public ExchangeAPIResponse SendLatestRatesResponse(APIResponse aPIResponse, CurrencyBase currencyBase)
        {
            ExchangeAPIResponse exchangeAPIResponse = new ExchangeAPIResponse();

            if (aPIResponse.success)
            {
                exchangeAPIResponse.IsSuccess       = true;
                exchangeAPIResponse.StatusCode      = HttpStatusCode.OK;
                exchangeAPIResponse.ConvertedAmount = _calculationHelper.GetConvertedAmount(aPIResponse.rates, currencyBase);
                exchangeAPIResponse.Date            = (currencyBase.ExchangeRateDate == DateTime.MinValue) ? String.Empty
                    : currencyBase.ExchangeRateDate.ToString("yyyy-MM-dd");
                exchangeAPIResponse.Description = "Successfully found rates";
            }
            else
            {
                exchangeAPIResponse.IsSuccess   = false;
                exchangeAPIResponse.Description = (aPIResponse.error != null)? aPIResponse.error.info: "There was an error processing the data";
                exchangeAPIResponse.StatusCode  = HttpStatusCode.BadRequest;
            }
            return(exchangeAPIResponse);
        }
Beispiel #7
0
        public async Task <ActionResult> GetLatestData([FromQuery] string baseCur, [FromQuery] string toCur, [FromQuery] string baseAmount)
        {
            ExchangeAPIResponse exchangeAPIResponse = new ExchangeAPIResponse();

            if (!string.IsNullOrEmpty(baseCur) && !string.IsNullOrEmpty(toCur) && !string.IsNullOrEmpty(baseAmount))
            {
                exchangeAPIResponse = await _aPIHelperService.GetLatestConversion(baseCur, toCur, baseAmount);

                if (exchangeAPIResponse.IsSuccess)
                {
                    return(Ok(exchangeAPIResponse));
                }
                else
                {
                    return(BadRequest(exchangeAPIResponse));
                }
            }
            else
            {
                return(BadRequest());
            }
        }
        /// <summary>
        /// This method is to fetch latest rates
        /// </summary>
        /// <param name="baseCur"></param>
        /// <param name="toCur"></param>
        /// <param name="baseAmount"></param>
        /// <returns></returns>
        public async Task <ExchangeAPIResponse> GetLatestConversion(string baseCur, string toCur, string baseAmount)
        {
            ExchangeAPIResponse exchangeResponse = new ExchangeAPIResponse();
            CurrencyBase        currencyBase     = new CurrencyBase();

            currencyBase.BaseAmount = _calculationHelper.CheckIfDouble(baseAmount);
            if (!double.IsNaN(currencyBase.BaseAmount))
            {
                currencyBase.BaseCurrency    = baseCur;
                currencyBase.ConvertCurrency = toCur;
                APIResponse aPIResponse =
                    await _aPIInteraction.GetCurrencyConversionData(currencyBase, _configuration.GetValue <string>("URL"),
                                                                    _configuration.GetValue <string>("AccessKey"));

                return(SendLatestRatesResponse(aPIResponse, currencyBase));
            }
            else
            {
                exchangeResponse.IsSuccess   = false;
                exchangeResponse.Description = "Base Amount is not a valid number";
                exchangeResponse.StatusCode  = HttpStatusCode.BadRequest;
                return(exchangeResponse);
            }
        }