public ActionResult Index()
        {
            using (db)
            {
                var user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name);
                if (user == null)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                var cartModel = new ShoppingCartViewModel();

                var cart = user.ShoppingCarts.FirstOrDefault();
                if (cart != null)
                {
                    foreach (var item in cart.ShoppingCartProducts)
                    {
                        var product = new ShoppingCartProductViewModel
                        {
                            ProductID = item.Product.ProductID,
                            Name      = item.Product.ProductName,
                            Price     = item.Product.Price ?? 99999,
                            ImageFile = item.Product.ImageFile,
                            Quantity  = item.Quantity ?? 0
                        };

                        cartModel.ShoppingCartList.Add(product);
                    }
                }

                return(View(cartModel));
            }
        }
Esempio n. 2
0
        public async Task <bool> AddProductAsync(bool isUserAuthenticated, ISession session, string userId, string productId, int quantity = 1)
        {
            if (isUserAuthenticated)
            {
                var user = await this.userManager.FindByIdAsync(userId);

                var shoppingCartId = user.ShoppingCartId;

                var shoppingCartExists = this.GetShoppingCartByIdAndProductId(productId, shoppingCartId) != null;

                if (shoppingCartExists)
                {
                    return(false);
                }

                var productExists = this.productsService.HasProduct(productId);

                if (!productExists)
                {
                    return(false);
                }

                var newShoppingCart = new ShoppingCartProduct
                {
                    ShoppingCartId = shoppingCartId,
                    ProductId      = productId,
                    Quantity       = quantity,
                };

                await this.shoppingCartProductRepository.AddAsync(newShoppingCart);

                await this.shoppingCartProductRepository.SaveChangesAsync();

                return(true);
            }
            else
            {
                var shoppingCartSession = session.GetObjectFromJson <List <ShoppingCartProductViewModel> >(GlobalConstants.SessionShoppingCartKey);
                if (shoppingCartSession == null)
                {
                    shoppingCartSession = new List <ShoppingCartProductViewModel>();
                }

                if (shoppingCartSession.Any(x => x.ProductId == productId))
                {
                    return(false);
                }

                var product             = this.productsService.GetById <ProductSidebarViewModel>(productId);
                var shoppingCartProduct = new ShoppingCartProductViewModel
                {
                    ProductId     = product.Id,
                    ProductName   = product.Name,
                    ProductPrice  = product.Price,
                    ImageUrl      = product.ImageUrl,
                    AverageRating = product.AverageRating,
                    Quantity      = quantity,
                };

                shoppingCartSession.Add(shoppingCartProduct);

                session.SetObjectAsJson(GlobalConstants.SessionShoppingCartKey, shoppingCartSession);

                return(true);
            }
        }