示例#1
0
        public IActionResult Buy(int id, int added)
        {
            var notes = SessionHelper.GetObjectFromJson <List <IdCountModel> >(HttpContext.Session, "notes");

            if (notes != null)
            {
                foreach (var cmodel in notes)
                {
                    if (cmodel.Id == id)
                    {
                        added = cmodel.Count;
                        break;
                    }
                }
            }

            CartPageModel productModel = new CartPageModel();

            if (SessionHelper.GetObjectFromJson <List <CartPageModel> >(HttpContext.Session, "cart") == null)
            {
                List <CartPageModel> cart    = new List <CartPageModel>();
                BookModel            thebook = new ApiGetBooks().ApiFetchedBooks.Where(b => b.Id == id).FirstOrDefault();
                if (thebook != null && thebook.Stock >= added)
                {
                    cart.Add(new CartPageModel {
                        bookModel = thebook, Count = added
                    });
                }
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {
                List <CartPageModel> cart = SessionHelper.GetObjectFromJson <List <CartPageModel> >(HttpContext.Session, "cart");
                int       index           = DoesExist(id);
                BookModel thebook;
                if (index != -1)
                {
                    thebook = new ApiGetBooks().ApiFetchedBooks.Where(b => b.Id == id).FirstOrDefault();
                    if (thebook != null && thebook.Stock >= added + cart[index].Count)
                    {
                        cart[index].Count += added;
                    }
                }
                else
                {
                    thebook = new ApiGetBooks().ApiFetchedBooks.Where(b => b.Id == id).FirstOrDefault();
                    if (thebook != null && thebook.Stock >= added)
                    {
                        cart.Add(new CartPageModel {
                            bookModel = thebook, Count = added
                        });
                    }
                }
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            return(RedirectToAction("Index"));
        }
示例#2
0
        public bool Query(int id, int added)
        {
            BookModel thebook = new ApiGetBooks().ApiFetchedBooks.Where(b => b.Id == id).FirstOrDefault();
            int       index   = DoesExist(id);

            if (index == -1)
            {
                if (thebook != null && thebook.Stock >= added)
                {
                    return(true);
                }
            }
            else
            {
                List <CartPageModel> cart = SessionHelper.GetObjectFromJson <List <CartPageModel> >(HttpContext.Session, "cart");
                if (thebook != null && thebook.Stock >= added + cart[index].Count)
                {
                    return(true);
                }
            }
            return(false);
        }