Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            #region configuration
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");
            // Check DB connection
            // Console.WriteLine(connString);
            IDbConnection conn = new MySqlConnection(connString);
            #endregion

            //Select the SQL Departments Table and load it into the IEnumerable depos
            DapperDepartmentRepository deptrepo = new DapperDepartmentRepository(conn);
            var depos = deptrepo.GetAllDepartments();

            //Select the SQL Products Table and load it into the IEnumerable products
            DapperProductRepository productrepo = new DapperProductRepository(conn);
            var products = productrepo.GetAllProducts();


            //Dispaly the menu and ask the user to select an option
            string userSelection;
            bool   stopAppliction = false;
            while (stopAppliction == false)
            {
                ConsoleLogging.DisplayMenu();
                Console.WriteLine();
                Console.Write("Please enter your selection: ");
                userSelection = Console.ReadLine();
                switch (userSelection)
                {
                case "1":     //Print All Departments
                    depos = deptrepo.GetAllDepartments();
                    ConsoleLogging.PrintDept(depos);
                    break;

                case "2":     //Add a New Department
                    Console.Write("Enter the Department Name: ");
                    deptrepo.InsertDepartment(Console.ReadLine());
                    break;

                case "3":     //Print All Products
                    products = productrepo.GetAllProducts();
                    ConsoleLogging.PrintProducts(products);
                    break;

                case "4":     //Add a New Product
                    Console.Write("Enter the Product Name: ");
                    string pName = Console.ReadLine();
                    Console.Write("Enter the Product Price: ");
                    decimal pprice = Decimal.Parse(Console.ReadLine());
                    Console.Write("Enter the Product CategoryID: ");
                    int pCategoryID = int.Parse(Console.ReadLine());
                    Console.Write("Enter 1 if OnSale or 0 if Not: ");
                    int pOnSale = int.Parse(Console.ReadLine());
                    Console.Write("Enter Stock Level: ");
                    int pStockLevel = int.Parse(Console.ReadLine());
                    productrepo.InsertProduct(pName, pprice, pCategoryID, pOnSale, pStockLevel);
                    break;

                case "5":
                    stopAppliction = true;
                    break;
                }
            }
            Console.WriteLine("Ending Best Buy Database Application");
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            #region Configuration
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");
            #endregion
            //MySqlConnection Object (we can access this class's members using --> conn)
            IDbConnection conn = new MySqlConnection(connString);
            //DapperDepartmentsRepository Object (we can access this class's members using --> repo)
            var repo = new DapperDepartmentRepository(conn);
            Console.WriteLine("The current departments are:");
            //depos = the result from GetAllDepartments()
            var depos = repo.GetAllDepartments();

            PrintDepartments(depos);//Print them to the console using the PrintDepartments() (see below)

            Console.WriteLine();

            Console.WriteLine("Do you want to add a department?");
            var userResponse = Console.ReadLine(); //User's respnse variable declaration

            if (userResponse.ToLower() == "yes")   //Case insensitive validation
            {
                Console.WriteLine("Enter the department's name");
                userResponse = Console.ReadLine();

                Console.WriteLine();

                repo.InsertDepartment(userResponse);    //Here is where we insert a new department using --> userResponse
            }
            PrintDepartments(repo.GetAllDepartments()); //Print out the Departments

            Console.WriteLine();                        //Add some blank spaces for readability
            Console.WriteLine();

            Console.WriteLine("The current products are: ");

            //DapperProductRepository Object (now we can access this class's members using --> prod)
            var prod = new DapperProductRepository(conn);

            var prods = prod.GetAllProducts(); //prods = an IEnumerable<Product> "list"

            PrintProducts(prods);              //Print out the products using the PrintProducts() (see below)

            Console.WriteLine();

            Console.WriteLine("Would you like to add a product?");
            userResponse = Console.ReadLine();//We already declared a user response variable so now we just reset it's value

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine("Enter the product's name");
                var productName = Console.ReadLine();

                Console.WriteLine("Enter the product's price");
                var productPrice = double.Parse(Console.ReadLine()); //We must parse our Console.ReadLine() for a double because price is of type double
                                                                     //and Console.ReadLine() gives us a string

                //For the sake of simplicity we will just list out the category numbers with a Console.WriteLine() --> cw + tab + tab
                Console.WriteLine("Enter the product's categoryID");
                Console.WriteLine();
                Console.WriteLine("1--> Computers");
                Console.WriteLine("2--> Applinces");
                Console.WriteLine("3--> Phones");
                Console.WriteLine("4--> Audio");
                Console.WriteLine("5--> Home Theater");
                Console.WriteLine("6--> Printers");
                Console.WriteLine("7--> Music");
                Console.WriteLine("8--> Games");
                Console.WriteLine("9--> Services");
                Console.WriteLine("10--> Other");
                Console.WriteLine();
                Console.WriteLine("Enter a number from above: ");
                var productCategoryId = int.Parse(Console.ReadLine());            //We must parse our Console.ReadLine for an integer bc CategoryID is an integer

                prod.CreateProduct(productName, productPrice, productCategoryId); //Here our INSERT INTO method is called and it needs 3 arguments matching our 3 parameters types
            }

            PrintProducts(prod.GetAllProducts());//now we print the products again and we see our new product we just created at the bottom of the list

            Console.WriteLine();

            Console.WriteLine("Would you like to update any products?");
            userResponse = Console.ReadLine();

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine("Please enter the products ID");

                var productID = int.Parse(Console.ReadLine());    //Parse for an int

                var productToUpdate = prod.GetProduct(productID); //Here we need a Product so we call the GetProduct() ***This is a bonus and not required*** { see GetProduct() }

                //We name this product --> productToUpdate

                Console.WriteLine("Please enter the products name");
                productToUpdate.Name = Console.ReadLine();

                Console.WriteLine();

                Console.WriteLine("Please enter the products price");
                productToUpdate.Price = double.Parse(Console.ReadLine());//Parse for a double again

                Console.WriteLine();
                Console.WriteLine();

                prod.UpdateProduct(productToUpdate); //Here we pass in the Product we just got back as an argument for the UpdateProduct()
                                                     //The product's name we pass in as an argument is --> productToUpdate

                ShowUpdatedProduct(productToUpdate); //We just want to show the product we just updated not the entire list so we use the ShowUpdatedProduct() (see below)
            }
            Console.WriteLine();

            Console.WriteLine("Would you like to delete any products?");
            userResponse = Console.ReadLine();

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine();
                Console.WriteLine("Please enter the products ID");
                var productID = int.Parse(Console.ReadLine());//Parse for an int

                Console.WriteLine();

                prod.DeleteProduct(productID);    //For the DeleteProduct() we only need to pass in the products ID as an argument; therefore, we pass in --> productID
            }
            PrintProducts(prod.GetAllProducts()); //Finally we print all of the products again to confirm that the product was indeed deleted
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            //This is the connection to the Database
            #region Configeration
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();


            string connString = config.GetConnectionString("DefaultConnection");


            IDbConnection conn = new MySqlConnection(connString);//this is the connection to MySQL database
            #endregion

            //An array for the choices for the different Tables in the DB
            int      userChoice;
            string[] choice = { "Categories", "Departments", "Employees", "Products", "Reviews", "Sales", "Exit" };
            #region ChoiceNumbers
            choice[0] = "1";
            choice[1] = "2";
            choice[2] = "3";
            choice[3] = "4";
            choice[4] = "5";
            choice[5] = "6";
            choice[6] = "7";
            #endregion
            do//a do/while loop create a user choice driven system
            {
                Console.WriteLine("Choose your Table to view.");
                Console.WriteLine("Categories(1), Departments(2), Employees(3), Products(4), Reviews(5), Sales(6) or Exit(7)");
                userChoice = int.Parse(Console.ReadLine());

                //switch statement to hold the function of the different tables
                switch (userChoice)
                {
                case 1:
                    #region Categories
                    //create an instance of the table
                    DapperCategoriesRepository repo1 = new DapperCategoriesRepository(conn);

                    Console.WriteLine("Hello user, here are the current categories!\n");
                    //print out the current categories
                    PrintCat(repo1.GetAllCategories());

                    //another user choice array
                    int      userChoice1;
                    string[] choice1 = { "Add", "Delete", "Go Back" };

                    choice1[0] = "1";
                    choice1[1] = "2";
                    choice1[2] = "3";
                    do    //another  do/while loop create a user choice driven system
                    {
                        Console.WriteLine("(1)Add Category, (2)Delete Category (3)Go Back");
                        userChoice1 = int.Parse(Console.ReadLine());
                        if (userChoice1 == 1)
                        {
                            //this is where the user will input information to add a category
                            Console.WriteLine("What is the name of the new category?");
                            var catName = Console.ReadLine();
                            Console.WriteLine("Department ID (only enter the number from one of the belowe options)");
                            Console.WriteLine("1.Large Electronics\n2.Small Electronics\n3.Media\n4.Other" +
                                              "\n5.Clearance\n6.Movies");
                            int depID = int.Parse(Console.ReadLine());

                            //inserts the user input into the DB
                            repo1.InsertCategory(catName, depID);
                            Console.WriteLine("Categories:");
                            //prints out the updated categories table
                            PrintCat(repo1.GetAllCategories());
                        }
                        else if (userChoice1 == 2)
                        {
                            Console.WriteLine("What is the Category ID for the category you want to remove?");
                            int catID = int.Parse(Console.ReadLine());

                            //takes user input and deletes the category selected based of the ID
                            repo1.DeleteCategory(catID);
                            Console.WriteLine("Categories:");
                            //prints out the updated categories table
                            PrintCat(repo1.GetAllCategories());
                        }
                    } while (userChoice1 != 3);
                    #endregion
                    break;

                case 2:
                    #region Departments
                    //create an instance of the table
                    DapperDepartmentRepository repo2 = new DapperDepartmentRepository(conn);

                    Console.WriteLine("Hello user, here are the current departments!\n");

                    //print out the current departments
                    PrintDep(repo2.GetAllDepartments());

                    //another user choice array
                    int      userChoice2;
                    string[] choice2 = { "Add", "Delete", "Go Back" };

                    choice2[0] = "1";
                    choice2[1] = "2";
                    choice2[2] = "3";
                    do     //another  do/while loop create a user choice driven system
                    {
                        Console.WriteLine("(1)Add Department, (2)Delete Department (3)Go Back");
                        userChoice2 = int.Parse(Console.ReadLine());
                        if (userChoice2 == 1)
                        {
                            Console.WriteLine("What is the name of the new department?");
                            string depName = Console.ReadLine();

                            //inserts the user input into the DB
                            repo2.InsertDepartment(depName);
                            Console.WriteLine("Departments");
                            //prints out the updated depatments table
                            PrintDep(repo2.GetAllDepartments());
                        }
                        else if (userChoice2 == 2)
                        {
                            Console.WriteLine("What is the Category ID for the category you want to remove?");
                            int depID = int.Parse(Console.ReadLine());

                            //takes user input and deletes the depatment selected based off the ID
                            repo2.DeleteDepartment(depID);
                            Console.WriteLine("Departments");
                            //prints out the updated depatments table
                            PrintDep(repo2.GetAllDepartments());
                        }
                    } while (userChoice2 != 3);
                    #endregion
                    break;

                case 3:
                    #region Employees
                    //create an instance of the table
                    DapperEmployeesRepository repo3 = new DapperEmployeesRepository(conn);

                    Console.WriteLine("Hello user, here are the current employees:\n");

                    //print out the current employees
                    PrintEmp(repo3.GetAllEmployees());

                    //another user choice array
                    int      userChoice3;
                    string[] choice3 = { "Add", "Update", "Delete", "Go Back" };

                    choice3[0] = "1";
                    choice3[1] = "2";
                    choice3[2] = "3";
                    choice3[3] = "4";

                    do     //another  do/while loop create a user choice driven system
                    {
                        Console.WriteLine("Add an employee(1), Update an employee(2), Delete an employee(3) or Go Back(4)");
                        userChoice3 = int.Parse(Console.ReadLine());
                        if (userChoice3 == 1)
                        {
                            Console.WriteLine("Enter in the employees information below.\n");

                            Console.Write("First Name: ");
                            string firstName = Console.ReadLine();
                            Console.Write("Middle Initial: ");
                            string middleInitial = Console.ReadLine();
                            Console.Write("Last Name: ");
                            string lastName = Console.ReadLine();
                            Console.Write("Email: ");
                            string emailAddress = Console.ReadLine();
                            Console.Write("Phone Number: ");
                            string phoneNumber = Console.ReadLine();
                            Console.Write("Title: ");
                            string title = Console.ReadLine();
                            Console.Write("Date of Birth => YYYY-MM-DD: ");
                            DateTime dateOfBirth = Convert.ToDateTime(Console.ReadLine());

                            //takes user input to create a new employee in the DB
                            repo3.CreateEmployee(firstName, middleInitial, lastName, emailAddress, phoneNumber, title, dateOfBirth);
                            Console.WriteLine("Employees");
                            //prints out updated employee table
                            PrintEmp(repo3.GetAllEmployees());
                        }
                        else if (userChoice3 == 2)
                        {
                            Console.WriteLine("Enter the Employee ID number for the employee you want to update.");
                            int employeeID = int.Parse(Console.ReadLine());
                            Console.WriteLine("What do you want to update about this employee?");
                            Console.WriteLine("FirstName, MiddleInitial, LastName, EmailAddress, PhoneNumber, Title, or DateOfBirth");
                            string columnPicked = Console.ReadLine();
                            Console.WriteLine("What do you want to change it to?");
                            string value = Console.ReadLine();

                            //alows the user to update specific values of the employee
                            repo3.UpdateEmployee(columnPicked, value, employeeID);
                            Console.WriteLine("Employees");
                            //prints out updated employees table
                            PrintEmp(repo3.GetAllEmployees());
                        }
                        else if (userChoice3 == 3)
                        {
                            Console.WriteLine("Enter the Employee ID for the employee you want to remove.");
                            var employeeID2 = int.Parse(Console.ReadLine());

                            //takes user input and removes the meployee selected based of the ID
                            repo3.DeleteEmployee(employeeID2);
                            Console.WriteLine("Employees");
                            //print out updated employees table
                            PrintEmp(repo3.GetAllEmployees());
                        }
                    } while (userChoice3 != 4);
                    #endregion
                    break;

                case 4:
                    #region Products
                    //create an instance of the table
                    DapperProductRepository repo4 = new DapperProductRepository(conn);

                    Console.WriteLine("Hello user, here are the current products\n");

                    //print out the current products
                    PrintProd(repo4.GetAllProducts());

                    //another user choice array
                    int      userChoice4;
                    string[] choice4 = { "Add", "Update", "Delete", "Go Back" };

                    choice4[0] = "1";
                    choice4[1] = "2";
                    choice4[2] = "3";
                    choice4[3] = "4";

                    do     //another  do/while loop create a user choice driven system
                    {
                        Console.WriteLine("Add a product(1), Update a product(2), Delete a product(3) or Go Back(4)");
                        userChoice4 = int.Parse(Console.ReadLine());
                        if (userChoice4 == 1)
                        {
                            Console.WriteLine("Enter the new products information below.\n");
                            Console.Write("Product Name: ");
                            string prodName = Console.ReadLine();
                            Console.Write("Price: ");
                            decimal price = decimal.Parse(Console.ReadLine());
                            Console.WriteLine("Category ID (only enter the number from one of the belowe options)");
                            Console.WriteLine("1.Computers\n2.Appliances\n3.Phones\n4.Audio" +
                                              "\n5.Home Theater\n6.Printers\n7.Music\n8.Games\n9.Services\n10.Other");
                            int catID = int.Parse(Console.ReadLine());

                            //takes user input to creat a product
                            repo4.CreateProduct(prodName, price, catID);
                            Console.WriteLine("Products");
                            //prints out updated products table
                            PrintProd(repo4.GetAllProducts());
                        }
                        else if (userChoice4 == 2)
                        {
                            Console.WriteLine("Enter the Product ID number for the item you want to update.");
                            int prodID = int.Parse(Console.ReadLine());
                            Console.WriteLine("What do you want to update on the product?");
                            Console.WriteLine("Name, Price, CategoryID, OnSale, StockLevel");
                            string columnPicked = Console.ReadLine();
                            Console.WriteLine("What do you want to change it to?");
                            string value = Console.ReadLine();

                            //takes user input to update a products information
                            repo4.UpdateProduct(columnPicked, value, prodID);
                            Console.WriteLine("Products");
                            //prints out updated products table
                            PrintProd(repo4.GetAllProducts());
                        }
                        else if (userChoice4 == 3)
                        {
                            Console.WriteLine("Enter the ProductID for the product you want to remove.");
                            var prodID2 = int.Parse(Console.ReadLine());

                            //takes user input to remove a product from the table based on the ID
                            repo4.DeleteProduct(prodID2);
                            Console.WriteLine("Products");
                            //prints out updated products table
                            PrintProd(repo4.GetAllProducts());
                        }
                    } while (userChoice4 != 4);
                    #endregion
                    break;

                case 5:
                    #region Reviews
                    //create an instance of the table
                    DapperReviewsRepository repo5 = new DapperReviewsRepository(conn);

                    Console.WriteLine("Hello user, here are all the reviews!\n");

                    //print out all reviews
                    PrintReviews(repo5.GetAllReviews());
                    #endregion
                    break;

                case 6:
                    #region Sales
                    //create an instance of the table
                    DapperSalesRepository repo6 = new DapperSalesRepository(conn);

                    Console.WriteLine("Hello user, here are the current sales\n");

                    //print out the current sales
                    PrintSales(repo6.GetSales());

                    //another user choice array
                    int      userChoice6;
                    string[] choice6 = { "Add", "Update", "Delete", "Go Back" };

                    choice6[0] = "1";
                    choice6[1] = "2";
                    choice6[2] = "3";
                    choice6[3] = "4";

                    do     //another  do/while loop create a user choice driven system
                    {
                        Console.WriteLine("Add a sale(1), Update a sale(2), Delete a sale(3) or Go Back(4)");
                        userChoice6 = int.Parse(Console.ReadLine());
                        if (userChoice6 == 1)
                        {
                            Console.WriteLine("Enter the new sales information below.\n");
                            Console.Write("Price Per Unit: ");
                            decimal pricePerUnit = decimal.Parse(Console.ReadLine());
                            Console.Write("Quantity: ");
                            int quantity = int.Parse(Console.ReadLine());
                            Console.Write("Enter the sale Date => YYYY-MM-DD:");
                            DateTime date = Convert.ToDateTime(Console.ReadLine());
                            Console.Write("ProductID: ");
                            int prodID = int.Parse(Console.ReadLine());

                            //takes user input ot create a sale in the table
                            repo6.CreateSale(pricePerUnit, quantity, date, prodID);
                            Console.WriteLine("Sales");
                            //prints out updated sales table
                            PrintSales(repo6.GetSales());
                        }
                        else if (userChoice6 == 2)
                        {
                            Console.WriteLine("Enter the sales ID number for the sale you want to update.");
                            int saleID = int.Parse(Console.ReadLine());
                            Console.WriteLine("What do you want to update on the sale?");
                            Console.WriteLine("Quantity, PricePerUnit, Date, EmployeeID");
                            string columnPicked = Console.ReadLine();
                            Console.WriteLine("What do you want to change it to?");
                            string value = Console.ReadLine();

                            //takes user input to update a sale in the table
                            repo6.UpdateSale(columnPicked, value, saleID);
                            Console.WriteLine("Sales");
                            //prints out updated sales table
                            PrintSales(repo6.GetSales());
                        }
                        else if (userChoice6 == 3)
                        {
                            Console.WriteLine("Enter the SalesID for the sale you want to remove.");
                            var saleID2 = int.Parse(Console.ReadLine());

                            //takes user input to delete a sale from the table based on the ID
                            repo6.DeleteSale(saleID2);
                            Console.WriteLine("Sales");
                            //prints out updated sales table
                            PrintSales(repo6.GetSales());
                        }
                    } while (userChoice6 != 4);
                    #endregion
                    break;
                }//closes out the loop and terminates the system.
            } while (userChoice != 7);
            Console.WriteLine("Have a wonderful day!");
        }