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

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

            IDbConnection connection = new MySqlConnection(connString);
            var           repo       = new DapperDepartmentRepository(connection);

            // var repo = new DepartmentRepository(connString);
            var departments = repo.GetAllDepartments();

            foreach (var dept in departments)
            {
                Console.WriteLine($"{dept.Name,-20} {dept.DepartmentId}");
            }

            Console.WriteLine();

            //repo.UpdateDepartment("Notebooks", "4");
            //foreach (var dept in departments)
            //{
            //    Console.WriteLine($"{dept.Name, -20} {dept.ID}");
            //}
        }
Exemplo n.º 2
0
        public static void DepartmentUpdate()
        {
            var repo = new DapperDepartmentRepository(conn);

            Console.WriteLine($"Would you like to update a department? yes or no");

            if (Console.ReadLine().ToUpper() == "YES")
            {
                Console.WriteLine($"What is the ID of the Department you would like to update?");

                var id = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine($"What would you like to change the name of the department to?");

                var newName = Console.ReadLine();

                repo.UpdateDepartment(id, newName);

                var depts = repo.GetAllDepartments();

                foreach (var item in depts)
                {
                    Console.WriteLine($"{item.DepartmentID} {item.Name}");
                }
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");
            //getting the value from DefaultConnection

            //var repo = new DepartmentRepository(connString); //Without Dapper

            IDbConnection conn = new MySqlConnection(connString);
            var           repo = new DapperDepartmentRepository(conn);

            Console.WriteLine("Type a new Department name:");

            var newDepartment = Console.ReadLine();

            repo.InsertDepartment(newDepartment);

            var departments = repo.GetAllDepartments();

            foreach (var dept in departments)
            {
                Console.WriteLine(dept.Name);
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string        connString = config.GetConnectionString("DefaultConnection");
            IDbConnection conn       = new MySqlConnection(connString);
            var           repo       = new DapperDepartmentRepository(conn);

            var departments = repo.GetAllDepartments();

            repo.InsertDepartment("Perfume");
            foreach (var item in departments)
            {
                Console.WriteLine($"{item.DepartmentID} {item.Name}");
            }



            Console.WriteLine("asdfa");
            var repo2    = new DapperProductRepository(conn);
            var products = repo2.GetAllProducts();

            Console.WriteLine("asdfasdfasdfa");

            foreach (var item in products)
            {
                Console.WriteLine($"{item.ProductId} {item.Name} {item.Price} {item.CategoryID} {item.OnSale} {item.StockLevel}");
            }

            repo2.CreateProduct("Mac Book", 2000, 2);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

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

            IDbConnection conn = new MySqlConnection(connString);


            //-------

            var departmentRepo = new DapperDepartmentRepository(conn);

            Console.WriteLine("Type a new Department name");

            var newDepartment = Console.ReadLine();

            departmentRepo.InsertDepartment(newDepartment);

            var departments = departmentRepo.GetAllDepartments();

            foreach (var dept in departments)
            {
                Console.WriteLine($"{dept.DepartmentID} {dept.Name}");
            }

            //-------


            var productRepo = new DapperProductRepository(conn);

            Console.WriteLine("Type a new Product name");

            var newProduct = Console.ReadLine();

            Console.WriteLine("Type the Price");

            var newPrice = double.Parse(Console.ReadLine());

            Console.WriteLine("Type the Category ID");

            var newCategoryId = int.Parse(Console.ReadLine());

            productRepo.InsertProduct(newProduct, newPrice, newCategoryId);

            var products = productRepo.GetAllProducts();

            foreach (var item in products)
            {
                Console.WriteLine($"{item.ProductID} {item.Name}");
            }
        }
Exemplo n.º 6
0
        public static void ListDepartments()
        {
            var repo = new DapperDepartmentRepository(conn);

            var departments = repo.GetAllDepartments();

            foreach (var item in departments)
            {
                Console.WriteLine($"{item.DepartmentID} {item.Name}");
            }
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            #region Configuration
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");
            // Console.WriteLine(connString);
            #endregion

            IDbConnection conn = new MySqlConnection(connString);

            DapperDepartmentRepository departmentRepo = new DapperDepartmentRepository(conn);

            Console.WriteLine("Hello user, here are the current departments: ");
            Console.WriteLine("Please press Enter. . .");
            Console.ReadLine();

            var departments = departmentRepo.GetAllDepartments();
            Print(departments);

            Console.WriteLine("Do you want to add a department?");
            string userResponse = Console.ReadLine();

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine("What is the name of the new department?");
                userResponse = Console.ReadLine();

                departmentRepo.InsertDepartment(userResponse);
                Print(departmentRepo.GetAllDepartments());
            }

            // PRODUCTS
            Console.WriteLine("Hello user, here are the current products: ");
            Console.WriteLine("Please press Enter. . .");
            Console.ReadLine();
            DapperProductRepository productRepo = new DapperProductRepository(conn);

            // Create, Update, and Delete Methods
            // productRepo.CreateProduct("newProduct", 20, 1);
            // productRepo.UpdateProductName(942, "newStuff");
            // productRepo.DeleteProduct(942);

            // Read Method
            // GetAllProducts belongs to the instance of the DapperProductRepository class
            var products = productRepo.GetAllProducts();
            PrintProducts(products);

            Console.WriteLine("Have a great day!");
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

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

            IDbConnection conn = new MySqlConnection(connString);

            var repo = new DapperDepartmentRepository(conn);


            Console.WriteLine("Hello user, here are the current departments:");
            Console.WriteLine("Please press enter . . .");
            Console.ReadLine();

            var departments = repo.GetAllDepartments();

            Print(departments);

            Console.WriteLine("Do you want to add a department????");
            string userResponse = Console.ReadLine();

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine("What is the name of your new Department??");
                userResponse = Console.ReadLine();

                repo.InsertDepartment(userResponse);
                Print(repo.GetAllDepartments());
            }

            Console.WriteLine("Have a great day.");
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            #region
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");
            //Console.WriteLine(connString);
            #endregion

            IDbConnection conn = new MySqlConnection(connString);
            DapperDepartmentRepository departmentRepo = new DapperDepartmentRepository(conn);
            DapperProductRepository    productRepo    = new DapperProductRepository(conn);

            Console.WriteLine("Hello user, would you like to view the departments or products?");
            string userPick = Console.ReadLine();


            if (userPick.ToLower() == "department" || userPick.ToLower() == "departments")
            {
                Console.WriteLine("Here are the current departments:");
                //Console.WriteLine("Please press enter...");
                Console.WriteLine();
                var depos = departmentRepo.GetAllDepartments();

                PrintDepartment(depos);

                Console.WriteLine("Do you want to add a department?");
                string userResponse = Console.ReadLine();

                if (userResponse.ToLower() == "yes")
                {
                    Console.WriteLine("What is the name of your new Department??");
                    userResponse = Console.ReadLine();
                    departmentRepo.InsertDepartment(userResponse);
                    PrintDepartment(departmentRepo.GetAllDepartments());
                }
            }
            if (userPick.ToLower() == "product" || userPick.ToLower() == "products")
            {
                Console.WriteLine("Here are the current products:");
                Console.WriteLine();
                var depos = productRepo.GetAllProducts();

                PrintProducts(depos);

                Console.WriteLine("Do you want to add a product?");
                string userRepsonse = Console.ReadLine();

                if (userRepsonse.ToLower() == "yes")
                {
                    Console.WriteLine("What is the Name of your new Product?");
                    string newProductName = Console.ReadLine();
                    Console.WriteLine("What is the Price of your new Product?");
                    var newProductPrice = double.Parse(Console.ReadLine());
                    Console.WriteLine("What is the CategoryID of your new Product?");
                    int newProductCategory = int.Parse(Console.ReadLine());

                    productRepo.CreateProduct(newProductName, newProductPrice, newProductCategory);
                    PrintProducts(productRepo.GetAllProducts());
                }
            }

            Console.WriteLine("Have a great day.");
        }
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            IDbConnection conn = new MySqlConnection(config.GetConnectionString("DefaultConnection"));

            bool quit = false;

            do
            {
                Console.WriteLine("\nNew Department:    D,    New Product:     P\n" +
                                  "Remove Department: X,    Update Product:  U\n" +
                                  "Quit:              Q,    Remove Product:  R\n");

                switch (Console.ReadLine().ToLower())
                {
                case "d":
                {
                    var repo = new DapperDepartmentRepository(conn);

                    Console.WriteLine("Enter a new department name:");

                    var newDepartment = Console.ReadLine();

                    repo.InsertDepartment(newDepartment);
                    Console.WriteLine();

                    foreach (var item in repo.GetAllDepartments())
                    {
                        Console.WriteLine(item.Name);
                    }
                } break;

                case "x":
                {
                    var repo = new DapperDepartmentRepository(conn);
                    foreach (var item in repo.GetAllDepartments())
                    {
                        Console.WriteLine(item.Name);
                    }

                    Console.WriteLine("\nEnter name of Department to remove:");
                    string temp = Console.ReadLine();
                    if (null == repo.GetDepartment(temp))
                    {
                        Console.WriteLine("Invalid Department Name");
                        continue;
                    }

                    Console.WriteLine($"Deleting {temp} department\n");
                    repo.DeleteDepartment(temp);
                    foreach (var item in repo.GetAllDepartments())
                    {
                        Console.WriteLine(item.Name);
                    }
                } break;

                case "q": quit = true; break;

                case "p":
                {
                    Console.WriteLine("Add a new product (category = \'Other\'):");
                    var otherName = Console.ReadLine();
                    Console.WriteLine("Price:");
                    var otherPrice = Double.Parse(Console.ReadLine());

                    var prodRepo = new DapperProductRepository(conn);
                    prodRepo.CreateProduct(otherName, otherPrice, 10);

                    Console.WriteLine($"\n\nFind new {otherName} at the bottom:");
                    foreach (var item in prodRepo.GetAllProducts())
                    {
                        Console.WriteLine($"{item.Name}: ${item.Price}, ID {item.ProductID}");
                    }
                } break;

                case "u":
                {
                    var prodRepo = new DapperProductRepository(conn);
                    foreach (var item in prodRepo.GetAllProducts())
                    {
                        Console.WriteLine($"{item.Name}: ${item.Price}, ID {item.ProductID}");
                    }
                    Console.WriteLine("\nProduct ID to change:");

                    int changeProductID;
                    if (Int32.TryParse(Console.ReadLine(), out changeProductID) == false)
                    {
                        Console.WriteLine("Invalid input");
                        continue;
                    }

                    var queryProduct = new Product();
                    queryProduct = prodRepo.GetProduct(changeProductID);
                    if (queryProduct == null)
                    {
                        Console.WriteLine("Invalid ProductID");
                        continue;
                    }

                    Console.WriteLine($"New name (or just <Enter> to keep '{queryProduct.Name}'):");
                    var temp      = Console.ReadLine();
                    var otherName = temp == "" ? queryProduct.Name : temp;

                    Console.WriteLine($"New price (or just <Enter> to keep {queryProduct.Price}):");
                    temp = Console.ReadLine();

                    double otherPrice;
                    if (temp == "")
                    {
                        otherPrice = queryProduct.Price;
                    }
                    else
                    {
                        otherPrice = double.Parse(temp);
                    }

                    prodRepo.UpdateProduct(otherName, otherPrice, changeProductID);

                    Console.WriteLine($"{queryProduct.Name}: ${queryProduct.Price}, ID {changeProductID} changed to");
                    queryProduct = prodRepo.GetProduct(changeProductID);
                    Console.WriteLine($"{queryProduct.Name}: ${queryProduct.Price}, ID {changeProductID}");
                } break;

                case "r":
                {
                    var prodRepo = new DapperProductRepository(conn);
                    foreach (var item in prodRepo.GetAllProducts())
                    {
                        Console.WriteLine($"{item.Name}: ${item.Price}, ID {item.ProductID}");
                    }
                    Console.WriteLine("\nProduct ID to remove:");

                    int changeProductID;
                    if (Int32.TryParse(Console.ReadLine(), out changeProductID) == false)
                    {
                        Console.WriteLine("Invalid input");
                        continue;
                    }

                    var queryProduct = new Product();
                    queryProduct = prodRepo.GetProduct(changeProductID);
                    if (queryProduct == null)
                    {
                        Console.WriteLine("Invalid ProductID");
                        continue;
                    }

                    Console.WriteLine("Removing product:\n" +
                                      $"{queryProduct.Name}: ${queryProduct.Price}, ID {queryProduct.ProductID}\n");
                    prodRepo.RemoveProduct(changeProductID);

                    var salesRepo = new DapperSalesRepository(conn);
                    if (salesRepo.GetSalesItem(changeProductID).Count() != 0)
                    {
                        Console.WriteLine("Removing from Sales");
                        salesRepo.RemoveProduct(changeProductID);
                    }
                    else
                    {
                        Console.WriteLine("Not found in Sales");
                    }

                    var reviewsRepo = new DapperReviewsRepository(conn);
                    if (reviewsRepo.GetReviewItem(changeProductID).Count() != 0)
                    {
                        Console.WriteLine("Removing from Reviews");
                        reviewsRepo.RemoveProduct(changeProductID);
                    }
                    else
                    {
                        Console.WriteLine("Not found in Reviews");
                    }

                    foreach (var item in prodRepo.GetAllProducts())
                    {
                        Console.WriteLine($"{item.Name}: ${item.Price}, ID {item.ProductID}");
                    }
                } break;

                default: quit = true; break;
                }
            }while (quit == false);
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string        connString = config.GetConnectionString("DefaultConnection");
            IDbConnection conn       = new MySqlConnection(connString);
            var           repo       = new DapperDepartmentRepository(conn);
            bool          todo       = true;

            do
            {
                Console.WriteLine("What would you like to edit?");
                Console.WriteLine("Choose the corsponding number,");
                Console.WriteLine("1: Departmennts | 2: Employess | 3: Products | 4: Reviews");
                var ans = Convert.ToInt32(Console.ReadLine());



                if (ans == 1)
                {
                    Console.WriteLine("You have chosen to enter the deparments!, ");
                    Console.WriteLine("What would you like to do?");

                    Console.WriteLine("1: Add a department | 2: Delete a department | 3: List The Departments | 4: Return to start ");
                    int depAns = Convert.ToInt32(Console.ReadLine());

                    if (depAns == 1)
                    {
                        Console.Write("Type a new Department name:");
                        var newDepartmentName = Console.ReadLine();

                        repo.InsertDepartment(newDepartmentName);

                        var departments = repo.GetALLDepartments();

                        foreach (var dept in departments)
                        {
                            Console.WriteLine($"{dept.DepartmentID} || {dept.Name}");
                            Console.WriteLine("--------------");
                        }
                    }


                    else if (depAns == 2)
                    {
                        var departments = repo.GetALLDepartments();

                        foreach (var dept in departments)
                        {
                            Console.WriteLine($"{dept.DepartmentID} || {dept.Name}");
                            Console.WriteLine("--------------");
                        }
                        Console.WriteLine("Write the Department Id for the department you want to delete.");
                        var depID = Convert.ToInt32(Console.ReadLine());

                        repo.DeleteDepartment(depID);

                        Console.WriteLine("Completed!");
                    }
                    else if (depAns == 3)
                    {
                        var departments = repo.GetALLDepartments();

                        foreach (var dept in departments)
                        {
                            Console.WriteLine($"{dept.DepartmentID} || {dept.Name}");
                            Console.WriteLine("--------------");
                        }
                    }
                    else if (depAns == '4')
                    {
                        todo = false;
                    }



                    Console.WriteLine("Do you want to continue? Y/N");
                    var cont = Console.ReadLine();
                    if (cont == "y")
                    {
                        todo = false;
                    }
                    else
                    {
                        todo = true;
                    }
                }
            } while (todo == false);



            /*  if (answer == "yes")
             * {
             *    foreach (var dept in departments )
             *    {
             *        Console.WriteLine(dept.Name);
             *        Console.WriteLine("----------------------");
             *    }
             * }
             * else
             * {
             *    Console.WriteLine("Thankyou!");
             *    System.Environment.Exit(1);
             * }
             *
             * Console.WriteLine("DO you want to create a deparment?");
             * ans = Console.ReadLine();
             * answer = ans.ToLower();
             *
             * if (answer == "yes")
             * {
             *
             *
             *    Console.Write("Type a new Department name:");
             *    var newDepartmentName = Console.ReadLine();
             *
             *    repo.InsertDepartment(newDepartmentName);
             *
             *
             *
             *    foreach (var dept in departments)
             *    {
             *        Console.WriteLine(dept.Name);
             *    }
             * }
             * else
             * {
             *    Console.WriteLine("NO Problem, Have a great day!");
             *    System.Environment.Exit(1);
             * }
             *
             * var prorepo = new DapperProductRepository(conn);
             * Console.Write("Product Name: ");
             * var newName = Console.ReadLine();
             * Console.Write("Price of Product: ");
             * var newPrice = Convert.ToInt32(Console.ReadLine());
             * Console.Write("Product CategoryID: ");
             * var newCategoryID = Convert.ToInt32(Console.ReadLine());
             *
             * prorepo.CreatProduct(newName, newPrice, newCategoryID);
             *
             * var products = prorepo.GetAllProducts();
             *
             * foreach (var prod in products)
             * {
             *    Console.WriteLine(prod.name, prod.price, prod.categoryId);
             * }
             *
             */
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            #region Configuration
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsetting.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");
            #endregion
            IDbConnection conn = new MySqlConnection(connString);
            DapperDepartmentRepository repo = new DapperDepartmentRepository(conn);



            Console.WriteLine("Hello user, here are the current departments.");
            Console.WriteLine("Please press enter");
            Console.ReadLine();
            var depos = repo.GetAllDepartments();
            Print(depos);

            Console.WriteLine("Do you want to add a department?");
            string userResponse = Console.ReadLine();

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine("What is the name of your new department?");
                userResponse = Console.ReadLine();

                repo.InsertDepartment(userResponse);
                Print(repo.GetAllDepartments());
            }
            Console.WriteLine("Have a great day!");

            //IDbConnection conn1 = new MySqlConnection(connString);
            DapperProductsRepository repo1 = new DapperProductsRepository(conn);
            Console.WriteLine("Hello user, here are the current products.");
            Console.WriteLine("Please press enter");
            Console.ReadLine();
            var produc = repo1.GetAllProducts();
            Print(produc);

            Console.WriteLine("Do you want to add a product?");
            string userResponse1 = Console.ReadLine();

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine("What is the name of your new product?");
                var proName = Console.ReadLine();

                Console.WriteLine($"What is the new product's price?");
                var price = Convert.ToDouble(Console.ReadLine());

                Console.WriteLine($"What is the new product's category id?");
                var categoryID = Convert.ToInt32(Console.ReadLine());

                repo1.CreateProduct(proName, price, categoryID);
                Print(repo1.GetAllProducts());
            }
            Console.WriteLine("Have a great day!");
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

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

            IDbConnection conn = new MySqlConnection(connString);
            var           repo = new DapperDepartmentRepository(conn);

            Console.WriteLine("Write a New Department Name");

            var newDepartmentName = Console.ReadLine();

            repo.InsertDepartment(newDepartmentName);

            var departement = repo.GetAllDepartments();

            foreach (var dept in departement)
            {
                Console.WriteLine(dept.Name);
            }

            var productRepo = new DapperProductRepository(conn);

            var products = productRepo.GetAllProducts();

            foreach (var product in products)
            {
                Console.WriteLine(product.Name);
            }

            Console.WriteLine("Let's Make a new product!");
            Console.WriteLine("What's your product's name?");
            string newProduct = Console.ReadLine();

            Console.WriteLine("What's the products category?");
            int newCategoryID = int.Parse(Console.ReadLine());

            Console.WriteLine("And how much is it? ");

            double newPrice = double.Parse(Console.ReadLine());

            productRepo.CreateProduct(newProduct, newPrice, newCategoryID);

            Console.WriteLine("Let's update a price!");

            Console.WriteLine("What is the product id that needs to be updated?");

            int productToBeUpdated = int.Parse(Console.ReadLine());

            Console.WriteLine("What's the new name of this product?");

            string productNameToBeUpdated = Console.ReadLine();

            Console.WriteLine("And What is the new price?");

            double updatedPriceProd = double.Parse(Console.ReadLine());
        }
