Exemple #1
0
        public RedirectToActionResult Add(int id)
        {
            var book = data.Get(new QueryOptions <Book> {
                Includes = "BookAuthors.Author, Genre",
                Where    = b => b.BookId == id
            });

            if (book == null)
            {
                TempData["message"] = "Unable to add book to cart.";
            }
            else
            {
                var dto = new BookDTO();
                dto.Load(book);
                CartItem item = new CartItem {
                    Book     = dto,
                    Quantity = 1  // default value
                };

                cart.Add(item);
                cart.Save();

                TempData["message"] = $"{book.Title} added to cart";
            }

            var builder = new BooksGridBuilder(HttpContext.Session);

            return(RedirectToAction("List", "Book", builder.CurrentRoute));
        }
Exemple #2
0
        public void Load(Repository <Book> data)
        {
            items = session.GetObject <List <CartItem> >(CartKey);
            if (items == null)
            {
                items       = new List <CartItem>();
                storedItems = requestCookies.GetObject <List <CartItemDTO> >(CartKey);
            }

            if (storedItems?.Count > items?.Count)
            {
                foreach (CartItemDTO storedItem in storedItems)
                {
                    var book = data.Get(new QueryOptions <Book>
                    {
                        Includes = "BookAuthors.Author, Genre",
                        Where    = b => b.BookID == storedItem.BookID
                    });

                    if (book != null)
                    {
                        var dto = new BookDTO();
                        dto.Load(book);

                        CartItem item = new CartItem
                        {
                            Book     = dto,
                            Quantity = storedItem.Quantity
                        };

                        items.Add(item);
                    }
                }

                Save();
            }
        }