Ejemplo n.º 1
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);
                }
            }
        }
Ejemplo n.º 2
0
        public static ICollection <RequestedAction> ParseArgs(string[] args)
        {
            int index = 0;
            List <RequestedAction> req_actions = new();

            try {
                while (index < args.Length)
                {
                    if (args[index] == "help")
                    {
                        PrintHelp();
                        return(null);
                    }
                    else if (args[index] == "show" && args[index + 1] == "it")
                    {
                        if (req_actions.Count() == 0 || req_actions.Last().Type != RequestedAction.RAType.FETCH)
                        {
                            throw new ArgumentException(String.Format(
                                                            "'it' must reference arguments of previous fetch (in 'show', arg #{0})", index + 2));
                        }

                        req_actions.Add(new(RequestedAction.RAType.SHOW, req_actions.Last().Start, req_actions.Last().End));
                        index += 2;
                    }
                    else if (args[index] == "show" && args[index + 1] == "all")
                    {
                        req_actions.Add(RequestedAction.ActionShowAll());
                        index += 2;
                    }
                    else if (args[index] == "fetch" || args[index] == "show")
                    {
                        DateTime startDate;
                        DateTime?endDate = null;

                        // Parse first date arg, which is required
                        try {
                            startDate = DateTime.Parse(args[index + 1]);
                        }
                        catch (FormatException) {
                            throw new ArgumentException(String.Format(
                                                            "'{0}' is not a valid date (in '{1}', arg #{2})",
                                                            args[index + 1], args[index], index + 2));
                        }

                        // Parse second date arg, which is optional
                        if (index + 2 < args.Length)
                        {
                            try {
                                endDate = DateTime.Parse(args[index + 2]);
                            }
                            catch (FormatException) { }
                        }

                        var req_type = (args[index] == "fetch") ? RequestedAction.RAType.FETCH : RequestedAction.RAType.SHOW;
                        try {
                            req_actions.Add(new(req_type, startDate, endDate));
                        }
                        catch (ArgumentException e) {
                            throw new ArgumentException(String.Format("{0} (in '{1}', arg#{2})", e.Message, args[index], index + 1));
                        }
                        index += 2 + (endDate != null ? 1 : 0);
                    }
                    else
                    {
                        throw new ArgumentException(String.Format("unknown action '{0}' (arg #{1})", args[index], index + 1));
                    }
                }
            }
            catch (IndexOutOfRangeException) {
                throw new ArgumentException(String.Format("not enough arguments for action '{0}' (arg #{1})", args[index], index + 1));
            }

            return(req_actions);
        }