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

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

                        if (address != null)
                        {
                            Console.Clear();
                            Console.WriteLine("\nThe adress you have selected is:\n" +
                                              $"{address.StreetName}\n" +
                                              $"{address.PostalCode} " +
                                              $"{address.City}\n");


                            Console.WriteLine("\nDo you really want to update this address?\n");
                            Console.WriteLine("\nType Y for yes and N for no\n");

                            var yesNo = Console.ReadLine();
                            if (yesNo == "y" || yesNo == "Y")
                            {
                                try
                                {
                                    Console.WriteLine("\nInput the new info\n");

                                    Console.WriteLine("Street name: ");
                                    var streetName = Console.ReadLine();

                                    Console.WriteLine("\nPostal code: ");
                                    var postalCode = Console.ReadLine();

                                    Console.WriteLine("\nCity: ");
                                    var city = Console.ReadLine();


                                    address.StreetName = streetName;
                                    address.PostalCode = postalCode;
                                    address.City       = city;

                                    context.Update(address);
                                    context.SaveChanges();
                                    Console.WriteLine("\nThe address has been updated\n");
                                    Console.WriteLine("\n----------------------------------------\n");

                                    Console.WriteLine("\nWhat would you like to do next?\n" +
                                                      "\n1. Choose another address to update" +
                                                      "\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. The info you have inserted has not been saved.\n");
                                    Console.WriteLine("\n----------------------------------------\n");
                                    MainMenuAddresses();
                                }
                            }

                            else if (yesNo == "n" || yesNo == "N")
                            {
                                Console.Clear();
                                Console.WriteLine("\nOk then, What would you like to do next?\n" +
                                                  "\n1. Choose another address to update" +
                                                  "\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();
                    }
                }
            }
        }
Example #2
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 update an address from the database and includes a TryCatch - in case the input does not work - and a failsafe in case the user has chosen the wrong address
        /// </summary>
        public static void UpdateCustomer()
        {
            using (var context = new OnlineShopDbContext())
            {
                while (true)
                {
                    DisplayCustomers();
                    Console.WriteLine("\nType the GUID of the customer you want to update:\n");
                    try
                    {
                        var input = Guid.Parse(Console.ReadLine());

                        Console.Clear();

                        var a = context.Customers.Find(input);

                        if (a != null)
                        {
                            Console.Clear();
                            Console.WriteLine("\nThe customer you have selected is:\n" +
                                              $"{a.FirstName} " +
                                              $"{a.LastName}\n" +
                                              $"{a.Email}\n");

                            Console.WriteLine("\nDo you really want to update this customer?\n");
                            Console.WriteLine("\nType Y for yes and N for no\n");

                            var yesNo = Console.ReadLine();
                            if (yesNo == "y" || yesNo == "Y")
                            {
                                Console.Clear();
                                Console.WriteLine("\nInput the new info\n");

                                Console.WriteLine("First name: ");
                                var firstName = Console.ReadLine();

                                Console.WriteLine("Last name: ");
                                var lastName = Console.ReadLine();

                                Console.WriteLine("\nPostal code: ");
                                var postalCode = Console.ReadLine();

                                Console.WriteLine("\nEmail: ");
                                var email = Console.ReadLine();

                                Console.WriteLine("\nPhonenumber: ");
                                var phoneNr = Console.ReadLine();


                                a.FirstName = firstName;
                                a.LastName  = lastName;
                                a.Email     = email;
                                a.PhoneNr   = phoneNr;

                                context.Update(a);
                                context.SaveChanges();
                                Console.WriteLine("\nThe customer has been updated\n");
                                Console.WriteLine("\n----------------------------------------\n");

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

                                var choice = Console.ReadLine();

                                Console.Clear();

                                if (choice == "2")
                                {
                                    MainMenuCustomers();
                                }
                            }

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

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

                            else // In case of unknown input
                            {
                                Console.Clear();
                                Console.WriteLine($"\n{input} is not recognized. Try again!\n");
                                Console.WriteLine("\n----------------------------------------\n");
                                MainMenuCustomers();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("\nSomething went wrong. Try again!\n");
                        Console.WriteLine("\n----------------------------------------\n");
                        MainMenuCustomers();
                    }
                }
            }
        }
        /// <summary>
        /// Makes it possible for the user to filter the customers by their first name that is returned as a list (includes a TryCatch)
        /// </summary>
        public static void FilterCustomer()
        {
            using (var context = new OnlineShopDbContext())
            {
                while (true)
                {
                    Console.WriteLine("Type the the first name of the customer/customers you want to display:\n");
                    try
                    {
                        var input = Console.ReadLine();
                        var convertedFirstLetter      = "";
                        var convertedRemainingLetters = "";
                        for (var i = 0; i < input.Length; i++)
                        {
                            if (i == 0)
                            {
                                convertedFirstLetter = Convert.ToString(input[i]).ToUpper();
                            }
                            else
                            {
                                convertedRemainingLetters += Convert.ToString(input[i]).ToLower();
                            }
                        }
                        input = convertedFirstLetter + convertedRemainingLetters;

                        Console.Clear();
                        var customers = context.Customers.Where(x => x.FirstName == input).ToList();

                        if (customers.Count != 0)
                        {
                            Console.WriteLine($"The following customer/customers have {input} as their first name");
                            Console.WriteLine("\n----------------------------------------\n");
                            foreach (var customer in customers)
                            {
                                Console.WriteLine(customer.CustomerId);
                                Console.WriteLine(customer.FirstName);
                                Console.WriteLine(customer.LastName);

                                Console.WriteLine("\n----------------------------------------\n");
                            }
                            Console.WriteLine("What would you like to do next?" +
                                              "\n1. Choose another first name to filter by" +
                                              "\n2. Go back to the customer menu");

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


                            if (choice == "1")
                            {
                                FilterCustomer();
                            }
                            else if (choice == "2")
                            {
                                MainMenuCustomers();
                            }
                            else
                            {
                                Console.WriteLine($"{choice} is not recognized, you will be sent to the customer main menu.");
                                MainMenuCustomers();
                            }
                        }


                        else if (customers.Count == 0)
                        {
                            Console.WriteLine($"There is no customer that has {input} as their first name\n" +
                                              "\nWhat would you like to do?" +
                                              "\n1. Choose another first name to filter by" +
                                              "\n2. Go back to the customer menu");

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


                            if (choice == "1")
                            {
                                FilterCustomer();
                            }
                            else if (choice == "2")
                            {
                                MainMenuCustomers();
                            }
                            else
                            {
                                Console.WriteLine($"{choice} is not recognized, you will be sent to the customer main menu.");
                                MainMenuCustomers();
                            }
                        }

                        else
                        {
                            Console.WriteLine($"\n{input} is not recognized. Try again!\n");
                            Console.WriteLine("----------------------------------------");
                            FilterCustomer();
                        }
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Something went wrong. Try again!");
                        Console.WriteLine("\n----------------------------------------\n");
                        MainMenuCustomers();
                    }
                }
            }
        }
        /// <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();
                    }
                }
            }
        }