예제 #1
0
파일: Program.cs 프로젝트: stepan101/ukoly
        public void AllBooks(ModelStore model)
        {
            int i = 0;

            Console.WriteLine("\tOur books for you:");
            Console.WriteLine("\t<table>");
            if (model.GetBooks().Count > 0)
            {
                Console.WriteLine("\t\t<tr>");
                foreach (Book book in model.GetBooks())
                {
                    if (i % 3 == 0 && i != 0)
                    {
                        Console.WriteLine("\t\t</tr>");
                        Console.WriteLine("\t\t<tr>");
                    }


                    Console.WriteLine("\t\t\t<td style=\"padding: 10px;\">");
                    Console.WriteLine("\t\t\t\t<a href=\"/Books/Detail/{0}\">{1}</a><br />", book.Id, book.Title);
                    Console.WriteLine("\t\t\t\tAuthor: {0}<br />", book.Author);
                    Console.WriteLine("\t\t\t\tPrice: {0} EUR &lt;<a href=\"/ShoppingCart/Add/{1}\">Buy</a>&gt;", book.Price, book.Id);
                    Console.WriteLine("\t\t\t</td>");
                    i++;
                }
                Console.WriteLine("\t\t</tr>");
            }
            Console.WriteLine("\t</table>");
        }
예제 #2
0
파일: Program.cs 프로젝트: stepan101/ukoly
        public void Check()
        {
            try {
                if (ControlURL())
                {
                    if (Url.Length > 3 && Url[2] == "Books")
                    {
                        if (Url[3] == "Detail")
                        {
                            Int32.TryParse(Url[4], out IdOfBook);
                            if (IdOfBook != -1)
                            {
                                if (model.GetBooks().Contains(model.GetBook(IdOfBook)))
                                {
                                    view.BooksDetail(CustomerId, IdOfBook, model);
                                }
                                else
                                {
                                    view.Invalid();
                                }
                            }
                        }
                        else
                        {
                            view.Invalid();
                        }
                    }
                    else if (Url.Length > 3 && Url[2] == "ShoppingCart")
                    {
                        if (Url[3] == "Add")
                        {
                            Int32.TryParse(Url[4], out IdOfBook);
                            if (IdOfBook != -1)
                            {
                                if (!model.GetBooks().Contains(model.GetBook(IdOfBook)))
                                {
                                    view.Invalid();
                                    return;
                                }
                                if (model.GetCustomer(CustomerId).ShoppingCart.Items.Contains(model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook)))
                                {
                                    model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook).Count++;
                                }
                                else
                                {
                                    model.GetCustomer(CustomerId).ShoppingCart.Items.Add(new ShoppingCartItem());
                                    model.GetCustomer(CustomerId).ShoppingCart.Items[model.GetCustomer(CustomerId).ShoppingCart.Items.Count - 1].BookId = IdOfBook;
                                    model.GetCustomer(CustomerId).ShoppingCart.Items[model.GetCustomer(CustomerId).ShoppingCart.Items.Count - 1].Count  = 1;
                                }
                                view.ShoppingCart(CustomerId, model);
                            }
                        }
                        else if (Url[3] == "Remove")
                        {
                            Int32.TryParse(Url[4], out IdOfBook);
                            if (IdOfBook != -1)
                            {
                                if (!model.GetBooks().Contains(model.GetBook(IdOfBook)))
                                {
                                    view.Invalid();
                                    return;
                                }
                                if (model.GetCustomer(CustomerId).ShoppingCart.Items.Contains(model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook)))
                                {
                                    if (model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook).Count >= 2)
                                    {
                                        model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook).Count--;
                                    }
                                    else if (model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook).Count == 1)
                                    {
                                        model.GetCustomer(CustomerId).ShoppingCart.Items.Remove(model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook));
                                    }

                                    if (model.GetCustomer(CustomerId).ShoppingCart.Items.Count == 0)
                                    {
                                        view.ShoppingCartEmpty(CustomerId, model);
                                    }
                                    else
                                    {
                                        view.ShoppingCart(CustomerId, model);
                                    }
                                }
                                else
                                {
                                    view.Invalid();
                                }
                            }
                        }
                        else
                        {
                            view.Invalid();
                        }
                    }
                    else
                    {
                        if (Url[2] == "Books")
                        {
                            if (model.GetCustomers().Count == 0)
                            {
                                view.Invalid();
                                return;
                            }
                            view.Books(CustomerId, model);
                        }
                        else if (Url[2] == "ShoppingCart")
                        {
                            if (model.GetCustomers().Count == 0)
                            {
                                view.Invalid();
                                return;
                            }
                            if (model.GetCustomer(CustomerId).ShoppingCart.Items.Count == 0)
                            {
                                view.ShoppingCartEmpty(CustomerId, model);
                            }
                            else
                            {
                                view.ShoppingCart(CustomerId, model);
                            }
                        }
                        else
                        {
                            view.Invalid();
                        }
                    }
                }
                else
                {
                    view.Invalid();
                }
            }
            catch { view.Invalid(); }
        }
