static void Main(string[] args) { //NLog setup NLog.LogManager.LoadConfiguration("NLog.config"); Logger logger = LogManager.GetCurrentClassLogger(); logger.Info(""); logger.Info("Process Started"); CurrencyExchangeEntities currencyExchangeEntities = new CurrencyExchangeEntities(); try { var bestRates = currencyExchangeEntities.BestRates.FirstOrDefault(); List <iDataResource> dataSources = new List <iDataResource>(); dataSources.Add(new RateSource()); dataSources.Add(new QuoteSource()); //Add more datasources if any in the above list //If no data (may be first time) then fetch and add ExchangeRateProcessor dataExchangeProcessor = new ExchangeRateProcessor(dataSources); bool isDone = true; //If old best rates exists then delete them if (bestRates != null && bestRates.Date != DateTime.Today) { isDone = dataExchangeProcessor.DeleteOldBestRates(); } //If no more bestrates then process if (isDone && bestRates == null) { //Add new symbols from new resources isDone = dataExchangeProcessor.FetchAndAddNewSymbols(); if (isDone) { //Add today rates from all resources isDone = dataExchangeProcessor.FetchAndInsertRates(); if (isDone) { //Find and add new best rates isDone = dataExchangeProcessor.FindBestRatesAndAdd(); if (isDone) { logger.Info("Process Completed Successfully!"); } } } } else { logger.Info("Data is uptodate!"); logger.Info("Process Completed Successfully!"); } } catch (Exception ex) { logger.Error("Error while doing the process."); logger.Info(ex.Message); logger.Error("Process not completed."); } }
/// <summary> /// To fetch new symbols from new resources /// </summary> /// <returns>New symbols</returns> public List <string> FetchNewSymbols() { List <string> newSymbols = new List <string>(); CurrencyExchangeEntities currencyExchangeEntities = new CurrencyExchangeEntities(); try { List <string> oldSymbols = currencyExchangeEntities.Currencies.Select(x => x.Code).ToList(); //Fetching Quote resource data var dataSources = currencyExchangeEntities.DataSources.Where(x => x.IsNew == true && x.DataElementName == _dataElementName); foreach (var dataSource in dataSources) { //Fetching data from resource QuoteSource currencyQuotes = ExchangeRateProcessor.GetDataFromSource <QuoteSource>(dataSource.Url); if (currencyQuotes.Quotes == null) { Program.logger.Error("Unable to fetch data from resource Url."); return(newSymbols); } else { //Processing and taking symbols alone List <string> CurrencySymbols = currencyQuotes.Quotes.Select(x => x.Key.Replace("USD", "").Trim().ToUpper()).ToList(); newSymbols.AddRange(CurrencySymbols); } } Program.logger.Info("QuoteSource: New symbols fetched succesfully!"); } catch (Exception ex) { Program.logger.Error("QuoteSource: Error while fetching new symbols."); Program.logger.Error(ex.Message); } return(newSymbols); }
/// <summary> /// To Fetch Today Exchange Rates /// </summary> /// <returns>Today Exchange Rates</returns> public List <ExchangeRate> FetchTodayExchangeRates() { CurrencyExchangeEntities currencyExchangeEntities = new CurrencyExchangeEntities(); List <ExchangeRate> todayExchangeRates = new List <ExchangeRate>(); try { //Fetching Quote resource data var dataSources = currencyExchangeEntities.DataSources.Where(x => x.DataElementName.Trim().ToUpper() == _dataElementName); foreach (var datasource in dataSources) { //Fetching data from resource RateSource currencyRates = ExchangeRateProcessor.GetDataFromSource <RateSource>(datasource.Url); if (currencyRates.Rates == null) { Program.logger.Error("Unable fetch data from resource Url."); return(todayExchangeRates); } else { //Processing and taking rates foreach (var rate in currencyRates.Rates) { ExchangeRate exchangeRate = new ExchangeRate(); exchangeRate.DataSourceId = datasource.Id; //Fetching corresponding symbol code from DB var cid = currencyExchangeEntities.Currencies.Where(x => x.Code == rate.Key.Trim().ToUpper()).FirstOrDefault(); exchangeRate.CurrencyId = cid.Id; exchangeRate.Rate = rate.Value; exchangeRate.Date = DateTime.Today; todayExchangeRates.Add(exchangeRate); } } } Program.logger.Info("RateSource: Today exchange rates fetched successfully!"); } catch (Exception ex) { Program.logger.Error("RateSource: Error while fetch today exchange rates."); Program.logger.Error(ex.Message); } return(todayExchangeRates); }