Ejemplo n.º 1
0
        /// <summary>
        /// sub menu for adding products to locations
        /// only products that have been added to the databass can be added to locations
        /// products are stored as ProductEntery in Locations that is they also have a quantity field
        /// </summary>
        /// <param name="data">the DBcontect for the database</param>
        public static void AddProductToLocation(IDataBase data)
        {
            List <Location> locations = data.GetAllLocations().ToList();
            List <Product>  products  = data.GetAllProducts().ToList();

            if (locations.Count() == 0)
            {
                Console.Write("There are no locations to add product to: Press enter to continue");
                Console.ReadLine();
            }
            else if (products.Count() == 0)
            {
                Console.Write("There are no products to add product to: Press enter to continue");
                Console.ReadLine();
            }
            else
            {
                Location location = locations[GetValidLocation(locations)];
                Product  product  = products[GetValidProduct(products)];

                bool isValid  = false;
                int  quantity = 0;
                while (!isValid)
                {
                    Console.Write("Enter a quantity to add: ");

                    if (int.TryParse(Console.ReadLine(), out quantity))
                    {
                        isValid = true;
                    }
                    else
                    {
                        Console.WriteLine("Quantity was invalid");
                    }
                }

                try
                {
                    ProductEntery newProductEntery = new ProductEntery()
                    {
                        Id           = 0,
                        Name         = product.Name,
                        LocationId   = location.Id,
                        ProductId    = product.Id,
                        Quantity     = quantity,
                        PricePerUnit = product.CostPerUnit,
                    };

                    location.AddProduct(newProductEntery);
                    data.AddProductEntery(newProductEntery);
                    data.Save();

                    data.UpdateLocation(location);
                    data.Save();

                    Console.Write("Product added: Press enter to continue ");
                    Console.ReadLine();
                }
                catch (ArgumentException ex)
                {
                    Log.Warning("Error in database update {Message}", ex.Message);
                    Console.WriteLine($"Error in creating new location: {ex.Message}");
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    Log.Warning("Error in database update {Message}", ex.Message);
                    Console.WriteLine($"Error in creating new location: {ex.Message}");
                }
                catch (DbUpdateException ex)
                {
                    Log.Warning("Error in database update {Message}", ex.Message);
                    Console.WriteLine($"Error in creating new location: {ex.Message}");
                }
            }
        }