public void DeleteCategoryAndProducts() { var database = new NwContext(); var enteredCategory = new Category(); var categoryList = database.Categories.OrderBy(c => c.CategoryName); Console.WriteLine($"{"Name:",-20}{"CategoryID:"}\n{"-----",-20}{"-----------"}"); foreach (var category in categoryList) { Console.WriteLine($"{category.CategoryName,-20}{category.CategoryId}"); } Console.Write("\n\nPlease Select A Category From The List" + "\nEnter Name Here--> "); enteredCategory.CategoryName = Console.ReadLine(); var categorySearch = database.Categories.Where(c => c.CategoryName == enteredCategory.CategoryName); var productList = database.Products.OrderBy(p1 => p1.ProductID); List <Product> productName = new List <Product>(); foreach (var p in productList) { productName.Add(p); } List <Category> categories = new List <Category>(); foreach (var c in categorySearch) { categories.Add(c); } foreach (var c in categories) { Console.WriteLine($"\n\n{"Category Name:",-15}\n{"--------------"}\n{c.CategoryName}\n"); Console.WriteLine($" {"Product Name:",-20}\n {"-------------"}"); foreach (var p in productName) { if (c.CategoryId == p.CategoryId) { Console.WriteLine($" {p.ProductName}"); } } Console.Write("The Category And Its Products Will Be Deleted\nAre You Sure?\n1. Yes\n2. No\nEnter Choice Here--> "); Int32.TryParse(Console.ReadLine(), out var deletionChoice); if (deletionChoice == 1) { foreach (var p in productName) { if (c.CategoryId == p.CategoryId) { database.Products.Remove(p); } } database.Categories.Remove(c); database.SaveChanges(); nLogger.Info($"User Removed{c.CategoryName} And All Its Products From The Database"); Console.WriteLine("Press Any Key To Continue"); Console.ReadKey(); } } }
public void AddOrUpdateProductToDatabase(Product product) { try { ValidationContext context = new ValidationContext(product, null, null); List <ValidationResult> results = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(product, context, results, true); if (isValid == true) { var database = new NwContext(); database.Products.AddOrUpdate(product); database.SaveChanges(); nLogger.Info($"New or Updated Product: {product} Vaildated And Added To The Database @ {DateTime.Now}"); Console.WriteLine("Press Any Key To Continue"); Console.ReadKey(); } else { foreach (var result in results) { nLogger.Error($"{result.MemberNames.First()} : {result.ErrorMessage}"); } } } catch (Exception ex) { Console.WriteLine(ex); throw; } }
public void DeleteAProduct() { var database = new NwContext(); DisplayAllProducts(); Console.WriteLine("\n\nWhich Product Would You Like To Delete?\nEnter ID Number From List Above--> "); Int32.TryParse(Console.ReadLine(), out var idNumber); var productSearch = database.Products.Where(p => p.ProductID == idNumber); Console.Clear(); if (productSearch.Any() == true) { foreach (var p in productSearch) { Console.WriteLine($"Product Selected For Deletion:" + $"\n\n{"Name--------------->",-22}{p.ProductName,-10}" + $"\n{"Unit Price -------->",-22}{p.UnitPrice,-10}" + $"\n{"Quantity Per Unit-->",-22}{p.QuantityPerUnit,-10}" + $"\n{"Units On Order----->",-22}{p.UnitsOnOrder,-10}" + $"\n{"Wholesale Price---->",-22}{p.WholeSalePrice,-10}" + $"\n{"Units In Stock----->",-22}{p.UnitsInStock,-10}" + $"\n{"Units On Order----->",-22}{p.UnitsOnOrder,-10}" + $"\n{"Reorder Level------>",-22}{p.ReorderLevel,-10}" + $"\n{"Discontinued?------>",-22}{p.Discontinued,-10}" + $"\n{"CategoryID--------->",-22}{p.CategoryId,-10}" + $"\n{"SupplierID--------->",-22}{p.SupplierId,-10}"); database.Products.Remove(p); nLogger.Info($"Product: {p.ProductName} Has Been Removed From The Database"); Console.ReadKey(); } database.SaveChanges(); } else if (productSearch.Any() == false) { nLogger.Error("User Entered ProductID Not In Database"); } }
public void AddOrUpdateCategory(Category newCategory) { var database = new NwContext(); ValidationContext context = new ValidationContext(newCategory, null, null); List <ValidationResult> results = new List <ValidationResult>(); var notValid = Validator.TryValidateObject(newCategory, context, results, true); if (notValid == true) { database.Categories.AddOrUpdate(newCategory); database.SaveChanges(); nLogger.Info($"New or Updated Category: {newCategory} Vaildated And Added To The Database @ {DateTime.Now}"); Console.WriteLine("Press Any Key To Continue"); Console.ReadKey(); } else { foreach (var result in results) { nLogger.Error($"{result.MemberNames.First()} : {result.ErrorMessage}"); } } }
public Product GetProductInfo() { var database = new NwContext(); Product newProduct = new Product(); var exit = 0; do { Console.Write("Enter Product Name--> "); var potentialName = Console.ReadLine(); var productNameSearch = database.Products.Where(p => p.ProductName == potentialName); if ((productNameSearch.Any() == true)) { nLogger.Error("User Entered A Product Name That Already Exists"); } else { newProduct.ProductName = potentialName; exit = 1; } } while(exit == 0); Console.Clear(); Console.Write("Enter Quantity Per Unit--> "); newProduct.QuantityPerUnit = Console.ReadLine(); Console.Clear(); Console.Write("Enter Unit Price--> "); Decimal.TryParse(Console.ReadLine(), out decimal unitPrice); newProduct.UnitPrice = unitPrice; Console.Clear(); Console.Write("Enter Wholesale Price--> "); Decimal.TryParse(Console.ReadLine(), out decimal wholesalePrice); newProduct.WholeSalePrice = wholesalePrice; Console.Clear(); Console.Write("Enter Units In Stock--> "); Int16.TryParse(Console.ReadLine(), out Int16 unitsInStock); newProduct.UnitsInStock = unitsInStock; Console.Clear(); Console.Write("Enter Units On Order--> "); Int16.TryParse(Console.ReadLine(), out Int16 unitsOnOrder); newProduct.UnitsOnOrder = unitsOnOrder; Console.Clear(); Console.Write("Enter the Re-Order Level--> "); Int16.TryParse(Console.ReadLine(), out Int16 reOrderLevel); newProduct.ReorderLevel = reOrderLevel; Console.Clear(); Console.Write("Is This Product Discontinued?\n1. Yes\n2. No\nEnter Number Here--> "); Int32.TryParse(Console.ReadLine(), out int discontinuedChoice); switch (discontinuedChoice) { case 1: newProduct.Discontinued = true; Console.Clear(); break; case 2: newProduct.Discontinued = false; Console.Clear(); break; } var enteredCategory = new Category(); var categoryList = database.Categories.OrderBy(c => c.CategoryName); Console.WriteLine($"{"Name:",-20}{"CategoryID:"}\n{"-----",-20}{"-----------"}"); foreach (var category in categoryList) { Console.WriteLine($"{category.CategoryName,-20}{category.CategoryId}"); } Console.Write("\n\nPlease Select A Category From The List" + "\nIf A New Category Is Needed, Enter The Name, And The Category Creation Operation Will Begin" + "\nEnter Name Here--> "); enteredCategory.CategoryName = Console.ReadLine(); var categorySearch = database.Categories.Any(c => c.CategoryName == enteredCategory.CategoryName); if (categorySearch == false) { Console.WriteLine($"\nA New Category Will Be Created Named: {enteredCategory.CategoryName}"); Console.Write("Enter the Category Description[Required]--> "); enteredCategory.Description = Console.ReadLine(); Console.Clear(); ValidationContext context = new ValidationContext(enteredCategory, null, null); List <ValidationResult> results = new List <ValidationResult>(); var notValid = Validator.TryValidateObject(enteredCategory, context, results, true); database.Categories.Add(enteredCategory); database.SaveChanges(); nLogger.Info($"New Category {enteredCategory.CategoryName} Has Been Validated And Added To The Database."); var category = database.Categories.Where(c => c.CategoryName == enteredCategory.CategoryName); foreach (var c in category) { newProduct.CategoryId = c.CategoryId; } Console.WriteLine($"The Category ID Is: {newProduct.CategoryId}"); Console.ReadKey(); Console.Clear(); } else { var category = database.Categories.Where(c => c.CategoryName == enteredCategory.CategoryName); foreach (var c in category) { newProduct.CategoryId = c.CategoryId; } Console.WriteLine($"The Category ID Is: {newProduct.CategoryId}\nPress Any Key To Continue"); Console.ReadKey(); Console.Clear(); } var enteredSupplier = new Supplier(); var SupplierList = database.Suppliers.OrderBy(s => s.CompanyName); Console.WriteLine($"{"Name:",-40}{"SupplierID:"}\n{"-----",-40}{"-----------"}"); foreach (var supplier in SupplierList) { Console.WriteLine($"\n{supplier.CompanyName,-40}{supplier.SupplierId}"); } Console.Write("\n\nPlease Select A Supplier From The List Above" + "\nIf A New Supplier Is Needed, Enter A Name Not On The List, And The Supplier Creation Operation Will Begin" + "\nEnter Name Here--> "); enteredSupplier.CompanyName = Console.ReadLine(); var supplierSearch = database.Suppliers.Any(s => s.CompanyName == enteredSupplier.CompanyName); if (supplierSearch == false) { Console.Clear(); Console.WriteLine($"\nA New Supplier Will Be Created Named: {enteredSupplier.CompanyName}"); Console.Write("\nEnter the Supplier Contact Name[Required]--> "); enteredSupplier.ContactName = Console.ReadLine(); Console.Clear(); Console.Write("Enter the Supplier Contact Title[Not Required]--> "); if (Console.ReadLine() == "") { } else { enteredSupplier.ContactTitle = Console.ReadLine(); } Console.Clear(); Console.Write("Enter the Supplier Address[Not Required]--> "); if (Console.ReadLine() == "") { } else { enteredSupplier.Address = Console.ReadLine(); } Console.Clear(); Console.Write("Enter the Supplier City[Not Required]--> "); if (Console.ReadLine() == "") { } else { enteredSupplier.City = Console.ReadLine(); } Console.Clear(); Console.Write("Enter the Supplier Region[Not Required]--> "); if (Console.ReadLine() == "") { } else { enteredSupplier.Region = Console.ReadLine(); } Console.Clear(); Console.Write("Enter the Supplier Postal Code[Not Required]--> "); if (Console.ReadLine() == "") { } else { enteredSupplier.PostalCode = Console.ReadLine(); } Console.Clear(); Console.Write("Enter the Supplier Country[Not Required]--> "); if (Console.ReadLine() == "") { } else { enteredSupplier.Country = Console.ReadLine(); } Console.Clear(); Console.Write("Enter the Supplier Phone Number[Not Required]--> "); if (Console.ReadLine() == "") { } else { enteredSupplier.Phone = Console.ReadLine(); } Console.Clear(); Console.Write("Enter the Supplier Fax Number[Not Required]--> "); if (Console.ReadLine() == "") { } else { enteredSupplier.Fax = Console.ReadLine(); } Console.Clear(); ValidationContext context = new ValidationContext(enteredSupplier, null, null); List <ValidationResult> results = new List <ValidationResult>(); var notValid = Validator.TryValidateObject(enteredSupplier, context, results, true); database.Suppliers.Add(enteredSupplier); database.SaveChanges(); nLogger.Info($"New Supplier: {enteredSupplier.CompanyName} Has Been Validated And Added To The Database."); var supplier = database.Suppliers.Where(s => s.CompanyName == enteredSupplier.CompanyName); foreach (var s in supplier) { newProduct.SupplierId = s.SupplierId; } Console.WriteLine($"The Supplier ID Is: {newProduct.SupplierId}\nPress Any Key To Continue"); Console.ReadKey(); Console.Clear(); } else { var supplier = database.Suppliers.Where(s => s.CompanyName == enteredSupplier.CompanyName); foreach (var s in supplier) { newProduct.SupplierId = s.SupplierId; } Console.WriteLine($"The Supplier ID Is: {newProduct.SupplierId}\nPress Any Key To Continue"); Console.ReadKey(); Console.Clear(); } return(newProduct); }