示例#1
0
        public void CancelsOrderOnNegativeInventory()
        {
            //Arrange
            var options = BuildInMemoryDb("CancelsOrder");

            //Act
            using (var context = new P0DbContext(options))
            {
                CreateOneCustomer(context);
                CreateTwoproducts(context);

                var store = new Store
                {
                    StoreId           = 1,
                    Location          = "Location1",
                    AvailableProducts = new List <Inventory>
                    {
                        new Inventory
                        {
                            ProductId = 1,
                            StoreId   = 1,
                            Quantity  = 10
                        },
                        new Inventory
                        {
                            ProductId = 2,
                            StoreId   = 1,
                            Quantity  = 50
                        }
                    }
                };
                context.Add(store);
                context.SaveChanges();
                try
                {
                    var backend = new StoreBackend(context);
                    var prods   = new List <ProductQuantity>()
                    {
                        new ProductQuantity()
                        {
                            ProductId = 1,
                            Quantity  = 12
                        }
                    };

                    backend.PlaceNewOrder(1, 1, prods);
                }
                catch { }
            }
            //Assert
            using (var context = new P0DbContext(options))
            {
                var orders = (from o in context.Orders
                              select o).ToList();

                Assert.Empty(orders);
            }
        }
示例#2
0
        public void DecrementsInventoryOnOrder()
        {
            //Arrange
            var options = BuildInMemoryDb("DecrementsInventory");
            int orderId;

            //Act
            using (var context = new P0DbContext(options))
            {
                CreateOneCustomer(context);
                CreateTwoproducts(context);

                var store = new Store
                {
                    StoreId           = 1,
                    Location          = "Location1",
                    AvailableProducts = new List <Inventory>
                    {
                        new Inventory
                        {
                            ProductId = 1,
                            StoreId   = 1,
                            Quantity  = 10
                        },
                        new Inventory
                        {
                            ProductId = 2,
                            StoreId   = 1,
                            Quantity  = 50
                        }
                    }
                };
                context.Add(store);
                context.SaveChanges();

                var backend = new StoreBackend(context);
                var prods   = new List <ProductQuantity>()
                {
                    new ProductQuantity()
                    {
                        ProductId = 1,
                        Quantity  = 2
                    }
                };

                orderId = backend.PlaceNewOrder(1, 1, prods).OrderId;
            }
            //Assert
            using (var context = new P0DbContext(options))
            {
                var item = (from inv in context.StoreInventories
                            where inv.StoreId == 1 && inv.ProductId == 1
                            select inv).Take(1).FirstOrDefault();

                Assert.Equal(8, item.Quantity);
            }
        }
示例#3
0
        public void ThrowsOnNegativeInventory()
        {
            //Arrange
            var options = BuildInMemoryDb("ThrowsException");

            //Act
            using (var context = new P0DbContext(options))
            {
                CreateOneCustomer(context);
                CreateTwoproducts(context);

                var store = new Store
                {
                    StoreId           = 1,
                    Location          = "Location1",
                    AvailableProducts = new List <Inventory>
                    {
                        new Inventory
                        {
                            ProductId = 1,
                            StoreId   = 1,
                            Quantity  = 10
                        },
                        new Inventory
                        {
                            ProductId = 2,
                            StoreId   = 1,
                            Quantity  = 50
                        }
                    }
                };
                context.Add(store);
                context.SaveChanges();
            }
            //Assert
            using (var context = new P0DbContext(options))
            {
                var backend = new StoreBackend(context);
                var prods   = new List <ProductQuantity>()
                {
                    new ProductQuantity()
                    {
                        ProductId = 1,
                        Quantity  = 12
                    }
                };

                Assert.Throws <ArgumentOutOfRangeException>(() => backend.PlaceNewOrder(1, 1, prods));
            }
        }
