public DeleteCat() { var db = new NorthwindContext(); Console.Clear(); Console.WriteLine("NORTHWIND Category & Products - Delete Category and related Products\n"); Console.WriteLine("Select the category you want to Delete:"); try { id = int.Parse(DisplayCat.DispCatSel()); // display the list of categories Console.Clear(); Console.WriteLine("NORTHWIND Category & Products - Delete Category and related Products\n"); logger.Info($"CategoryId {id} selected"); Category category = db.Categories.FirstOrDefault(c => c.CategoryId == id); Console.WriteLine($"You chose {category.CategoryName} - {category.Description}"); GetProductCount getProductCount = new GetProductCount(id); // display the product count first foreach (Product p in category.Products) // products enumerated in the category bcause of the list { Console.WriteLine($"\t{p.ProductName}"); } Console.WriteLine("Delete the Category and any related Products? Y or any key to bypass"); string del = Console.ReadLine(); logger.Info($"{del}"); if (del.ToUpper() == "Y") { var dquery = db.Products.Where(p => p.CategoryId == id); foreach (var item in dquery) { db.Products.Remove(item); db.SaveChanges(); } var delcat = db.Categories.FirstOrDefault(c => c.CategoryId == id); db.Categories.Remove(delcat); db.SaveChanges(); } } catch { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("** No Category selected"); Console.ResetColor(); } }
public CategoryMain() { do { var categorymenu = new CategoryMenu(); selection = char.ToLower(categorymenu.GetCatInput()); if (selection.Equals('q')) { break; } choice = selection.ToString(); logger.Info("User choice: {Choice}", choice); if (choice == "1") // display the categories { DisplayCat displayCat = new DisplayCat(); } else if (choice == "2") // Add a category { AddCat addCat = new AddCat(); } else if (choice == "3") // Display the category and its products { DisplayCatProd displayCatProd = new DisplayCatProd(); } else if (choice == "4") { DisplayAllCatProd displayAllCatProd = new DisplayAllCatProd(); // Display all categories and related products } else if (choice == "5") { EditCat editCat = new EditCat(); // Edit a category } else if (choice == "6") { DeleteCat deleteCat = new DeleteCat(); // Edit a category } } while (!selection.Equals('q')); }
public DisplayCatProd() { var db = new NorthwindContext(); Console.Clear(); Console.WriteLine(); Console.WriteLine("NORTHWIND Category & Products - Display Category and related Products\n"); try { int id = int.Parse(DisplayCat.DispCatSel()); // display the list of categories Console.Clear(); Console.WriteLine("NORTHWIND Category & Products - Display Category and related Products\n"); logger.Info($"CategoryId {id} selected"); Category category = db.Categories.FirstOrDefault(c => c.CategoryId == id); Console.WriteLine($"You chose {category.CategoryName} - {category.Description}"); GetProductCount getProductCount = new GetProductCount(id); // display the product count first foreach (Product p in category.Products) // products enumerated in the category bcause of the list { if (!p.Discontinued) { Console.WriteLine($"\t{p.ProductName}"); } } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } catch { logger.Info("Error in Selection"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"** Error - Try again"); Console.ResetColor(); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } }
public EditCat() { var db = new NorthwindContext(); string newname; string newdesc; // try { Console.Clear(); Console.WriteLine(); Console.WriteLine("NORTHWIND Category & Products - Edit CATEGORIES\n"); Console.WriteLine("Select the category you want to edit:"); int id = int.Parse(DisplayCat.DispCatSel()); // display the list of categories Console.Clear(); Console.WriteLine(); Console.WriteLine("NORTHWIND Category & Products - Edit CATEGORIES\n"); logger.Info($"CategoryId {id} selected"); // get the record Category category = db.Categories.FirstOrDefault(c => c.CategoryId == id); // get the record context - is connected to the database - have to get the context first to update it Console.WriteLine($"You chose {category.CategoryName} - {category.Description}\n"); logger.Info($" {category.CategoryName} - {category.Description}"); Console.WriteLine("Edit Category Name? Y or press any key to bypass"); string edit = Console.ReadLine(); logger.Info($"{edit}"); if (edit.ToLower() == "y") { badentry = true; do { Console.WriteLine("Enter a new Category name"); s = Console.ReadLine(); newname = s; // get the new name if (CustomMethod.IsBlank(s)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("** Must enter something"); logger.Info("blank category entered"); Console.ResetColor(); } else { logger.Info($"{newname}"); badentry = false; logger.Info("Validation passed"); category.CategoryName = newname; break; } } while (badentry); } else { category.CategoryName = category.CategoryName; } Console.WriteLine("Edit Category Description? Y or press any key to bypass"); edit = Console.ReadLine(); // get the new description logger.Info($"{edit}"); if (edit.ToLower() == "y") { badentry = true; do { Console.WriteLine("Enter new Category description"); s = Console.ReadLine(); newdesc = s; if (CustomMethod.IsBlank(s)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("** Must enter something"); logger.Info("blank category desc entered"); Console.ResetColor(); } else { badentry = false; category.Description = newdesc; logger.Info($"{newdesc}"); break; } } while (badentry); } else { category.Description = category.Description; } ValidationContext context = new ValidationContext(category, null, null); // what do I want to validate? = category put category in our context List <ValidationResult> results = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(category, context, results, true); // validate category and return it to results = bool if (!isValid) { foreach (var result in results) { logger.Error($"{result.MemberNames.First()} : {result.ErrorMessage}"); Console.WriteLine($"ERROR: {result.ErrorMessage}"); Console.ResetColor(); } } else { db.SaveChanges(); } } catch { Console.ForegroundColor = ConsoleColor.Red; logger.Error($"no selection made"); Console.WriteLine($"No Category Selected"); Console.ResetColor(); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }