/// <summary> /// /// </summary> /// <param name="productEntery"></param> public void UpdateProductEntry(ProductEntery productEntery) { ProductEntry currentEntity = _context.ProductEntry.Find(productEntery.Id); ProductEntry newEntity = Mapper.MapProductEnteries(productEntery); _context.Entry(currentEntity).CurrentValues.SetValues(newEntity); }
public void AddProductEntery(ProductEntery product) { ProductEntry entity = Mapper.MapProductEnteries(product); _context.Add(entity); }
/// <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}"); } } }
/// <summary> /// sub menu for creating a new order then adding it to the database /// </summary> /// <param name="data">the DBcontect for the database</param> public static void PlaceOrder(IDataBase data) { List <Customer> customers = data.GetAllCustomers().ToList(); List <Location> locations = data.GetAllLocations().ToList(); if (locations.Count == 0) { Console.WriteLine("There are no locations to make a puchase: Press enter to return to the main menu"); Console.ReadLine(); return; } if (customers.Count == 0) { Console.WriteLine("There are no customers to make a puchase: Press enter to return to the main menu"); Console.ReadLine(); return; } Customer customer = customers[GetValidCustomer(customers)]; Location location = locations[GetValidLocation(locations)]; if (location.Inventory.Count == 0) { Console.WriteLine("The location has no products to sell: Press enter to return to the main menu"); Console.ReadLine(); return; } Order order = new Order() { Id = 0, LocationId = location.Id, CustomerId = customer.Id, }; bool orderReady = false; while (!orderReady) { ProductEntery product = location.Inventory[GetValidProductEntery(location.Inventory)]; Console.WriteLine("Enter the quantity to perchase: "); int quantity; if (int.TryParse(Console.ReadLine(), out quantity)) { Business.ProductOrder productOrder = new Business.ProductOrder() { Name = product.Name, Id = 0, OrderId = 0, ProductId = product.ProductId, Quantity = quantity, PricePerUnit = product.PricePerUnit, }; order.AddProduct(productOrder); } else { Console.WriteLine("Quantity was invalid:"); } Console.WriteLine("---Current order information---"); PrintOrder(order); Console.Write("[y/n] - would you like to add another product to the order: "); if (Console.ReadLine() == "n") { orderReady = true; try { location.PlaceOrder(order); customer.Orders.Add(order); data.AddOrder(order); data.Save(); foreach (ProductEntery p in location.Inventory) { data.UpdateProductEntry(p); } data.UpdateLocation(location); data.UpdateCustomer(customer); data.Save(); PrintOrder(order); Console.Write("Order placed: Press enter to return to the main menu: "); Console.ReadLine(); } catch (ArgumentException ex) { Console.WriteLine($"Error while placing order: {ex.Message}"); Console.Write("Press enter to return to the main menu: "); Console.ReadLine(); } catch (DbUpdateConcurrencyException ex) { Log.Warning("Error in database update {Message}", ex.Message); Console.WriteLine($"Error in updating database: {ex.Message}"); } catch (DbUpdateException ex) { Log.Warning("Error in database update {Message}", ex.Message); Console.WriteLine($"Error in updating database: {ex.Message}"); } } } }