public async Task <IActionResult> GetExchangeRates([FromQuery] ExchangeRatesQueryInputModel queryInputModel)
        {
            try
            {
                //Validate model
                if (!ModelState.IsValid)
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, ModelState.Values.SelectMany(x => x.Errors)));
                }
                //Map from query string values and split the values
                var inputModel = _helper.Mapper(queryInputModel);
                //validate if date input are correct
                if (inputModel.ExchangeRatesDates.Count < 2)
                {
                    ModelState.AddModelError(nameof(inputModel.ExchangeRatesDates), "ExchangeRatesDates must have at least two date values");
                    return(StatusCode(StatusCodes.Status400BadRequest, ModelState.Values.SelectMany(x => x.Errors)));
                }

                //Order the input into asc order
                inputModel = _helper.OrderInputDates(inputModel);

                //Get the end point
                var endPointUrl = _helper.GetEndPointUrl("history", inputModel.BaseCurrency, inputModel.TargetCurrency, inputModel.ExchangeRatesDates.First().ExchangeRatesDate, inputModel.ExchangeRatesDates.Last().ExchangeRatesDate);
                //Create http client
                var client = _clientFactory.CreateClient("ExchangeRates");
                //Make a request and get response
                var apiResponse = await client.GetAsync(endPointUrl);

                //Validate the response
                if (apiResponse.IsSuccessStatusCode)
                {
                    var responseString = await apiResponse.Content.ReadAsStringAsync();

                    //If empty response then return no content
                    if (responseString.Length <= 0)
                    {
                        return(StatusCode(StatusCodes.Status204NoContent));
                    }
                    //Parse the json api response into class model
                    var exchangeRatesHistoryResult = _helper.ParseExchangeRatesHistory(responseString, inputModel);
                    //Create the response that we want to return
                    var response = _helper.CreateExchangeRatesResponse(exchangeRatesHistoryResult);
                    //return the response
                    return(StatusCode(StatusCodes.Status200OK, response));
                }
                //If not valid response , return the error
                return(StatusCode((int)apiResponse.StatusCode, apiResponse.ReasonPhrase));
            }
            catch (Exception ex)
            {
                //return if any exception
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Map and convert comma seprated values into dates
        /// </summary>
        /// <param name="fromModel"></param>
        /// <returns></returns>
        public ExchangeRatesInputModel Mapper(ExchangeRatesQueryInputModel fromModel)
        {
            var toModel = new ExchangeRatesInputModel
            {
                BaseCurrency   = fromModel.BaseCurrency,
                TargetCurrency = fromModel.TargetCurrency
            };
            var splitExchangeRatesDates = fromModel.ExchangeRatesDates.Split(",");

            foreach (var ratesDate in splitExchangeRatesDates)
            {
                if (DateTime.TryParse(ratesDate, out DateTime ratesDateResult))
                {
                    toModel.ExchangeRatesDates.Add(new ExchangeRatesDatesInputModel
                    {
                        ExchangeRatesDate = ratesDateResult
                    });
                }
            }
            return(toModel);
        }