Ejemplo n.º 1
0
        /// <summary>
        /// Returns the specific Location matching the given name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns> Location loc </returns>
        public Domain.Location GetLocationByName(string name)
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var l = _context.Set <Location>().Where(x => x.Name.ToLower() == name.ToLower()).FirstOrDefault();

            Domain.Location loc = new Domain.Location(l.Id)
            {
                Name = l.Name
            };

            var inventories = _context.Set <Inventory>().Where(i => i.LocationId == loc.ID).ToList();

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

            return(loc);
        }
Ejemplo n.º 2
0
        // CRUD Location

        /// <summary>
        /// Returns the list of all Locations from the database.
        /// </summary>
        /// <returns> List<Location> toReturn </returns>
        public List <Domain.Location> GetAllLocations()
        {
            using var logStream = new StreamWriter("bkdb-logs.txt", append: false)
                  {
                      AutoFlush = true
                  };
            using var _context = GenerateDBContext(logStream);

            var dbLocations = _context.Set <Location>().ToList();
            List <Domain.Location> toReturn = new List <Domain.Location>();

            foreach (var l in dbLocations)
            {
                var n = new Domain.Location(l.Id)
                {
                    Name = l.Name
                };
                var inventories = _context.Set <Inventory>().Where(i => i.LocationId == l.Id).ToList();
                foreach (var i in inventories)
                {
                    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.SetProductAmount(domProduct, i.Amount);
                }
                toReturn.Add(n);
            }

            return(toReturn);
        }
        /// <summary>
        /// Returns the specific Location matching the given name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns> Location loc </returns>
        public Domain.Location GetLocationByName(string name)
        {
            var l = _context.Set <Location>().Where(x => x.Name.ToLower() == name.ToLower()).FirstOrDefault();

            Domain.Location loc = new Domain.Location(l.Id)
            {
                Name = l.Name
            };

            var inventories = _context.Set <Inventory>().Where(i => i.LocationId == loc.ID).ToList();

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

            return(loc);
        }
        // CRUD Location

        /// <summary>
        /// Returns the list of all Locations from the database.
        /// </summary>
        /// <returns> List<Location> toReturn </returns>
        public List <Domain.Location> GetAllLocations()
        {
            var dbLocations = _context.Set <Location>().ToList();
            List <Domain.Location> toReturn = new List <Domain.Location>();

            foreach (var l in dbLocations)
            {
                var n = new Domain.Location(l.Id)
                {
                    Name = l.Name
                };
                var inventories = _context.Set <Inventory>().Where(i => i.LocationId == l.Id).ToList();
                foreach (var i in inventories)
                {
                    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.SetProductAmount(domProduct, i.Amount);
                }
                toReturn.Add(n);
            }

            return(toReturn);
        }
Ejemplo 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);
        }