示例#4
0
        public void AddsOrderToDb()
        {
            //Arrange
            var options = BuildInMemoryDb("AddsOrderToDb");
            int orderId;

            //Act
            using (var context = new P0DbContext(options))
            {
                CreateOneCustomer(context);
                CreateTwoproducts(context);

                var store = new Store
                {
                    StoreId           = 1,
                    Location          = "Location1",
                    AvailableProducts = new List <Inventory>
                    {
                        new Inventory
                        {
                            ProductId = 1,
                            StoreId   = 1,
                            Quantity  = 10
                        },
                        new Inventory
                        {
                            ProductId = 2,
                            StoreId   = 1,
                            Quantity  = 50
                        }
                    }
                };
                context.Add(store);
                context.SaveChanges();

                var backend = new StoreBackend(context);
                var prods   = new List <ProductQuantity>()
                {
                    new ProductQuantity()
                    {
                        ProductId = 1,
                        Quantity  = 2
                    },
                    new ProductQuantity()
                    {
                        ProductId = 2,
                        Quantity  = 5
                    }
                };

                orderId = backend.PlaceNewOrder(1, 1, prods).OrderId;
            }
            //Assert
            using (var context = new P0DbContext(options))
            {
                var orders = from ord in context.Orders
                             where ord.OrderId == orderId
                             select ord;

                Assert.Single(orders);
            }
        }
示例#5
0
        private void ManagePlacingOrder()
        {
            //Get locations
            Console.Clear();
            PrintTitle();

            var locations = db.GetAllLocations();

            for (int i = 0; i < locations.Count; i++)
            {
                Console.WriteLine($"{i+1}. {locations[i].Location}");
            }
            //have the user select a location
            int selection;

            do
            {
                Console.Write("Select a store number to view its available products. ");
            } while (int.TryParse(Console.ReadLine(), out selection) && (selection < 0 || selection > locations.Count));
            //Display the products for that location
            Console.Clear();
            Console.WriteLine($"Inventory for location {locations[selection - 1].Location}\n");

            /*
             * if (null == locations[selection - 1].AvailableProducts || locations[selection - 1].AvailableProducts.Count == 0)
             * {
             *  Console.Write("There are no available products at that location. Press any key to return to the locations menu.");
             *  Console.ReadLine();
             *
             * }
             * else
             * {
             */
            foreach (var item in locations[selection - 1].AvailableProducts)
            {
                Console.WriteLine($"{item.ProductId}. {item.ProductDescription} available quantity {item.Quantity}");
            }
            //}

            //Have the user add products and quantites to the order
            Console.WriteLine("To add an item to the order enter an item number followed by the quantity.");
            var items = new List <ProductQuantity>();

            string[] input;
            bool     cont = true;

            do
            {
                var itemAlreadyAdded = false;
                input = Console.ReadLine().Split(' ');
                if (input.Length == 1)
                {
                    if (!(cont != string.IsNullOrEmpty(input[0])))
                    {
                        break;
                    }
                }
                if (input.Length == 2)
                {
                    foreach (var item in items)
                    {
                        if (item.ProductId == int.Parse(input[0]))
                        {
                            itemAlreadyAdded = true;
                            Console.WriteLine("That item has already been added to the order.");
                            break;
                        }
                    }
                    if (!itemAlreadyAdded)
                    {
                        items.Add(new ProductQuantity()
                        {
                            ProductId = int.Parse(input[0]),
                            Quantity  = int.Parse(input[1])
                        });
                    }
                    Console.WriteLine("You can continue to add items or press enter to finish the order.");
                }
                else
                {
                    Console.WriteLine("Enter the product number followed by a space followed by the desired quantity.");
                }
            } while (cont);
            if (DisplayPreOrderTotals(locations[selection - 1], items))
            {
                var orderInfo = db.PlaceNewOrder(locations[selection - 1].StoreId, customer.CustomerId, items);
                DisplayNewOrder(orderInfo);
            }
            else
            {
                Console.WriteLine("Order Canceled.");
            }

            Console.Write("Press any key to return to previous menu.");
            Console.ReadLine();
        }