Example #1
0
        /// <summary>
        /// Makes it possible for the user to delete an address from the database and includes a TryCatch - in case the input does not work - and a failsafe if the user has chosen the wrong address
        /// </summary>
        public static void DeleteAddress()
        {
            using (var context = new OnlineShopDbContext())
            {
                while (true)
                {
                    DisplayAddresses();
                    Console.WriteLine("\nType the GUID of the adress you want to remove from the table:\n");
                    var addressId = Guid.Parse(Console.ReadLine());

                    Console.Clear();

                    var address = context.Addresses.Find(addressId);

                    if (address != null)
                    {
                        Console.WriteLine($"\nThe adress you have selected is:\n{address.StreetName}\n{address.PostalCode} {address.City}\n");
                        Console.WriteLine("\nDo you really want to delete?\n");
                        Console.WriteLine("\nType Y for yes and N for no\n");
                        try
                        {
                            var yesNo = Console.ReadLine();
                            Console.Clear();

                            if (yesNo == "y" || yesNo == "Y")
                            {
                                context.Remove(address);
                                context.SaveChanges();
                                Console.WriteLine("\nThe address is now removed\n");
                                Console.WriteLine("\n----------------------------------------\n");

                                Console.WriteLine("\nWhat would you like to do next?\n" +
                                                  "\n1. Choose another address to delete" +
                                                  "\n2. Go back to the main menu\n");

                                var choice = Console.ReadLine();
                                Console.Clear();
                                if (choice == "2")
                                {
                                    MainMenuAddresses();
                                }
                            }

                            else if (yesNo == "n" || yesNo == "N")
                            {
                                Console.WriteLine("\nOk then, What would you like to do next?\n" +
                                                  "\n1. Choose another address to delete" +
                                                  "\n2. Go back to the main menu\n");

                                var choice = Console.ReadLine();
                                Console.Clear();

                                if (choice == "2")
                                {
                                    MainMenuAddresses();
                                }
                            }
                        }
                        catch (Exception)
                        {
                            Console.Clear();
                            Console.WriteLine("\nSomething went wrong. Try again!\n");
                            Console.WriteLine("\n----------------------------------------\n");
                            MainMenuAddresses();
                        }
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("\nThe Guid is not recognized. Try again!\n");
                        Console.WriteLine("\n----------------------------------------\n");
                    }
                }
            }
        }
        /// <summary>
        /// Makes it possible for the user to delete a customer from the database and includes a TryCatch - in case the input does not work - and a failsafe if the user has chosen the wrong customer
        /// </summary>
        public static void DeleteCustomer()
        {
            using (var context = new OnlineShopDbContext())
            {
                while (true)
                {
                    DisplayCustomers();
                    Console.WriteLine("Type the GUID of the customer you want to remove from the table:\n");
                    try
                    {
                        var customerId = Guid.Parse(Console.ReadLine());
                        Console.Clear();

                        var customer = context.Customers.Find(customerId);

                        if (customer != null)
                        {
                            Console.WriteLine("\nThe customer you have selected is:\n" +
                                              $"{customer.FirstName} {customer.LastName}\n ");
                            Console.WriteLine("Do you really want to delete?");
                            Console.WriteLine("Type Y for yes and N for no\n");
                            var yesNo = Console.ReadLine();
                            if (yesNo == "y" || yesNo == "Y")
                            {
                                context.Remove(customer);
                                context.SaveChanges();
                                Console.WriteLine("\nThe customer is now removed\n");
                                Console.WriteLine("\n----------------------------------------\n");


                                Console.WriteLine("Ok then, What would you like to do next?\n" +
                                                  "\n1. Choose another customer to delete" +
                                                  "\n2. Go back to the customer menu\n");

                                var choice = Console.ReadLine();
                                Console.Clear();
                                if (choice == "2")
                                {
                                    MainMenuCustomers();
                                }
                            }

                            else if (yesNo == "n" || yesNo == "N")
                            {
                                Console.WriteLine("Ok then, What would you like to do next?\n" +
                                                  "\n1. Choose another customer to delete" +
                                                  "\n2. Go back to the customer menu");

                                var choice = Console.ReadLine();
                                Console.Clear();
                                if (choice == "2")
                                {
                                    MainMenuCustomers();
                                }
                            }

                            else
                            {
                                Console.WriteLine($"\n{customerId} is not recognized. Try again!\n");
                                Console.WriteLine("----------------------------------------");
                                MainMenuCustomers();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Something went wrong. Try again!");
                        Console.WriteLine("\n----------------------------------------\n");
                        MainMenuCustomers();
                    }
                }
            }
        }