Esempio n. 1
0
        public IActionResult SearchProduct(string inputSearch)
        {
            ViewData["Title"] = "Search";
            List <ProductViewModel> productsVMs = _productService.GetAllProducts();

            if (!HelperService.AProductExists(productsVMs))
            {
                RedirectToAction(nameof(ProductNotFound), new { message = "" });
            }
            _logger.LogInfo("Got products that user searched for from product service.");

            var productsFound = HelperService.GetProductsWithTerm(inputSearch, productsVMs);

            if (productsFound.Count == 0)
            {
                return(RedirectToAction(nameof(ProductNotFound), new { message = inputSearch }));
            }
            else
            {
                productsVMs = productsFound;
            }

            var productsVM = new ProductsViewModel()
            {
                Products = productsVMs
            };

            return(View(productsVM));
        }
        public RedirectToActionResult AddToShoppingCart(int productId)
        {
            var productVM = _productService.GetProductById(productId);

            if (!HelperService.AProductExists(productVM))
            {
                RedirectToAction(nameof(ProductController.ProductNotFound), nameof(Product), new { message = "" });
            }
            else
            {
                var productToAdd = _mapper.Map <Product>(productVM);
                _shoppingCart.AddToCart(productToAdd, 1);
                _logger.LogInfo($"A product with id: {productId} has been added to the shopping cart.");
            }

            return(RedirectToAction(nameof(ShoppingCartIndex)));
        }
Esempio n. 3
0
        public ViewResult Laptops()
        {
            ViewData["Title"] = "Laptops";
            List <ProductViewModel> laptopsVMs = _productService.GetLaptops();

            if (!HelperService.AProductExists(laptopsVMs))
            {
                RedirectToAction(nameof(ProductNotFound), new { message = "" });
            }

            _logger.LogInfo("Got laptops from product service.");
            var productsVM = new ProductsViewModel()
            {
                Laptops = laptopsVMs
            };

            return(View(productsVM));
        }
        public RedirectToActionResult RemoveFromShoppingCart(int productId)
        {
            var productVM = _productService.GetProductById(productId);

            if (!HelperService.AProductExists(productVM))
            {
                RedirectToAction(nameof(ProductController.ProductNotFound), nameof(Product), new { message = "" });
            }
            else
            {
                var productToRemove = _mapper.Map <Product>(productVM);
                _shoppingCart.RemoveFromCart(productToRemove);
                _logger.LogInfo($"A product with id: {productId} has been removed from the shopping cart.");
            }

            if (_shoppingCart.GetNumberOfItems() == 0)
            {
                return(RedirectToAction(nameof(ProductController.Index), nameof(Product)));
            }

            return(RedirectToAction(nameof(ShoppingCartIndex)));
        }