public IActionResult AddToCart(int id, int quantity)
        {
            var    product   = ECommData.GetProduct(id);
            var    totalCost = quantity * product.UnitPrice;
            string message   = $"You added {product.ProductName} " +
                               $"(x{quantity}) to your cart at a total cost of {totalCost:C}.";

            var cart     = ShoppingCart.GetFromSession(HttpContext.Session);
            var lineItem = cart.LineItems.SingleOrDefault(item => item.Product.Id == id);

            if (lineItem != null)
            {
                lineItem.Quantity += quantity;
            }
            else
            {
                cart.LineItems.Add(new ShoppingCart.LineItem
                {
                    Product = product, Quantity = quantity
                });
            }

            ShoppingCart.StoreInSession(cart, HttpContext.Session);
            Logger.LogInformation("*** AddToCart({id}, {quantity}): Cart Updated", id, quantity);

            return(PartialView("_AddedToCart", message));
        }
        public IActionResult Detail(int id)
        {
            //ViewBag.guid1 = ECommData.ToString();
            //ViewBag.guid2 = ECommData.ToString();

            var product = ECommData.GetProduct(id);

            if (product == null)
            {
                Logger.LogError("!!! Detail({id}): Not Found", id);
                return(NotFound());
            }
            Logger.LogInformation("*** Detail({id}): Found", id);
            return(View(product));
        }
 public ProductController(ECommData ecommData)
 {
     ECommData = ecommData;
 }
 public async Task <IActionResult> Async()
 {
     Logger.LogInformation("*** Async(): Called");
     return(View("Index", await ECommData.GetProductsAsync()));
 }
 public IActionResult Index()
 {
     Logger.LogInformation("*** Index(): Called");
     return(View(ECommData.GetProducts()));
 }
 public ProductCountViewComponent(ECommData ecommData)
 {
     ECommData = ecommData;
 }
Esempio n. 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome ABC Ecomm Store!!\nWe have following products in store.\n");

            Thread.Sleep(2000);

            var allProducts = ECommData.GetAllProducts();

            ECommData.DisplayProductsInStore(allProducts);

            /*
             *
             * WebCustomer webCustomer = new WebCustomer();
             *
             * //Must Login First
             * webCustomer.LogIn();
             * ContinueShopping:
             * System.Threading.Thread.Sleep(2000);
             * Console.WriteLine("Enter Product Name and Quantity to Add to Your Cart");
             * var itemName = Console.ReadLine();
             * var quantity = Console.ReadLine();
             * var product = allProducts.FirstOrDefault(x => x.ProductName.ToUpper() == itemName.ToUpper());
             * if (product != null)
             *  webCustomer.AddProductInCart(product, int.Parse(quantity));
             *
             * Console.WriteLine("Still shopping? Y/N");
             *
             * var response = Console.ReadLine();
             *
             * if (response.ToUpper() == "Y")
             *  goto ContinueShopping;
             *
             * webCustomer.CalculateTotal();
             *
             */


            //If Customer is Premium customer, should I repeat above code again??
            //NO

            //ToDo: Use below code

            Console.WriteLine("");
            Console.WriteLine("Enter 'P' for premium customer or enter any other key for web customer");
            var customerType = Console.ReadLine();

            BaseCustomer customer;

            if (customerType.ToUpper() == "P")
            {
                customer       = new PremiumCustomer();
                customer.Name  = "John Smith";
                customer.Email = "*****@*****.**";
                customer.Id    = 1015;
            }
            else
            {
                customer       = new WebCustomer();
                customer.Name  = "John Smith";
                customer.Email = "*****@*****.**";
                customer.Id    = 1015;
            }


            //Must Login First
            customer.LogIn();
ContinueShopping:
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Enter Product Name and Quantity to Add to Your Cart");
            var itemName = Console.ReadLine();
            var quantity = Console.ReadLine();
            var product  = allProducts.FirstOrDefault(x => x.ProductName.ToUpper() == itemName.ToUpper());

            if (product != null)
            {
                customer.AddProductInCart(product, int.Parse(quantity));
            }

            Console.WriteLine("Still shopping? Y/N");

            var response = Console.ReadLine();

            if (response.ToUpper() == "Y")
            {
                goto ContinueShopping;
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\nItems in your cart\n");
            Console.ForegroundColor = ConsoleColor.White;
            customer.ViewAllCartProducts();
            Console.WriteLine("");
            decimal totalToPay = customer.CalculateTotal();

            Console.WriteLine($"You need to pay {totalToPay}");

            customer.MakePayment(totalToPay);

            Thread.Sleep(1500);
            Console.WriteLine("Cheking customer's rewards.\n");

            Thread.Sleep(1500);
            if (customerType.ToUpper() == "P")
            {
                int rewardPoints = (customer as IRewardSystem).GetTheRewardPoints();
                Console.WriteLine($"You have total {rewardPoints} so far.\n\n");
            }
            else
            {
                Console.WriteLine("Web customer is not eligible for reward system");
            }
        }
Esempio n. 8
0
 public ProductController(ECommData ecommData, ILogger <ProductController> logger)
 {
     ECommData = ecommData;
     Logger    = logger;
 }