private static void deleteCategory() { logger.Info("Deleting A category and its products"); var db = new NWConsole_96_SRContext(); var query = db.Categories.OrderBy(p => p.CategoryId); Console.WriteLine("Select the category which you would like to delete"); Console.ForegroundColor = ConsoleColor.DarkRed; foreach (var item in query) { Console.WriteLine($"{item.CategoryId}) {item.CategoryName}"); } Console.ForegroundColor = ConsoleColor.White; int id = int.Parse(Console.ReadLine()); Console.Clear(); logger.Info($"CategoryId {id} selected"); //Categories category = db.Categories.FirstOrDefault(c => c.CategoryId == id); Categories category = db.Categories.Include("Products").FirstOrDefault(c => c.CategoryId == id); Console.WriteLine($"{category.CategoryName} - {category.Description}"); foreach (Products p in category.Products) { db.Products.Remove(p); } db.Categories.Remove(category); db.SaveChanges(); }
private static void listRelatedProducts() { logger.Info("Listing A category and its products"); var db = new NWConsole_96_SRContext(); var query = db.Categories.OrderBy(p => p.CategoryId); Console.WriteLine("Select the category whose products you want to display:"); Console.ForegroundColor = ConsoleColor.DarkRed; foreach (var item in query) { Console.WriteLine($"{item.CategoryId}) {item.CategoryName}"); } Console.ForegroundColor = ConsoleColor.White; int id = int.Parse(Console.ReadLine()); Console.Clear(); logger.Info($"CategoryId {id} selected"); //Categories category = db.Categories.FirstOrDefault(c => c.CategoryId == id); Categories category = db.Categories.Include("Products").FirstOrDefault(c => c.CategoryId == id); Console.WriteLine($"{category.CategoryName} - {category.Description}"); foreach (Products p in category.Products) { Console.WriteLine(p.ProductName); } }
private static void createProduct() { logger.Info("Creating a product"); Products product = new Products(); Console.WriteLine("Enter Product Name:"); product.ProductName = Console.ReadLine(); Console.WriteLine("Enter the Product Supplier Id:"); product.SupplierId = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the Product Category Id:"); product.CategoryId = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the Product Quantity Per Unit:"); product.QuantityPerUnit = Console.ReadLine(); Console.WriteLine("Enter the Product Unit Price:"); product.UnitPrice = decimal.Parse(Console.ReadLine()); Console.WriteLine("Enter the Product Units In Stock:"); product.UnitsInStock = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Enter the Product Units On Order:"); product.UnitsOnOrder = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Enter the Product Reorder Level:"); product.ReorderLevel = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Enter the Product Discontinued:"); product.Discontinued = bool.Parse(Console.ReadLine()); product.Category = null; product.OrderDetails = null; product.Supplier = null; ValidationContext context = new ValidationContext(product, null, null); List <ValidationResult> results = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(product, context, results, true); if (isValid) { var db = new NWConsole_96_SRContext(); // check for unique name if (db.Products.Any(p => p.ProductName == product.ProductName)) { // generate validation error isValid = false; results.Add(new ValidationResult("Name exists", new string[] { "ProductName" })); } else { logger.Info("Validation passed"); db.Products.Add(product); db.SaveChanges(); } } if (!isValid) { foreach (var result in results) { logger.Error($"{result.MemberNames.First()} : {result.ErrorMessage}"); } } }
private static void getProduct() { logger.Info("Getting a single product"); var db = new NWConsole_96_SRContext(); Console.WriteLine("Enter Product Name To Search For"); string productName = Console.ReadLine(); Products product = db.Products.FirstOrDefault(b => b.ProductName == productName); Console.WriteLine($"{product.ProductName}\n - Product Id: {product.ProductId}\n - Supplier Id: {product.SupplierId}\n - Category Id: {product.CategoryId}\n - Quantity Per Unit: {product.QuantityPerUnit}\n - Unit Price: {product.UnitPrice}\n - Units In Stock: {product.UnitsInStock}\n - Units On Order: {product.UnitsOnOrder}\n - Reorder Level: {product.ReorderLevel}\n - Discontinued: {product.Discontinued}\n"); }
private static void deleteProduct() { logger.Info("Delete a single product"); var db = new NWConsole_96_SRContext(); Console.WriteLine("Enter Product Name To Search For And Delete"); string productName = Console.ReadLine(); Products product = db.Products.FirstOrDefault(b => b.ProductName == productName); db.Products.Remove(product); db.SaveChanges(); }
private static void listCategories() { logger.Info("Listing all cateogries"); var db = new NWConsole_96_SRContext(); var query = db.Categories.OrderBy(p => p.CategoryName); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"{query.Count()} records returned"); Console.ForegroundColor = ConsoleColor.Magenta; foreach (var item in query) { Console.WriteLine($"{item.CategoryName} - {item.Description} - {item.CategoryId}"); } Console.ForegroundColor = ConsoleColor.White; }
private static void updateProduct() { logger.Info("Updating a product"); var db = new NWConsole_96_SRContext(); Console.WriteLine("Enter Product Name To Search For"); string productName = Console.ReadLine(); Products product = db.Products.FirstOrDefault(b => b.ProductName == productName); product.ProductName = change("Product Name", product.ProductName); product.SupplierId = Int32.Parse(change("Supplier Id", Convert.ToString(product.SupplierId))); product.CategoryId = Int32.Parse(change("Category Id", Convert.ToString(product.CategoryId))); product.QuantityPerUnit = change("Quantity Per Unit", product.QuantityPerUnit); product.UnitPrice = decimal.Parse(change("Unit Price", Convert.ToString(product.UnitPrice))); product.UnitsInStock = Int16.Parse(change("Units in stock", Convert.ToString(product.UnitsInStock))); product.UnitsOnOrder = Int16.Parse(change("Units On Order", Convert.ToString(product.UnitsOnOrder))); product.ReorderLevel = Int16.Parse(change("Reorder Level", Convert.ToString(product.ReorderLevel))); product.Discontinued = bool.Parse(change("Discountinued", Convert.ToString(product.Discontinued))); ValidationContext context = new ValidationContext(product, null, null); List <ValidationResult> results = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(product, context, results, true); if (isValid) { // check for unique name if (db.Products.Any(p => p.ProductName == product.ProductName)) { // generate validation error isValid = false; results.Add(new ValidationResult("Name exists", new string[] { "ProductName" })); } else { logger.Info("Validation passed"); db.Products.Update(product); db.SaveChanges(); } } if (!isValid) { foreach (var result in results) { logger.Error($"{result.MemberNames.First()} : {result.ErrorMessage}"); } } }
private static void listAllCategoriesAndThierProducts() { logger.Info("Listing All Categories and All Products"); var db = new NWConsole_96_SRContext(); var query = db.Categories.Include("Products").OrderBy(p => p.CategoryId); foreach (var item in query) { Console.WriteLine($"{item.CategoryName}"); foreach (Products p in item.Products) { Console.WriteLine($"\t{p.ProductName}"); } } }
private static void listProducts() { logger.Info("Listing all products"); Console.WriteLine("Would you like to see\n - 1) All Products\n - 2) Discontinued Products\n - 3) Active Products"); String choice = Console.ReadLine(); var db = new NWConsole_96_SRContext(); var query = db.Products.OrderBy(p => p.ProductName); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"{query.Count()} records returned"); Console.ForegroundColor = ConsoleColor.Magenta; if (choice == "1") { foreach (var item in query) { Console.WriteLine($"{item.ProductName} - {item.UnitPrice}"); } Console.ForegroundColor = ConsoleColor.White; } else if (choice == "2") { foreach (var item in query) { if (item.Discontinued) { Console.WriteLine($"{item.ProductName} - {item.UnitPrice} - ( Discontinued )"); } } Console.ForegroundColor = ConsoleColor.White; } else if (choice == "3") { foreach (var item in query) { if (!item.Discontinued) { Console.WriteLine($"{item.ProductName} - {item.UnitPrice}"); } } Console.ForegroundColor = ConsoleColor.White; } else { Console.WriteLine("Bad Choice"); } }
private static void DisplayAllCategoriesAndAllActiveProducts() { logger.Info("Displaying all category and all active products"); var db = new NWConsole_96_SRContext(); var query = db.Categories.Include("Products").OrderBy(p => p.CategoryId); foreach (var item in query) { Console.WriteLine($"{item.CategoryName}"); foreach (Products p in item.Products) { if (!p.Discontinued) { Console.WriteLine($"\t{p.ProductName}"); } } } }
private static void updateCategory() { logger.Info("Updating Category"); var db = new NWConsole_96_SRContext(); Console.WriteLine("Enter Category Name To Search For"); string categoryName = Console.ReadLine(); Categories category = db.Categories.FirstOrDefault(c => c.CategoryName == categoryName); category.CategoryName = change("Category Name", category.CategoryName); category.Description = change("Description", category.Description); ValidationContext context = new ValidationContext(category, null, null); List <ValidationResult> results = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(category, context, results, true); if (isValid) { // check for unique name if (db.Categories.Any(c => c.CategoryName == category.CategoryName)) { // generate validation error isValid = false; results.Add(new ValidationResult("Name exists", new string[] { "CategoryName" })); } else { logger.Info("Validation passed"); db.Categories.Update(category); db.SaveChanges(); } } if (!isValid) { foreach (var result in results) { logger.Error($"{result.MemberNames.First()} : {result.ErrorMessage}"); } } }