private static void Flush(CommandLineOptions options, IEnumerable<CurrencyRate> totalResult) { if (options.HasOutputFile) { new FormatterFactory(new FileInfo(options.OutputFile)).Create().Format(totalResult.Select(d=>new ExcelRate(d))); } else { Service.Upsert(totalResult); } }
private static void Download(DateTime fromDate, IList<string> currencies, CommandLineOptions options) { var totalResult = new List<CurrencyRate>((int) ((DateTime.Now.Date - options.FromDate.Date).TotalDays * 50) ); Console.WriteLine("Downloading {1} from {0}... ", options.FromDate.ToString("dd.MM.yyyy"), string.Join(",", currencies)); Console.ForegroundColor = ConsoleColor.White; foreach (var data in LoadData(fromDate, currencies)) { totalResult.AddRange(data); Console.Write("-"); } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("{0}{0}Writing {1} records to database...", Environment.NewLine, totalResult.Count); Flush(options, totalResult); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("{0}Done.", Environment.NewLine); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Yellow; try { var options = new CommandLineOptions(); if (Parser.Default.ParseArguments(args, options)) { if (options.List) { List(); return; } if (!options.HasFromDate) { options.FromDate = DateTime.Now.Date; } Download(options.FromDate, options.Currency, options); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0}{0}ERROR", Environment.NewLine); Console.WriteLine("Name: {0}", ex.GetType().Name); Console.WriteLine("Message: {0}", ex.Message); if (ex.InnerException != null) Console.WriteLine("Inner Message: {0}", ex.InnerException.Message); } finally { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("{0}use -h parameter for more options{0}",Environment.NewLine); } }