Exemplo n.º 14
0
        static void Main(string[] args)
        {
            #region Configuration
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");
            //Console.WriteLine(connString);
            #endregion

            IDbConnection conn = new MySqlConnection(connString);
            var           repo = new DapperDepartmentRepository(conn);
            //exercise 2 portion:
            var repo2 = new DapperProductRepository(conn);
            //bonus 1 update product
            var repo3 = new DapperProductRepository(conn);
            //bonus 2 extra delete products
            var repo4 = new DapperProductRepository(conn);


            Console.WriteLine("Type a new Department name");
            var newDepartment = Console.ReadLine();
            repo.InsertDepartment(newDepartment);

            Console.WriteLine("Type a new Product name");
            var newProduct = Console.ReadLine();
            Console.WriteLine("What is the price?");
            var newPrice = Console.ReadLine();
            Console.WriteLine("Give it a category ID");
            var newCategoryID = Console.ReadLine();
            repo2.CreateProduct(newProduct, double.Parse(newPrice), int.Parse(newCategoryID));


            Console.WriteLine("What is the product ID of the item you want to change the name of?");
            var productID = Console.ReadLine();
            Console.WriteLine("What do you want to change the product name to?");
            var updateName = Console.ReadLine();
            repo3.UpdateProduct(int.Parse(productID), updateName);


            Console.WriteLine("What is the product ID you would like to delete?");
            var deleteProduct = Console.ReadLine();
            repo4.DeleteProduct(int.Parse(deleteProduct));


            var departments = repo.GetAllDepartments();

            foreach (var dept in departments)
            {
                Console.WriteLine(dept.Name);
            }


            var products = repo2.GetAllProducts();

            foreach (var product in products)
            {
                Console.WriteLine(product.Name);
                Console.WriteLine(product.Price);
                Console.WriteLine(product.CategoryID);
            }


            var products2 = repo3.GetAllProducts();

            foreach (var product in products2)
            {
                Console.WriteLine(product.Name);
            }


            var products3 = repo4.GetAllProducts();

            foreach (var product in products3)
            {
                Console.WriteLine(product.Name);
            }
        }
