/// <summary>
        /// Returns the list of Orders placed at the Location with the given ID.
        /// </summary>
        /// <param name="locationID"></param>
        /// <returns> List<Order> toReturn </returns>
        public List <Domain.Order> GetOrdersByLocationID(int locationID)
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var dbOrders = _context.Set <Order>().Where(i => i.LocationId == locationID).ToList();
            List <Domain.Order> toReturn = new List <Domain.Order>();

            foreach (var o in dbOrders)
            {
                var n = new Domain.Order(o.Id, o.CustomerId, o.LocationId)
                {
                    Time = (DateTimeOffset)o.Time
                };
                var lines = _context.Set <OrderLine>().Where(i => i.OrderId == o.Id).ToList();
                foreach (var i in lines)
                {
                    var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                    var domProduct = new Domain.Product(dbProduct.Name, (decimal)dbProduct.Price);
                    n.SetItemAmount(domProduct, i.Amount);
                }
                toReturn.Add(n);
            }

            return(toReturn);
        }
        /// <summary>
        /// Returns the specific Order matching the given ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns> Order ord </returns>
        public Domain.Order GetOrderByID(int id)
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var o   = _context.Set <Order>().Find(id);
            var ord = new Domain.Order(o.Id, o.CustomerId, o.LocationId)
            {
                Time = (DateTimeOffset)o.Time
            };

            var items = _context.Set <OrderLine>().Where(i => i.OrderId == id).ToList();

            foreach (var i in items)
            {
                var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                var domProduct = new Domain.Product(dbProduct.Name, (decimal)dbProduct.Price);
                ord.SetItemAmount(domProduct, i.Amount);
            }

            return(ord);
        }
        // CRUD Orders

        /// <summary>
        /// Returns the list of all Orders in the database.
        /// </summary>
        /// <returns> List<Order> toReturn </returns>
        public List <Domain.Order> GetAllOrders()
        {
            var dbOrders = _context.Set <Order>().ToList();
            List <Domain.Order> toReturn = new List <Domain.Order>();

            foreach (var o in dbOrders)
            {
                var n = new Domain.Order(o.Id, o.CustomerId, o.LocationId)
                {
                    Time = (DateTimeOffset)o.Time
                };

                var lines = _context.Set <OrderLine>().Where(i => i.OrderId == o.Id).ToList();
                foreach (var i in lines)
                {
                    var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                    var domProduct = new Domain.Product(dbProduct.Id, dbProduct.Name, (decimal)dbProduct.Price);

                    n.SetItemAmount(domProduct, i.Amount);
                }
                toReturn.Add(n);
            }

            return(toReturn);
        }
        /// <summary>
        /// Returns the specific Order matching the given ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns> Order ord </returns>
        public Domain.Order GetOrderByID(int id)
        {
            var o   = _context.Set <Order>().Find(id);
            var ord = new Domain.Order(o.Id, o.CustomerId, o.LocationId)
            {
                Time = (DateTimeOffset)o.Time
            };

            var items = _context.Set <OrderLine>().Where(i => i.OrderId == id).ToList();

            foreach (var i in items)
            {
                var dbProduct  = _context.Set <Product>().Where(p => p.Id == i.ProductId).FirstOrDefault();
                var domProduct = new Domain.Product(dbProduct.Id, dbProduct.Name, (decimal)dbProduct.Price);

                ord.SetItemAmount(domProduct, i.Amount);
            }

            return(ord);
        }
Esempio n. 5
0
        /// <summary>
        /// Allows the user to choose the products they will order now that the customer and location for the order have been chosen.
        /// </summary>
        /// <param name="hs"></param>
        /// <param name="loc"></param>
        /// <param name="customerID"></param>
        /// <returns></returns>
        static Domain.Order PlaceOrderChooseProducts(HelperService hs, Domain.Location loc, int customerID)
        {
            Domain.Order order = new Domain.Order();
            order.LocationID = loc.ID;
            order.CustomerID = customerID;
            string input = "";
            bool   valid = false;

            do
            {
                Console.WriteLine("\nEnter the ID of a product to add to the order:");
                Console.WriteLine("l : View list of products at this location");
                Console.WriteLine("f : Review and finish the order");
                Console.WriteLine("b : Go back a menu");
                Console.WriteLine("q : Quit out of the program");
                input = "";
                input = Console.ReadLine();
                valid = Char.IsLetter(input[0]);
                if (valid)
                {
                    char userInput = input[0];
                    switch (userInput)
                    {
                    case 'l':
                        Console.WriteLine("{0, -3} | {1, -50} {2, -15} {3, -10}", "ID", "Name", "Price per unit", "Amount in stock");
                        Console.WriteLine("-------------------------------------------------------------------------------------");
                        foreach (var kv in loc.Inventory)
                        {
                            Console.WriteLine($"{kv.Key.ID,-3} | {kv.Key.Name, -50} {kv.Key.Price, -15} {kv.Value, -10}");
                        }
                        valid = false;
                        break;

                    case 'f':
                        return(order);

                    case 'b':
                        // go up a level in the menu
                        return(null);

                    case 'q':
                        Environment.Exit(0);
                        break;

                    default:
                        break;
                    }
                }
                else if (input.All(Char.IsDigit))
                {
                    int i = Int32.Parse(input);
                    valid = loc.Inventory.Any(kv => kv.Key.ID == i);
                    if (valid)
                    {
                        Console.WriteLine("\nEnter the amount of the product to order:");
                        Console.WriteLine("b : Go back a menu");
                        Console.WriteLine("q : Quit out of the program");
                        string amt = Console.ReadLine();
                        if (Char.IsLetter(amt[0]))
                        {
                            char u = amt[0];
                            switch (u)
                            {
                            case 'b':
                                valid = false;
                                break;

                            case 'q':
                                Environment.Exit(0);
                                break;
                            }
                        }
                        else if (amt.All(Char.IsDigit))
                        {
                            int amount = Int32.Parse(amt);
                            if (amount <= loc.GetProductAmount(i))
                            {
                                var p = loc.Inventory.Keys.Where(x => x.ID == i).First();
                                Console.WriteLine($"\nSetting an amount for {p.Name}\n");
                                order.SetItemAmount(p, amount);
                                loc.SetProductAmount(p, loc.GetProductAmount(p) - amount);
                                valid = false;
                            }
                            else
                            {
                                Console.WriteLine("Not enough stock of that product to fulfill that order. Please enter a new amount.");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid product ID number. Please enter another number.");
                    }
                }
            } while (!valid);
            return(null);
        }