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);
        }
Пример #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);
            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);
             * }
             *
             */
        }