Ejemplo n.º 1
0
        public IActionResult AddToCart(Guid productId)
        {
            var stockForProductId = _productsService.GetProduct(productId).Stock;

            if (stockForProductId == 0)
            {
                TempData["Warning"] = "Unfortunatley, The Chosen Product is Out Of Stock.";
                return(RedirectToAction("Details", "Products", new { id = productId }));
            }

            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            Guid GuidUserId = Guid.Empty;

            Guid.TryParse(userId, out GuidUserId);

            if (userId == null)
            {
                var  orderId     = HttpContext.Request.Cookies["tempOrder_id"];
                Guid GuidOrderId = Guid.Empty;
                Guid.TryParse(orderId, out GuidOrderId);

                _ordersService.AddGuestOrder(GuidOrderId);
                _ordersDetailsService.AddToGuestCart(productId, GuidOrderId);
                _ordersDetailsService.SetTotal(GuidOrderId);
            }
            else
            {
                _ordersDetailsService.AddToCart(productId, GuidUserId);
                var orderId = _ordersDetailsService.GetOrderId(GuidUserId);
                _ordersDetailsService.SetTotal(orderId);
            }


            TempData["Success"] = "Product Was Added to the Cart Succesfully";
            return(RedirectToAction("Details", "Products", new { id = productId }));
        }