Exemplo n.º 1
0
        /// <summary>
        /// Lets you update Customer's details if you have necessary authorization.
        /// </summary>
        /// <returns></returns>
        public async Task UpdateTheCustomerAsync()
        {
            // Customer updatedCustomer, string updatedFirstName, string updatedLastName

            Console.WriteLine("\n************************* Welcome to the  Customer Update menu ******************************\n");

            // Check if the current user is admin or not
            if (!await IdendityValidator.IsAdmin())
            {
                string text = "\nUsername and or password failed...\nPlease enter your username and password carefully next time.\nExiting to main menu now....\nAny unauthorized attempts to subvert the system will be logged and reported to the relevant authority.\n";
                foreach (char c in text)
                {
                    Console.Write(c);
                    Thread.Sleep(40);
                }
                return;
            }

            Console.Write("\nEnter Id of the Customer you want update: ");
            var     customerId = Console.ReadLine();
            Program p          = new Program();

            var customerToUpdate = await GetCustomerByIdAsync(customerId);

            if (customerToUpdate != null)
            {
                Console.WriteLine($"You sure want to Update {customerToUpdate.FirstName} {customerToUpdate.LastName} 's details from db? ");
                Console.Write("Write yes and press enter to continue: ");
                var result = Console.ReadLine();
                if (result.ToLower().Trim() != "yes")
                {
                    return;
                }
                else
                {
                    Console.Write("Write the updated first name: ");
                    var updatedFirstName = Console.ReadLine();
                    Console.Write("Write the updated last name: ");
                    var updatedLastName = Console.ReadLine();

                    customerToUpdate.FirstName = updatedFirstName;
                    customerToUpdate.LastName  = updatedLastName;
                    var customerService = Container.GetService <ICustomerRepository>();
                    await customerService.UpdateCustomerAsync(customerToUpdate);

                    Console.Write("Record Succesfully Updated");
                    Console.Write("\nReturning to the main menu...");
                    Thread.Sleep(3000);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deletes Customer form the database if the user has authorization.
        /// </summary>
        /// <returns></returns>
        public async Task DeleteCustomerAsync()
        {
            Console.WriteLine("\n************************* Welcome to the Customer delete menu ******************************\n");

            // Check if the current user is admin or not
            if (!await IdendityValidator.IsAdmin())
            {
                string text = "\nUsername and or password failed...\nPlease enter your username and password carefully next time.\nExiting to main menu now....\nAny unauthorized attempts to subvert the system will be logged and reported to the relevant authority.\n";
                foreach (char c in text)
                {
                    Console.Write(c);
                    Thread.Sleep(40);
                }
                return;
            }

            // Asks for the Customer's Id whom should be deleted.
            Console.Write("Enter Id of the Customer you want delete: ");

            var customerId = Console.ReadLine();

            // flag to disallow GetCustomerByIdAsync method to display message.
            flag = false;
            // Note : customerId of type string gets converted to int inside GetCustomerByIdAsync method
            var customerToDelete = await GetCustomerByIdAsync(customerId);

            var result1         = customerToDelete;
            var customerService = Container.GetService <ICustomerRepository>();

            if (customerToDelete != null)
            {
                Console.WriteLine($"You sure want to delete {customerToDelete.FirstName} {customerToDelete.LastName} with Id {customerToDelete.Id} from db? ");
                var result = Console.ReadLine();
                if (result.ToLower() == "yes")
                {
                    await customerService.DeleteCustomerAsync(customerToDelete.Id);

                    Console.WriteLine($"{customerToDelete.FirstName} {customerToDelete.LastName} deleted.");
                }
            }
            else
            {
                Console.WriteLine("Customer not found");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds Product Inventory to Store (increases quantity).
        /// </summary>
        /// <returns></returns>
        public async Task AddProductInventoryToStoreAsync()
        {
            // Customer updatedCustomer, string updatedFirstName, string updatedLastName

            Console.WriteLine("************************* Welcome to the  Product Inventory Update menu ******************************\n");

            //Check if the current user is admin or not
            if (!await IdendityValidator.IsAdmin())
            {
                string text = "\nUsername and or password failed...\nPlease enter your username and password carefully next time.\nExiting to main menu now....\nAny unauthorized attempts to subvert the system will be logged and reported to the relevant authority.\n";
                foreach (char c in text)
                {
                    Console.Write(c);
                    Thread.Sleep(40);
                }
                return;
            }
            Console.WriteLine("*****************Product Inventory Item********************\n");
            Dictionary <string, string> instruments = new Dictionary <string, string>()
            {
                { "P00001", "Piano" },
                { "P00002", "Flute" },
                { "P00003", "Accordion" },
                { "P00004", "Piccolo" },
                { "P00005", "Trombone" },
                { "P00006", "Violin" },
                { "P00007", "Guitar" },
                { "P00008", "Bagpipes" },
                { "P00009", "Ukulele" },
                { "P00010", "Saxophone" }
            };

            Console.Write("*****Product Code************ Item *******\n");
            foreach (KeyValuePair <string, string> entry in instruments)
            {
                Console.WriteLine($"{entry.Key.PadLeft(15)} \t{entry.Value.PadLeft(10)}");
            }

            // Validation for product code.
            var productCode = "";

            while (true)
            {
                Console.Write("\nEnter Product code of product to update: ");
                productCode = Console.ReadLine();

                Regex regex = new Regex(@"^[P][0]{4}[1-9]|10$");
                Match match = regex.Match(productCode);
                if (!match.Success)
                {
                    Console.WriteLine("Product code did not match: ");
                    continue;
                }
                else
                {
                    break;
                }
            }

            var productToUpdate = await GetProductByCodeAsync(productCode);

            if (productToUpdate != null)
            {
                Console.WriteLine($"You sure you want to add  {productToUpdate.Product.Name} with ,code {productToUpdate.Product.ProductCode} into the inventory? ");
                Console.Write("Write yes and press enter to continue: ");
                var result = Console.ReadLine();
                if (result.ToLower().Trim() != "yes")
                {
                    return;
                }
                else
                {
                    // Choose store location

                    /*
                     *      Florida
                     *      New York
                     *      Texas
                     *      Washington
                     *      California
                     *
                     */

                    string[] locations = { "Florida", "New York", "Texas", "Washington", "California" };

                    var storeLocation = "";
                    int storeId       = 0;
                    while (true)
                    {
                        Console.WriteLine("\nPress 1 for Florida\nPress 2 for New York\nPress 3 for Texas\nPress 4 for Washington\nPress 5 for California\n");
                        storeLocation = Console.ReadLine();
                        if (storeLocation == "1" || storeLocation == "2" || storeLocation == "3" || storeLocation == "4" || storeLocation == "5")
                        {
                            storeId = ParseString.ToInt(storeLocation);
                            if (storeId == 0)
                            {
                                Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid input"); continue;
                            }
                            break;
                        }
                        continue;
                    }
                    var quantity       = "";
                    var parsedQuantity = 0;
                    productToUpdate.StoreId = storeId;
                    while (true)
                    {
                        // Checks if StoreId 0 which means input provided was not valid one.
                        Console.Write($"How many {productToUpdate.Product.Name} do you want to add?: ");
                        quantity       = Console.ReadLine();
                        parsedQuantity = ParseString.ToInt(quantity);
                        if (parsedQuantity != 0)
                        {
                            productToUpdate.LoggedUserId = SessionHolder.UserId;
                            productToUpdate.ChangedDate  = DateTimeOffset.Now.LocalDateTime;
                            productToUpdate.Quantity    += parsedQuantity;
                            break;
                        }
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Invalid input");
                        continue;
                    }


                    var productService = Container.GetService <IProductRepository>();
                    await productService.UpdateProductAsync(productToUpdate);

                    // Empty the session userId
                    SessionHolder.UserId = 0;
                    Console.WriteLine("Product Inventory Updated Successfully");
                    return;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Lets user to create a account, if user has further authorization, user can create an account of admin type.
        /// </summary>
        /// <returns>void</returns>
        public async Task CreateACustomerAsync()
        {
            Console.WriteLine("************************* Welcome to the Customer Creation menu ******************************\n");

            Console.WriteLine("Your email address will be used as the username.");
            // initalizes the variable for holding user inputs.
            var firstName = "";
            var lastName  = "";
            var street    = "";
            var city      = "";
            var state     = "";
            var zip       = "";

            // Check for empty inputs and prompts user again to correct if invalid of empty entries attempted.
            while (true)
            {
                Console.Write("\nEnter your first name: ");
                firstName = Console.ReadLine();
                Console.Write("Enter your last name: ");
                lastName = Console.ReadLine();
                Console.Write("Enter your Street Address: ");
                street = Console.ReadLine();
                Console.Write("Enter your City: ");
                city = Console.ReadLine();
                Console.Write("Enter your State: ");
                state = Console.ReadLine();
                Console.Write("Enter your Zip: ");
                zip = Console.ReadLine();

                // Sanitize the inputs
                if (firstName.Sanitize() == "empty" || lastName.Sanitize() == "empty" || street.Sanitize() == "empty" ||
                    city.Sanitize() == "empty" || state.Sanitize() == "empty" || zip.Sanitize() == "empty")
                {
                    Console.WriteLine("System doesn't accept empty entries \nPlease enter again, or press Q to return to the main menu ");
                    var keyinfo = Console.ReadKey();

                    if (keyinfo.Key == ConsoleKey.Q)
                    {
                        Console.Clear(); return;
                    }
                    continue;
                }
                firstName.Sanitize();
                lastName = lastName.Sanitize();
                street   = street.Sanitize();
                city     = city.Sanitize();
                state    = state.Sanitize();
                zip      = zip.Sanitize();
                break;
            }


            var email       = "";
            var phoneNumber = "";
            // password must atleast contain one digit, one upper case and one lower case with no whitespace
            var  password        = "";
            bool fromPhoneNumber = false;
            bool fromPassword    = false;

            while (true)
            {
                if (fromPhoneNumber == false || fromPassword == false)
                {
                    Console.Write("Enter your email: ");
                    email = Console.ReadLine();
                    // Sanitize email
                    email = email.Sanitize();
                    if (!email.IsValidEmail())
                    {
                        Console.WriteLine("Invalid Email format");
                        continue;
                    }
                }

                if (fromPassword == false)
                {
                    Console.Write("Enter your phone no: ");
                    phoneNumber = Console.ReadLine();
                    // Sanitize phone number
                    phoneNumber = phoneNumber.Sanitize();
                    if (!phoneNumber.IsValidPhoneNumber())
                    {
                        Console.WriteLine("Invalid Phone number format");
                        Console.Write($"\t123-456-7890\n\t(123) 456-7890\n\t123 456 7890\n\t123.456.7890\n\t+91 (123) 456-7890\t");
                        fromPhoneNumber = true;
                        continue;
                    }
                }

                Console.Write("Password must contain atleast contain one digit, one upper case and one lower case and no whitespace\n");
                Console.Write("Enter your preferred password: "******"Enter your password again for confirmation: ");
                var password2 = Orb.App.Console.ReadPassword();

                if (password.IsValidPassword())
                {
                    Console.Clear();
                    var origRow = Console.CursorTop;
                    var origCol = Console.CursorLeft;
                    Console.Clear();
                    if (password != password2)
                    {
                        Console.WriteLine("Sorry your password didn't match");

                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("Enter valid password...");
                    continue;
                }
            }

            // Check if user has Admin privilege
            Console.Write("Are you one of our staff members? \nType yes and press enter if you are, otherwise press any other key to continue : ");
            string staffCheck = "";

            staffCheck = Console.ReadLine();
            if (staffCheck.Trim().ToLower() == "yes")
            {
                if (!await IdendityValidator.IsAdmin())
                {
                    // loops through the character of string on by one and display it in the console.
                    // link : https://stackoverflow.com/questions/27718901/writing-one-character-at-a-time-in-a-c-sharp-console-application
                    // author : https://stackoverflow.com/users/22656/jon-skeet
                    string text = "\nUsername and or password failed...\nPlease enter your username and password carefully next time.\nExiting to main menu now....\nAny unauthorized attempts to subvert the system will be logged and reported to the relevant authority.\n";
                    foreach (char c in text)
                    {
                        Console.Write(c);
                        Thread.Sleep(40);
                    }
                    return;
                }
                else
                {
                    isAdmin = true;
                }
            }

            if (!string.IsNullOrEmpty(firstName) &&
                !string.IsNullOrEmpty(lastName) && !string.IsNullOrEmpty(password))
            {
                // Create a customer from user inserted strings

                var customerAddress = new CustomerAddress {
                    Street = street, City = city, State = state, Zip = zip
                };
                var customer = new Customer {
                    FirstName = firstName, LastName = lastName, Password = password, Email = email, PhoneNo = phoneNumber, UserTypeId = isAdmin ? 1 : 2, CustomerAddress = customerAddress
                };


                // conjure up an interface to service layer
                var insertCustomer = Container.GetService <ICustomerRepository>();

                // pass the customer up the chain through DI injected dbcontext to the service layer
                await insertCustomer.InsertCustomerAsync(customer);

                Console.WriteLine("Customer Creation successful");
                Console.WriteLine($"Welcome to the Console Shopper family {firstName} {lastName}");
                if (isAdmin)
                {
                    isAdmin = false;
                }
            }
            else
            {
                return;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Lets you update product's price if you have necessary authorization.
        /// </summary>
        /// <returns></returns>
        public async Task UpdateProductPriceAsync()
        {
            // Customer updatedCustomer, string updatedFirstName, string updatedLastName

            Console.WriteLine("************************* Welcome to the  Product Inventory Update menu ******************************\n");

            //Check if the current user is admin or not
            if (!await IdendityValidator.IsAdmin())
            {
                string text = "\nUsername and or password failed...\nPlease enter your username and password carefully next time.\nExiting to main menu now....\nAny unauthorized attempts to subvert the system will be logged and reported to the relevant authority.\n";
                foreach (char c in text)
                {
                    Console.Write(c);
                    Thread.Sleep(40);
                }
                return;
            }
            Console.WriteLine("*****************Product Inventory Item********************\n");
            Dictionary <string, string> instruments = new Dictionary <string, string>()
            {
                { "P00001", "Piano" },
                { "P00002", "Flute" },
                { "P00003", "Accordion" },
                { "P00004", "Piccolo" },
                { "P00005", "Trombone" },
                { "P00006", "Violin" },
                { "P00007", "Guitar" },
                { "P00008", "Bagpipes" },
                { "P00009", "Ukulele" },
                { "P00010", "Saxophone" }
            };

            Console.Write("*****Product Code************ Item *******\n");
            foreach (KeyValuePair <string, string> entry in instruments)
            {
                Console.WriteLine($"{entry.Key.PadLeft(15)} \t{entry.Value.PadLeft(10)}");
            }

            // Validation for product code.
            var productCode = "";

            while (true)
            {
                Console.Write("\nEnter Product code of product to update: ");
                productCode = Console.ReadLine();

                Regex regex = new Regex(@"^[P][0]{4}[1-9]|10$");
                Match match = regex.Match(productCode);
                if (!match.Success)
                {
                    Console.WriteLine("Product code did not match: ");
                    continue;
                }
                else
                {
                    break;
                }
            }



            var productToUpdate = await GetProductByCodeAsync(productCode);

            if (productToUpdate != null)
            {
                Console.WriteLine($"Want to change product with Name : {productToUpdate.Product.Name}, Code : {productToUpdate.Product.ProductCode}, Price : {productToUpdate.Product.Price} 's details from db? ");
                Console.Write("Write yes and press enter to continue:");
                var result = Console.ReadLine();
                if (result.ToLower().Trim() != "yes")
                {
                    return;
                }
                else
                {
                    Console.WriteLine("Enter the updated price: ");
                    var price          = Console.ReadLine();
                    var convertedPrice = 0.0M;
                    if (decimal.TryParse(price, out convertedPrice))
                    {
                        productToUpdate.ChangedDate   = DateTimeOffset.Now.LocalDateTime;
                        productToUpdate.LoggedUserId  = SessionHolder.UserId;
                        productToUpdate.Product.Price = convertedPrice;
                    }
                    var productService = Container.GetService <IProductRepository>();
                    await productService.UpdateProductAsync(productToUpdate);

                    SessionHolder.UserId = 0;
                    Console.WriteLine("Product Price Updated Successfully");
                    return;
                }
            }
        }