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 And Add New Symbols /// </summary> /// <returns>Success or Fails (True or False)</returns> public bool FetchAndAddNewSymbols() { CurrencyExchangeEntities currencyExchangeEntities = new CurrencyExchangeEntities(); List <string> symbols = new List <string>(); List <string> newSymbols = new List <string>(); try { List <string> existingCurrencySymbols = currencyExchangeEntities.Currencies.Select(x => x.Code.Trim().ToUpper()).ToList(); //Fetching symbols from all new resource types foreach (iDataResource dataSource in _dataSources) { symbols.AddRange(dataSource.FetchNewSymbols()); } if (symbols.Count() > 0) { if (existingCurrencySymbols.Count > 0) { //Fetching Distinct symbols from resources as well as symbols not in DB already newSymbols = symbols.Distinct().Except(existingCurrencySymbols).ToList(); } else { //Fetching Distinct symbols from resources newSymbols = symbols.Distinct().ToList(); } if (newSymbols.Count() > 0) { //Add all the symbols into currency table foreach (var symbol in newSymbols) { Currency currency = new Currency(); currency.Code = symbol.Trim().ToUpper(); currencyExchangeEntities.Currencies.Add(currency);//DB } var dataSources = currencyExchangeEntities.DataSources; //Marking all the datasources as not new foreach (var dataSource in dataSources) { dataSource.IsNew = false; //DB } currencyExchangeEntities.SaveChanges(); //DB Program.logger.Info("New symbols added successfully!"); } } return(true); } catch (Exception ex) { Program.logger.Error("Error while adding new symbols."); Program.logger.Error(ex.Message); return(false); } }
/// <summary> /// To Delete Old Best Rates /// </summary> /// <returns>Success or Fails (True or False)</returns> public bool DeleteOldBestRates() { CurrencyExchangeEntities currencyExchangeEntities = new CurrencyExchangeEntities(); try { //Deleting old best rates currencyExchangeEntities.BestRates.RemoveRange(currencyExchangeEntities.BestRates); //DB currencyExchangeEntities.SaveChanges(); //DB Program.logger.Info("Old best rates deleted successfully!"); return(true); } catch (Exception ex) { Program.logger.Error("Error while deleting old best rates."); Program.logger.Error(ex.Message); return(false); } }
/// <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); }
/// <summary> /// To Fetch And Insert Rates /// </summary> /// <returns>Success or Fails (True or False)</returns> public bool FetchAndInsertRates() { CurrencyExchangeEntities currencyExchangeEntities = new CurrencyExchangeEntities(); try { //Check data is available for today List <ExchangeRate> todayExchangeRates = currencyExchangeEntities.ExchangeRates.Where(x => x.Date == DateTime.Today).ToList(); if (todayExchangeRates.Count == 0) { List <ExchangeRate> exchangeRates = new List <ExchangeRate>(); //Fetching rates from all the recources foreach (iDataResource dataSource in _dataSources) { exchangeRates.AddRange(dataSource.FetchTodayExchangeRates()); } if (exchangeRates.Count() > 0) { //Adding rates from all the recources into DB foreach (var rate in exchangeRates) { ExchangeRate exchangeRate = new ExchangeRate(); exchangeRate.DataSourceId = rate.DataSourceId; exchangeRate.CurrencyId = rate.CurrencyId; exchangeRate.Rate = rate.Rate; exchangeRate.Date = rate.Date; currencyExchangeEntities.ExchangeRates.Add(exchangeRate); //DB } currencyExchangeEntities.SaveChanges(); //DB Program.logger.Info("Today exchange rates added successfully!"); } } return(true); } catch (Exception ex) { Program.logger.Error("Error while adding today rates."); Program.logger.Error(ex.Message); return(false); } }
/// <summary> /// To Find Best Rates And Add /// </summary> /// <returns>Success or Fails (True or False)</returns> public bool FindBestRatesAndAdd() { CurrencyExchangeEntities currencyExchangeEntities = new CurrencyExchangeEntities(); try { var todayBestRates = currencyExchangeEntities.BestRates.Where(x => x.Date == DateTime.Today).ToList(); if (todayBestRates.Count() == 0) { //Finding best rate by comparing them by grouping them by symbol var groupedResult = currencyExchangeEntities.ExchangeRates.Where(x => x.Date == DateTime.Today).GroupBy(x => x.CurrencyId); if (groupedResult.Count() > 0) { foreach (var group in groupedResult) { BestRate bestRate = new BestRate(); foreach (var item in group) { if (item.Rate > bestRate.Rate) { bestRate.Rate = item.Rate; bestRate.CurrencyId = item.CurrencyId; bestRate.DataSourceId = item.DataSourceId; bestRate.Date = item.Date; } } currencyExchangeEntities.BestRates.Add(bestRate); //DB } currencyExchangeEntities.SaveChanges(); //DB Program.logger.Info("Today best rates added successfully!"); } } return(true); } catch (Exception ex) { Program.logger.Error("Error while adding today best rates."); Program.logger.Error(ex.Message); return(false); } }