Example #1
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!");
        }
Example #2
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();
        }