Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BasketManager"/> class.
 /// </summary>
 /// <param name="goodsManager">The goods manager.</param>
 public BasketManager(IGoodsManager goodsManager)
 {
     this.goodsManager  = goodsManager;
     this.BasketDetails = new BasketDetails();
     AddHistory(HistoryStart);
 }
Exemplo n.º 2
0
        public IActionResult AddToCart(int productId, int quantity, int colorId, int sizeId)
        {
            var product = _productService.GetById(productId);

            var session = HttpContext.Session.Get <List <CartViewModel> >(CommonConstants.CartSession);

            if (session != null)
            {
                bool hasChanged = false;
                if (session.Any(x => x.Product.Id == productId))
                {
                    foreach (var item in session)
                    {
                        if (item.Product.Id == productId)
                        {
                            item.Quantity += quantity;
                            item.Price     = product.PromotionPrice ?? product.Price;
                            item.ColorId   = colorId;
                            item.SizeId    = sizeId;
                            hasChanged     = true;
                        }
                    }
                }
                else
                {
                    session.Add(new CartViewModel()
                    {
                        Product  = product,
                        ColorId  = colorId,
                        SizeId   = sizeId,
                        Price    = product.PromotionPrice ?? product.Price,
                        Quantity = quantity
                    });
                    hasChanged = true;
                }

                if (hasChanged)
                {
                    HttpContext.Session.Set(CommonConstants.CartSession, session);
                }
            }
            else
            {
                var cart = new List <CartViewModel>();
                cart.Add(new CartViewModel()
                {
                    Product  = product,
                    Quantity = quantity,
                    SizeId   = sizeId,
                    ColorId  = colorId,
                    Price    = product.PromotionPrice ?? product.Price
                });
                HttpContext.Session.Set(CommonConstants.CartSession, cart);
            }
            var basketDetail = new BasketDetails()
            {
                Product  = product,
                Color    = _colorService.GetColorbyId(colorId),
                Size     = _sizeService.GetSizeById(sizeId),
                Quantity = quantity
            };

            return(new OkObjectResult(basketDetail));
        }