예제 #3
0
        public static void ReadAdnDoRequests(TextReader reader, TextWriter writer, ModelStore store)
        {
            Regex BookList              = new Regex(@"^GET \d+ http://www.nezarka.net/Books$");
            Regex BookDetail            = new Regex(@"^GET \d+ http://www.nezarka.net/Books/Detail/\d+$");
            Regex ShopingCart           = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart$");
            Regex ShopingCartAddItem    = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart/Add/\d+$");
            Regex ShopingCartRemoveItem = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart/Remove/\d+$");

            char[] delims = new char[] { ' ', '/' };

            // bool first = true;

            string line = reader.ReadLine();

            while (line != null && line != "")
            {
                string[] Records = line.Split(delims, StringSplitOptions.RemoveEmptyEntries);

                /*if (!first)
                 * {
                 *  writer.WriteLine("====");
                 * }
                 * first = false; */
                if (BookList.Match(line).Length > 0)
                {
                    int      cusID = Int32.Parse(Records[1]);
                    Customer cust  = store.GetCustomer(cusID);

                    if (cust == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    string      firstName = cust.FirstName;
                    List <Book> Books     = store.GetBooks();
                    int         numItems  = cust.CountItemsInCart();
                    View.GenBookList(writer, firstName, numItems, Books);
                }
                else if (BookDetail.Match(line).Length > 0)
                {
                    int      cusID  = Int32.Parse(Records[1]);
                    int      bookID = Int32.Parse(Records[Records.Length - 1]);
                    Customer cust   = store.GetCustomer(cusID);
                    Book     b      = store.GetBook(bookID);
                    if (cust == null || b == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    string firstNsame = cust.FirstName;
                    int    numItems   = cust.CountItemsInCart();

                    View.GenBookDetail(writer, firstNsame, numItems, b.Title, b.Author, b.Price, b.Id);
                }
                else if (ShopingCart.Match(line).Length > 0)
                {
                    int      cusID = Int32.Parse(Records[1]);
                    Customer cust  = store.GetCustomer(cusID);
                    if (cust == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }

                    string firstName = cust.FirstName;
                    if (cust.CountItemsInCart() == 0)
                    {
                        View.GenCartEmpty(writer, firstName);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    List <ShoppingCartItem> cart = cust.GetCart();

                    View.GenCart(writer, firstName, cart, store);
                }
                else if (ShopingCartAddItem.Match(line).Length > 0)
                {
                    int      cusID  = Int32.Parse(Records[1]);
                    int      bookID = Int32.Parse(Records[Records.Length - 1]);
                    Customer cust   = store.GetCustomer(cusID);
                    Book     b      = store.GetBook(bookID);
                    if (cust == null || b == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    //TODO: create shopping cart?
                    cust.ShoppingCart.AddItem(bookID);

                    string firstName             = cust.FirstName;
                    List <ShoppingCartItem> cart = cust.GetCart();

                    View.GenCart(writer, firstName, cart, store);
                }
                else if (ShopingCartRemoveItem.Match(line).Length > 0)
                {
                    int      cusID  = Int32.Parse(Records[1]);
                    int      bookID = Int32.Parse(Records[Records.Length - 1]);
                    Customer cust   = store.GetCustomer(cusID);
                    Book     b      = store.GetBook(bookID);
                    if (cust == null || b == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    bool RemoveSuccesful = cust.ShoppingCart.RemoveItem(bookID);
                    if (RemoveSuccesful)
                    {
                        string firstName             = cust.FirstName;
                        List <ShoppingCartItem> cart = cust.GetCart();
                        if (cust.CountItemsInCart() == 0)
                        {
                            View.GenCartEmpty(writer, firstName);
                            writer.WriteLine("====");
                            writer.Flush();
                            line = reader.ReadLine();
                            continue;
                        }
                        View.GenCart(writer, firstName, cart, store);
                    }
                    else
                    {
                        View.GenInvalidRequest(writer);
                    }
                }
                else
                {
                    View.GenInvalidRequest(writer);
                }
                writer.WriteLine("====");
                writer.Flush();
                line = reader.ReadLine();
            }
        }