Beispiel #1
0
        public static int Main(string[] args)
        {
            CultureInfo.CurrentCulture = new CultureInfo("pl-PL", true);
            ICollection <RequestedAction> req_actions;

            try {
                req_actions = ParseArgs(args);
            }
            catch (ArgumentException e) {
                Console.WriteLine(e.Message);
                return(1);
            }

            if (req_actions == null)
            {
                return(1);
            }
            else if (req_actions.Count == 0)
            {
                Console.WriteLine("Nothing to do. See help with 'help' option.");
            }

            var context = new RatesHistory();

            context.Database.EnsureCreated();

            foreach (var action in req_actions)
            {
                // Console.WriteLine(action);
                Act(action, context);
            }

            return(0);
        }
Beispiel #2
0
 public static RatesRecord GetRatesRecordFromHistory(RatesHistory context, DateTime date)
 {
     try {
         return(context.RatesRecords.Where(r => r.Date == date.Date).First());
     }
     catch (System.InvalidOperationException) {
         return(null);
     }
 }
Beispiel #3
0
        public static void Act(RequestedAction action, RatesHistory context)
        {
            if (action.Type == RequestedAction.RAType.FETCH)
            {
                var dates   = action.Dates().ToList();
                var threads = new List <Thread>(dates.Count);

                foreach (var date in dates)
                {
                    if (GetRatesRecordFromHistory(context, date) == null)
                    {
                        var thread = new Thread(delegate() {
                            Console.WriteLine("Fetching data for {0}", date);
                            var resp = Fetch(date);
                            context.RatesRecords.Add(RatesRecord.FromRates(date, resp.Rates));
                        });
                        threads.Add(thread);
                        thread.Start();
                    }
                }

                foreach (var thread in threads)
                {
                    thread.Join();
                }

                context.SaveChanges();
            }
            else if (action.Type == RequestedAction.RAType.SHOW)
            {
                foreach (var date in action.Dates())
                {
                    var rates = GetRatesRecordFromHistory(context, date);
                    if (rates == null)
                    {
                        Console.WriteLine("{0}: no data", date.ToString("d"));
                    }
                    else
                    {
                        Console.WriteLine(rates);
                    }
                }
            }
            else if (action.Type == RequestedAction.RAType.SHOW_ALL)
            {
                foreach (var rates in context.RatesRecords.OrderBy(rr => rr.Date))
                {
                    Console.WriteLine(rates);
                }
            }
        }