예제 #1
0
        //We can use these methods that add user interaction with our Dapper Methods
        public static void DeleteProduct()
        {
            //ProductRepository instance - so we can call our dapper methods
            var prodRepo = new DapperProductRepository(conn);

            //User interaction
            Console.WriteLine($"Please enter the productID of the product you would like to delete:");
            var productID = Convert.ToInt32(Console.ReadLine());

            //Call the Dapper method
            prodRepo.DeleteProduct(productID);
        }
예제 #2
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);

            ////Create a new Department

            //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 department in departments)
            //{
            //    Console.WriteLine($"{department.DepartmentID} {department.Name}");
            //}

            //Create a new product
            Console.WriteLine("Would you like to create a new product? (type 'nothing' if you have nothing to add)");
            var repo2 = new DapperProductRepository(conn);

            var products = repo2.GetAllProducts();

            var answer = Console.ReadLine();

            if (answer == "nothing")
            {
                Console.WriteLine("thanks!");
            }
            else
            {
                foreach (var item in products)
                {
                    Console.WriteLine($"{item.CategoryID} {item.Name} {item.Price} {item.ProductID} {item.OnSale} {item.StockLevel}");
                }

                Console.WriteLine("Type the new product name:");

                var newProductName = Console.ReadLine();

                Console.WriteLine("Type the new product price:");

                var newProductPrice = Convert.ToDouble(Console.ReadLine());

                Console.WriteLine("Type the new product category ID:");

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

                repo2.CreateProduct(newProductName, newProductPrice, newProductCID);

                products = repo2.GetAllProducts();

                foreach (var item in products)
                {
                    Console.WriteLine($"{item.CategoryID} {item.Name} {item.Price} {item.ProductID} {item.OnSale} {item.StockLevel}");
                }
            }
            //Update an existing product:

            Console.WriteLine("Would you like to update an existing product? (type 'nothing' if you have nothing to update)");

            answer = Console.ReadLine();

            if (answer == "nothing")
            {
                Console.WriteLine("thanks!");
            }
            else
            {
                products = repo2.GetAllProducts();

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

                Console.WriteLine("Type the name of the product you would like to update:");

                var existingProductName = Console.ReadLine();

                Console.WriteLine("Type the new product name:");

                var newProductName = Console.ReadLine();

                Console.WriteLine("Type the new product price:");

                var newProductPrice = Convert.ToDouble(Console.ReadLine());

                Console.WriteLine("Type the new product category ID:");

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

                repo2.UpdateProduct(existingProductName, newProductName, newProductPrice, newProductCID);

                products = repo2.GetAllProducts();

                foreach (var item in products)
                {
                    Console.WriteLine($"{item.CategoryID} {item.Name} {item.Price} {item.ProductID} {item.OnSale} {item.StockLevel}");
                }
            }
            //Delete an existing product:

            Console.WriteLine("What would you like to delete? (type 'nothing' if you have nothing to delete)");

            answer = Console.ReadLine();

            if (answer == "nothing")
            {
                Console.WriteLine("thanks!");
            }
            else
            {
                repo2.DeleteProduct(answer);

                //showing the reulting database

                products = repo2.GetAllProducts();

                foreach (var item in products)
                {
                    Console.WriteLine($"{item.CategoryID} {item.Name} {item.Price} {item.ProductID} {item.OnSale} {item.StockLevel}");
                }
            }
        }
예제 #3
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);
            }
        }