Example #1
0
        public void UpdateMenu()
        {
            var config = new configurations.Config();

            Configuration.GetSection("PasswordConfig").Bind(config);

            string connectionString = $"Server=IP;Database=DB;User Id=USERID;Password={config}";

            Console.WriteLine("Getting Menu");
            proxy.MenuProxy proxy = new proxy.MenuProxy();

            var menu = proxy.GetMenu(menuApiUrl).GetAwaiter().GetResult();

            using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
                using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("TRUNCATE TABLE MENU", connection)){
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    repo = new repositories.MenuRepository(connection);

                    insertMenu(menu);
                }
            void insertMenu(IEnumerable <model.MenuItem> menu, int?parentMenuId = null)
            {
                int id = 0;

                foreach (var mi in menu)
                {
                    mi.ParentMenuId = parentMenuId;
                    id = repo.Insert(mi).GetAwaiter().GetResult();

                    Console.WriteLine($"Inserted menuitem id = {id}");

                    if (mi.Children != null)
                    {
                        insertMenu(mi.Children, id);
                    }
                }
            }
        }