/// <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); }
/// <summary> /// Parse the api json and return the model /// </summary> /// <param name="exchangeRatesHistoryData"></param> /// <param name="inputModel"></param> /// <returns></returns> public List <ExchangeRatesHistoryModel> ParseExchangeRatesHistory(string exchangeRatesHistoryData, ExchangeRatesInputModel inputModel) { var exchangeRatesHistory = JObject.Parse(exchangeRatesHistoryData).Value <JObject>("rates"); List <ExchangeRatesHistoryModel> exchangeRatesHistoryResult = new List <ExchangeRatesHistoryModel>(); foreach (var rateHistory in exchangeRatesHistory) { if (rateHistory.Value.HasValues) { var exchangeRatesDate = DateTime.Parse(rateHistory.Key); if (inputModel.ExchangeRatesDates.Any(d => d.ExchangeRatesDate == exchangeRatesDate)) { exchangeRatesHistoryResult.Add(new ExchangeRatesHistoryModel { ExchangeRatesDate = exchangeRatesDate, BaseCurrenyRate = rateHistory.Value[inputModel.BaseCurrency].Value <float>(), TargetCurrenyRate = rateHistory.Value[inputModel.TargetCurrency].Value <float>() }); } } } return(exchangeRatesHistoryResult); }
/// <summary> /// Order according to ExchangeRatesDates /// </summary> /// <param name="inputModel"></param> /// <returns></returns> public ExchangeRatesInputModel OrderInputDates(ExchangeRatesInputModel inputModel) { inputModel.ExchangeRatesDates = inputModel.ExchangeRatesDates.OrderBy(d => d.ExchangeRatesDate).ToList(); return(inputModel); }