Exemplo n.º 1
0
        public async Task <IActionResult> Index(string productId)
        {
            var result = await _apiService.GetProductAsync(productId);

            if (result.Code != HttpStatusCode.OK)
            {
                return(View(new ProductDto()));
            }
            return(View(result.ResultObj));
        }
Exemplo n.º 2
0
        public async Task <HttpStatusCode> AddProductToCartNoLogin(string productId)
        {
            var cartDto = HttpContext.Session.GetJson <CartDto>("Cart");
            var product = await _productApiService.GetProductAsync(productId);

            if (cartDto == null)
            {
                cartDto             = new CartDto();
                cartDto.ListProduct = new List <CartDetailDto>();
                cartDto.ListProduct.Add(new CartDetailDto()
                {
                    ProductId   = productId,
                    Quantity    = 1,
                    ImagePath   = product.ResultObj.Path,
                    ProductName = product.ResultObj.Name,
                    UnitPrice   = product.ResultObj.PromotionPrice == product.ResultObj.RetailPrice?product.ResultObj.RetailPrice:product.ResultObj.PromotionPrice
                });
            }
            else
            {
                var checkProduct = cartDto.ListProduct.Where(x => x.ProductId == productId).SingleOrDefault();
                if (checkProduct != null)
                {
                    checkProduct.Quantity += 1;
                }
                else
                {
                    cartDto.ListProduct.Add(new CartDetailDto()
                    {
                        Quantity    = 1,
                        ProductId   = productId,
                        ImagePath   = product.ResultObj.Path,
                        ProductName = product.ResultObj.Name,
                        UnitPrice   = product.ResultObj.PromotionPrice == product.ResultObj.RetailPrice?product.ResultObj.RetailPrice:product.ResultObj.PromotionPrice
                    });
                }
            }
            HttpContext.Session.SetJson("Cart", cartDto);
            return(HttpStatusCode.OK);
        }