Пример #1
0
        static void RemoveFromCart() //remove item from cart
        {
            if (logedIn == true)
            {
                using (var db = new AppDbContext())
                {
                    try
                    {
                        Console.WriteLine("Remove Product Number: ");
                        int removeId = Convert.ToInt32(Console.ReadLine());

                        //check to see if item is in cart
                        var         inCart = db.CartProducts.Where(p => p.ProductId == removeId && p.CartId == cartId).FirstOrDefault();
                        CartProduct r      = db.CartProducts.Find(cartId, removeId);

                        if (inCart is null)
                        {
                            Console.WriteLine();
                            Console.WriteLine("This item is not in your cart.");
                            Console.WriteLine();
                        }
                        else
                        {
                            if (r.Quantity > 1)
                            {
                                r.Quantity--;
                                db.SaveChanges();
                                Console.WriteLine($"New Quantity is: {r.Quantity}");
                            }
                            else
                            {
                                db.CartProducts.Remove(r);
                                db.SaveChanges();
                            }
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Invalid input.");
                    }
                }
            }
            else
            {
                Console.WriteLine("You must be logged in to remove items from cart.");
                Login();
            }
        }
Пример #2
0
        //Adds products to cart
        static void Add()
        {
            if (logedIn == true)
            {
                using (var db = new AppDbContext())
                {
                    try
                    {
                        Console.WriteLine("Enter Product to Add");
                        int addProduct = Convert.ToInt32(Console.ReadLine());

                        //checks to see if product exists
                        var productExists = db.Products.Where(p => p.ProductId == addProduct).FirstOrDefault();

                        if (productExists is null)
                        {
                            Console.WriteLine("This product does not exists.");
                        }
                        else
                        {
                            try{
                                //finds product price
                                var needPrice = db.Products.Where(x => x.ProductId == addProduct).Select(x => x.Price).First();

                                //Check if product is in cart
                                var productInCart = db.CartProducts.Where(p => p.ProductId == addProduct && p.CartId == cartId).FirstOrDefault();

                                if (productInCart is null) //if product doesn't exist, create new product in CartProducts.
                                {
                                    CartProduct newCart = new CartProduct {
                                        Quantity = 1, ProductId = addProduct, CartId = cartId, ProductTotal = needPrice
                                    };
                                    db.Add(newCart);
                                    db.SaveChanges();
                                }
                                else //If product does exist, update quantity in CartProducts
                                {
                                    CartProduct update = db.CartProducts.Find(cartId, addProduct);
                                    //int newQuant = update.Quantity ++;
                                    update.Quantity++;

                                    update.ProductTotal = (update.Quantity * needPrice);
                                    db.SaveChanges();

                                    var test = db.CartProducts.Where(c => c.CartId == cartId);

                                    foreach (var product in test)
                                    {
                                        Console.WriteLine(product.Quantity);
                                        Console.WriteLine(product.ProductTotal);
                                    }
                                }
                            }catch
                            {
                                Console.WriteLine("Product doesn't have information.");
                            }
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Invalid input.");
                    }
                }
            }
            else
            {
                Console.WriteLine("Please log in to continue.");
                Login();
            }
            Console.WriteLine();
        }