public static ModelStore LoadFrom(TextReader reader)
        {
            var store = new ModelStore();

            try {
                if (reader.ReadLine() != "DATA-BEGIN")
                {
                    return(null);
                }
                while (true)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        return(null);
                    }
                    else if (line == "DATA-END")
                    {
                        break;
                    }

                    string[] tokens = line.Split(';');
                    switch (tokens[0])
                    {
                    case "BOOK":
                        store.books.Add(new Book {
                            Id = int.Parse(tokens[1]), Title = tokens[2], Author = tokens[3], Price = decimal.Parse(tokens[4])
                        });
                        break;

                    case "CUSTOMER": {
                        var customer = new Customer {
                            Id = int.Parse(tokens[1]), FirstName = tokens[2], LastName = tokens[3], DateJoined = null
                        };
                        if (tokens.Length >= 6)
                        {
                            customer.DateJoined = new DateTime(int.Parse(tokens[4]), int.Parse(tokens[5]), int.Parse(tokens[6]));
                        }
                        store.customers.Add(customer);
                        break;
                    }

                    case "CART-ITEM": {
                        var customer = store.GetCustomer(int.Parse(tokens[1]));
                        if (customer == null)
                        {
                            return(null);
                        }
                        customer.ShoppingCart.Items.Add(new ShoppingCartItem {
                                BookId = int.Parse(tokens[2]), Count = int.Parse(tokens[3])
                            });
                        break;
                    }

                    default:
                        return(null);
                    }
                }
            } catch (Exception ex) {
                if (ex is FormatException || ex is IndexOutOfRangeException)
                {
                    return(null);
                }
                throw;
            }

            return(store);
        }
Esempio n. 2
0
        public void ExecuteRequest(string request, TextWriter responseWriter)
        {
            IView view = InvalidRequestView.Instance;

            try {
                string[] tokens = request.Split(' ');

                if (tokens[0] == "GET")
                {
                    var customer = store.GetCustomer(int.Parse(tokens[1]));

                    if (customer != null && tokens[2].StartsWith(CommonRequestPrefix))
                    {
                        string[] verbs = tokens[2].Substring(CommonRequestPrefix.Length).Split('/');

                        if (verbs.Length <= 3)
                        {
                            string action = verbs.Length >= 2 ? verbs[1] : null;
                            int    id     = verbs.Length >= 3 ? int.Parse(verbs[2]) : 0;

                            switch (verbs[0])
                            {
                            case "Books":
                                view = booksController.HandleRequest(customer, action, id);
                                break;

                            case "ShoppingCart":
                                view = shoppingCartController.HandleRequest(customer, action, id);
                                break;

                            default:
                                view = null;
                                break;
                            }

                            if (view == null)
                            {
                                view = InvalidRequestView.Instance;
                            }
                            else
                            {
                                view = masterViewFactory.CreateView(
                                    menuViewFactory.CreateView(customer),
                                    view
                                    );
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                if (ex is IndexOutOfRangeException || ex is FormatException || ex is ArgumentOutOfRangeException)
                {
                    // Do nothing <- InvalidRequestView.Instance view is fine
                }
                else
                {
                    throw;
                }
            }

            view.RenderTo(responseWriter);
        }