Ejemplo n.º 1
0
        public IActionResult AddToCart(int productId, int colorId, int sizeId, int quantity)
        {
            var shoppingCart = HttpContext.Session.GetList <ShoppingCardViewModel>(CommonConstants.SesstionCart);

            if (shoppingCart == null)
            {
                shoppingCart = new List <ShoppingCardViewModel>();
            }

            SizeViewModel  sizeVm  = _productQuantityService.GetSizeById(sizeId);
            ColorViewModel colorVm = _productQuantityService.GetColorById(colorId);

            if (shoppingCart.Any(x => x.ProductId == productId && x.SizeVm.Id == sizeId && x.ColorVm.Id == colorId))
            {
                foreach (var item in shoppingCart)
                {
                    if (item.ProductId == productId && item.SizeVm.Id == sizeId && item.ColorVm.Id == colorId)
                    {
                        item.Quantity += quantity;
                        break;
                    }
                }
            }
            else
            {
                ProductViewModel      product = _productService.GetById(productId);
                ShoppingCardViewModel cart    = new ShoppingCardViewModel()
                {
                    ProductId = productId,
                    SizeVm    = sizeVm,
                    ColorVm   = colorVm,
                    Quantity  = quantity,
                    ProductVm = product,
                };
                shoppingCart.Add(cart);
            }
            HttpContext.Session.SetList <ShoppingCardViewModel>(CommonConstants.SesstionCart, shoppingCart);
            return(new OkObjectResult(productId));
        }
 public IActionResult SizeDetail(int id)
 {
     return(new OkObjectResult(_productQuantityService.GetSizeById(id)));
 }