Exemplo n.º 15
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         // sets this projects directory to the bas directory
                         .SetBasePath(Directory.GetCurrentDirectory())
                         // adds .json as a reference to this projects path
                         .AddJsonFile("appsettings.json")
                         // builds configuration with the file above
                         .Build();

            // sets connection string to "Default connection" that we stated in the .json file
            string connString = config.GetConnectionString("DefaultConnection");
            // places the Sql connection that is using the "Default Connection" key value pair from .json file
            // into a IDbConnection object
            IDbConnection conn = new MySqlConnection(connString);

            // Use the IDbConnection object in this constructor to set the connection to _connection in DapperDeparmentRepository
            // as well as create an instance of this class to call the methods for querying
            var deptRepo = new DapperDepartmentRepository(conn);
            var prodRepo = new DapperProductsRepository(conn);



            Console.WriteLine("What kind of data would you like to access?");
            Console.WriteLine(
                "(D)epartments\n" +
                "(P)roducts\n");
            Console.Write("> ");

            var userInput = Console.ReadLine().ToLower().Trim();


            if (userInput.StartsWith('d'))
            {
                Console.WriteLine("\nDo you want to add a department? yes/no");
                Console.Write("> ");
                var addDepartmentInput = Console.ReadLine().ToLower().Trim();

                if (addDepartmentInput.StartsWith('y'))
                {
                    // Takes input from the user for new department name
                    Console.WriteLine("\nEnter a new department name to add to the database..\n");
                    var userInputNewDepartment = Console.ReadLine();
                    deptRepo.InsertDepartment(userInputNewDepartment);

                    Console.WriteLine("List of Departments:");
                    var departmentList = deptRepo.GetDepartments();
                    // create foreach loop to enumerate over the list and write all department names to the console
                    foreach (var dept in departmentList)
                    {
                        Console.WriteLine(dept.Name);
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("List of Departments:");
                    // reads departments and return a list of the departments
                    var departmentList = deptRepo.GetDepartments();

                    // create foreach loop to enumerate over the list and write all department names to the console
                    foreach (var dept in departmentList)
                    {
                        Console.WriteLine(dept.Name);
                    }
                }
            }
            else if (userInput.StartsWith('p'))
            {
                Console.WriteLine("Do you want to add a product? yes/no");
                Console.Write("> ");
                var addProductInput = Console.ReadLine().ToLower().Trim();

                if (addProductInput.StartsWith('y'))
                {
                    var newProduct = new Products();
                    Console.Write("\nProduct Name: ");
                    newProduct.Name = Console.ReadLine();

                    Console.Write("\nProduct Price: ");
                    newProduct.Price = double.Parse(Console.ReadLine());

                    Console.Write("\nProduct Category ID: ");
                    newProduct.CategoryID = int.Parse(Console.ReadLine());

                    prodRepo.CreateProduct(newProduct.Name, newProduct.Price, newProduct.CategoryID);
                    prodRepo.ListAllProducts();
                }
                else
                {
                    prodRepo.ListAllProducts();
                }
            }
            else
            {
                Console.WriteLine("Incorrect Input");
            }



            Console.ReadLine();
        }