/// <summary>
        /// Passes the user's cart to the AddCartToDb method and returns a confirmation screen.
        /// </summary>
        /// <returns>View</returns>
        public IActionResult Checkout()
        {
            bool check = BusinessLogic.checkUserCache(_cache);

            if (!check)
            {
                _logger.LogInformation("User not logged in, returning to login");
                return(RedirectToAction("Login", "Home"));
            }

            bool cartCheck = _cache.TryGetValue("prodIds", out List <int> prodIds);

            if (!cartCheck)
            {
                return(View("EmptyCart"));
            }

            Cart currentCart = BusinessLogic.NewCart();

            currentCart.ProdIds       = prodIds;
            currentCart.LocIds        = (List <int>)_cache.Get("locIds");
            currentCart.CustId        = (int)_cache.Get("custId");
            currentCart.OrderQuantity = (List <int>)_cache.Get("quantities");
            currentCart.ProdNames     = (List <string>)_cache.Get("prodNames");
            currentCart.Costs         = (List <double>)_cache.Get("costs");
            currentCart.TotalCost     = (double)_cache.Get("totalCost");

            BusinessLogic.AddCartToDb(currentCart, _db);

            return(View());
        }
Esempio n. 2
0
        public void TestDbDecrement()
        {
            var options = new DbContextOptionsBuilder <MyDbContext>().UseInMemoryDatabase(databaseName: "TestDbDecrement").Options;

            using (var context = new MyDbContext(options))
            {
                var productList = new List <Product>()
                {
                    new Product {
                        Name = "Mattress Frame", Description = "This is a mattress frame to fit a king-sized mattress.", Price = 149.99, Quantity = 50, StoreId = 1
                    },
                    new Product {
                        Name = "Memory Foam Mattress", Description = "This is a king-sized memory foam mattress. The best of the best!", Price = 499.99, Quantity = 50, StoreId = 2
                    },
                    new Product {
                        Name = "Spring Mattress", Description = "This is a king-sized spring mattress. Comfort at an affordable price!", Price = 274.99, Quantity = 50, StoreId = 3
                    },
                };
                context.Products.AddRange(productList);
                context.SaveChanges();

                Cart newCart = BusinessLogic.NewCart();
                newCart.Costs.Add(100);
                newCart.Costs.Add(200);
                newCart.Costs.Add(300);
                newCart.CustId = 1;
                newCart.LocIds.Add(1);
                newCart.LocIds.Add(2);
                newCart.LocIds.Add(3);
                newCart.OrderQuantity.Add(3);
                newCart.OrderQuantity.Add(2);
                newCart.OrderQuantity.Add(1);
                newCart.ProdIds.Add(1);
                newCart.ProdIds.Add(2);
                newCart.ProdIds.Add(3);
                newCart.ProdNames.Add("a");
                newCart.ProdNames.Add("b");
                newCart.ProdNames.Add("c");
                newCart.TotalCost = 1000.00;

                BusinessLogic.AddCartToDb(newCart, context);
                int ans = context.Products.Find(1).Quantity;

                Assert.Equal(47, ans);
            }
        }
Esempio n. 3
0
        public void TestSubcartFormatCount()
        {
            int numOfOrderGroups = -1;
            var options          = new DbContextOptionsBuilder <MyDbContext>().UseInMemoryDatabase(databaseName: "TestSubcartFormatCount").Options;

            using (var context = new MyDbContext(options))
            {
                var productList = new List <Product>()
                {
                    new Product {
                        Name = "Mattress Frame", Description = "This is a mattress frame to fit a king-sized mattress.", Price = 149.99, Quantity = 50, StoreId = 1
                    },
                    new Product {
                        Name = "Memory Foam Mattress", Description = "This is a king-sized memory foam mattress. The best of the best!", Price = 499.99, Quantity = 50, StoreId = 2
                    },
                    new Product {
                        Name = "Spring Mattress", Description = "This is a king-sized spring mattress. Comfort at an affordable price!", Price = 274.99, Quantity = 50, StoreId = 3
                    },
                };
                context.Products.AddRange(productList);
                context.SaveChanges();

                var stores = new List <Store>
                {
                    new Store {
                        Location = "Irving"
                    },
                    new Store {
                        Location = "Plano"
                    },
                    new Store {
                        Location = "Arlington"
                    },
                    new Store {
                        Location = "Lewisville"
                    },
                    new Store {
                        Location = "Mesquite"
                    },
                };
                context.AddRange(stores);
                context.SaveChanges();

                Cart newCart = BusinessLogic.NewCart();
                newCart.CustId = 1;
                newCart.Costs.Add(100);
                newCart.Costs.Add(200);
                newCart.Costs.Add(300);
                newCart.CustId = 1;
                newCart.LocIds.Add(1);
                newCart.LocIds.Add(2);
                newCart.LocIds.Add(3);
                newCart.OrderQuantity.Add(3);
                newCart.OrderQuantity.Add(2);
                newCart.OrderQuantity.Add(1);
                newCart.ProdIds.Add(1);
                newCart.ProdIds.Add(2);
                newCart.ProdIds.Add(3);
                newCart.ProdNames.Add("a");
                newCart.ProdNames.Add("b");
                newCart.ProdNames.Add("c");
                newCart.TotalCost = 0.00;

                BusinessLogic.AddCartToDb(newCart, context);
                BusinessLogic.AddCartToDb(newCart, context);

                List <HistorySubcart> historySubcart = BusinessLogic.FormatLocOrderHistory(1, context);
                numOfOrderGroups = historySubcart.Count();
            }
            Assert.Equal(2, numOfOrderGroups);
        }