/// <summary>
        /// Makes it possible for the user to add a new customer to the database
        /// It has a TryCatch in case something goes wrong and redirects the user to the main menu for the address section
        /// </summary>
        public static void AddCustomer()
        {
            using (var context = new OnlineShopDbContext())
            {
                Console.WriteLine("\nThis is where you add a new customer. Please fill out the form.\n");
                Console.WriteLine("First name: ");
                var firstName = Console.ReadLine();
                Console.WriteLine("Last name: ");
                var lastName = Console.ReadLine();
                Console.WriteLine("Phonenumber: ");
                var phoneNr = Console.ReadLine();
                Console.WriteLine("Email: ");
                var email = Console.ReadLine();
                Console.Clear();

                Console.WriteLine("\nTime to add an address ID.\n");

                AddressMethods.DisplayAddresses();
                Console.WriteLine("\nAddressId(GUID): ");
                var address = Guid.Parse(Console.ReadLine());

                Console.Clear();

                Console.WriteLine($"\nA customer has been added.\n " +
                                  $"\nFollowing information has been saved in the database:\n" +
                                  $"\nFirst name: {firstName}\n" +
                                  $"\nLast name: {lastName}\n" +
                                  $"\nPhone number: {phoneNr}\n" +
                                  $"\nEmail: {email}\n" +
                                  $"\nAddressId: {address}\n");
                Console.WriteLine("\n----------------------------------------\n");



                try
                {
                    var customer = new Customer()
                    {
                        CustomerId = new Guid(),
                        FirstName  = firstName,
                        LastName   = lastName,
                        PhoneNr    = phoneNr,
                        Email      = email,
                        AddressID  = address
                    };

                    context.Customers.Add(customer);
                    context.SaveChanges();
                }

                catch (Exception)
                {
                    Console.Clear();
                    Console.WriteLine("\nSomething went wrong. The info you have inserted has not been saved.\n");
                    Console.WriteLine("\n----------------------------------------\n");
                    MainMenuCustomers();
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Main menu. Includes a TryCatch in case of wrong input.
        /// Also include references to the following methods
        /// newSeed.Seed(),
        /// AddressMethods.MainMenuAddresses(),
        /// CustomerMethods.MainMenuCustomers(),
        /// CombineMethods.DisplayCombined(),
        /// </summary>
        public static void MainMenuStart()
        {
            Console.WriteLine("\nWhat would you like look at?\n");
            Console.WriteLine("0. Seed");
            Console.WriteLine("1. Addresses ");
            Console.WriteLine("2. Customers");
            Console.WriteLine("3. Customer and address");
            Console.WriteLine("4. Exit console app\n");

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

                if (choice == "0")
                {
                    var newSeed = new Seeds();
                    newSeed.Seed();
                }

                else if (choice == "1")
                {
                    AddressMethods.MainMenuAddresses();
                }

                else if (choice == "2")
                {
                    CustomerMethods.MainMenuCustomers();
                }

                else if (choice == "3")
                {
                    CombineMethods.DisplayCombined();
                }
                else if (choice == "4")
                {
                    Environment.Exit(0);
                }
                else
                {
                    Console.WriteLine($"{choice} is not recognized. Try again!");

                    MainMenuStart();
                }
            }
            catch (Exception)
            {
                Console.WriteLine("\nSomething went wrong. Try again!\n");
                Console.WriteLine("\n----------------------------------------\n");
                MainMenuStart();
            } // In case the input is not a string this will prevent the app from shutting down
        }