Exemplo n.º 1
0
        private static void NewChat()
        {
            int        creatorId = -1, member = -1;
            MenuChoice NewChatMenu = MenuManager.Menu(new string[] { "Select chat creator", "Select member", "Create chat", "Exit" }, $"New chat menu\r\nCreator: {creatorId}\r\nMember: {member}");

            while (NewChatMenu.choice != "Exit")
            {
                switch (NewChatMenu.choice_id)
                {
                case 0:
                    creatorId = SelectUser("Please select chat creator");
                    break;

                case 1:
                    member = SelectUser("Please select chat member");
                    break;

                case 2:
                    CreateNewChat(creatorId, member);
                    break;
                }

                MenuManager.Menu(NewChatMenu, $"New chat menu\r\nCreator: {creatorId}\r\nMember: {member}");
            }
        }
Exemplo n.º 2
0
        private static void Menu()
        {
            MenuChoice MainMenu = MenuManager.Menu(new string[] { "Create new chat", "Print \"accounts\" table", "Transaction", "Exit" }, "Messenger example usage Linq to Sql");

            while (MainMenu.choice != "Exit")
            {
                switch (MainMenu.choice)
                {
                case "Create new chat":
                    NewChat();
                    break;

                case "Print \"accounts\" table":
                    DispAccountsTable();
                    break;

                case "Transaction":
                    Transaction();
                    break;

                default:
                    break;
                }

                Console.ReadKey(true);
                MenuManager.Menu(MainMenu);
            }
        }
Exemplo n.º 3
0
        private void EditPassword(BaseContext cntx, Users query, MenuChoice usersMenuChoice)
        {
            Console.Clear();
            Console.WriteLine($"Current password: {cntx.Users.Where(u => u.Username == usersMenuChoice.choice).FirstOrDefault().Password}");
            string newPassword = Core.GetInput("New password");

            Console.WriteLine("In process . . .");
            query.Password = newPassword;
        }
Exemplo n.º 4
0
        private void EditUser(BaseContext cntx)
        {
            Dictionary <int, string> users = new Dictionary <int, string>();

            foreach (Users cntxUser in cntx.Users)
            {
                users.Add(cntxUser.Id, cntxUser.Username);
            }

            string[] usernames = new string[users.Values.Count];
            users.Values.CopyTo(usernames, 0);
            MenuChoice usersMenuChoice = MenuManager.Menu(usernames, "Choose what user you want to edit");

            MenuChoice EditUserMenu = MenuManager.Menu(new string[] { "Edit username", "Edit password", "Go back" });

            while (EditUserMenu.choice != "Go back")
            {
                var query = (from e in cntx.Users
                             where e.Username == usersMenuChoice.choice
                             orderby e.Id ascending
                             select e).FirstOrDefault();

                switch (EditUserMenu.choice)
                {
                case "Edit username":
                    EditUsername(cntx, query, usersMenuChoice);
                    break;

                case "Edit password":
                    EditPassword(cntx, query, usersMenuChoice);
                    break;
                }


                try
                {
                    cntx.SaveChanges();
                    Console.WriteLine(
                        $"{(EditUserMenu.choice == "Edit username" ? "Username" : EditUserMenu.choice == "Edit password" ? "Password" : "undefined")} for user '{usersMenuChoice.choice}' was successfully changed!"
                        .DrawInConsoleBox());
                    Console.WriteLine("Press any key to continue . . .");
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    Console.WriteLine("Conflict detected!".DrawInConsoleBox());
                    foreach (DbEntityEntry exEntry in ex.Entries)
                    {
                        var dbValues = exEntry.GetDatabaseValues();
                        exEntry.OriginalValues.SetValues(dbValues);
                    }
                }

                Console.ReadKey(true);
                MenuManager.Menu(EditUserMenu);
            }
        }
Exemplo n.º 5
0
        private void EditUsername(BaseContext cntx, Users query, MenuChoice usersMenuChoice)
        {
            Console.Clear();
            Console.WriteLine($"Current username: {usersMenuChoice.choice}");
            string newUsername = Core.GetInput("New username");

            Console.WriteLine("In process . . .");
            query.Username         = newUsername;
            usersMenuChoice.choice = newUsername;
        }
Exemplo n.º 6
0
        public static MenuChoice Menu(string[] choices, string title = "Make your choice")
        {
            var mc = new MenuChoice();

            mc.choices = choices;
            Console.Clear();
            for (int i = 0; i < choices.Length + 1; i++)
            {
                Console.Write("\r\n");
            }
            Console.WriteLine(title.DrawInConsoleBox());
            //var menu = new Menu(new string[] { "John", "Bill", "Janusz", "Grażyna", "1500", ":)" });
            var menu        = new MenuClass(choices);
            var menuPainter = new ConsoleMenuPainter(menu);

            var done = false;

            do
            {
                menuPainter.Paint(0, 0);

                var keyInfo = Console.ReadKey();

                switch (keyInfo.Key)
                {
                case ConsoleKey.UpArrow:
                    menu.MoveUp();
                    break;

                case ConsoleKey.DownArrow:
                    menu.MoveDown();
                    break;

                case ConsoleKey.Enter:
                    done = true;
                    break;
                }
            } while (!done);

            mc.choice_id = menu.SelectedIndex;
            mc.choice    = menu.SelectedOption;
            mc.used      = true;
            mc.title     = title;

            Console.ForegroundColor = ConsoleColor.Cyan;
            //Console.WriteLine("Selected option: " + (menu.SelectedOption ?? "(nothing)"));
            //Console.ReadKey();

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();
            //return menu.SelectedOption;
            return(mc);
        }
Exemplo n.º 7
0
        private void DeleteUser(BaseContext cntx)
        {
            Dictionary <int, string> users = new Dictionary <int, string>();

            foreach (Users cntxUser in cntx.Users)
            {
                users.Add(cntxUser.Id, cntxUser.Username);
            }

            string[] usernames = new string[users.Values.Count];
            users.Values.CopyTo(usernames, 0);
            MenuChoice usersMenuChoice = MenuManager.Menu(usernames, "Choose which user you want delete");

            Console.WriteLine("In process . . .");
            Users cntxUsers = cntx.Users
                              .Where(u => u.Username == usersMenuChoice.choice)
                              .FirstOrDefault();

            cntx.Users.Remove(cntxUsers);
            cntx.SaveChanges();
            Console.WriteLine($"User '{usersMenuChoice.choice}' was successfully deleted!".DrawInConsoleBox());
            Console.WriteLine("Press any key to continue . . .");
        }
Exemplo n.º 8
0
        //Отложенная загрузка (lazy loading) заключается в том, что Entity Framework автоматически загружает данные,
        //при этом не загружая связанные данные. Когда потребуются связанные данные Entity Framework создаст еще один запрос к базе данных.
        public static void LazyLoading(BaseContext cntx)
        {
            Dictionary <int, string> users = new Dictionary <int, string>();

            foreach (Users cntxUser in cntx.Users)
            {
                users.Add(cntxUser.Id, cntxUser.Username);
            }

            string[] usernames = new string[users.Values.Count];
            users.Values.CopyTo(usernames, 0);
            MenuChoice usersMenuChoice = MenuManager.Menu(usernames, "Choose user to load");

            var user = (from e in cntx.Users
                        where e.Username == usersMenuChoice.choice
                        orderby e.Id ascending
                        select e).FirstOrDefault();

            // Попытаться загрузить связанные с ним ключи
            if (user != null && user.Keys != null)
            {
                for (int i = 0; i < user.Keys.Count; i++)
                {
                    DataModel.Keys key = user.Keys.ElementAt(i);
                    if (key != null)
                    {
                        Console.WriteLine($"Key: {key.Key}");
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine($"All existing keys owned by '{user.Username}'".DrawInConsoleBox());
            Console.ReadKey(true);
            //                foreach (var key in user.Keys)
            //                    Console.WriteLine("Key: " + key.Key);
        }
Exemplo n.º 9
0
        private void AddKeyForUser(BaseContext cntx)
        {
            Dictionary <int, string> users = new Dictionary <int, string>();

            foreach (Users cntxUser in cntx.Users)
            {
                users.Add(cntxUser.Id, cntxUser.Username);
            }

            string[] usernames = new string[users.Values.Count];
            users.Values.CopyTo(usernames, 0);
            MenuChoice usersMenuChoice = MenuManager.Menu(usernames, "Choose key owner for creating a new key");
            var        user            = (from e in cntx.Users
                                          where e.Username == usersMenuChoice.choice
                                          orderby e.Id ascending
                                          select e).FirstOrDefault();

            MenuChoice KeygenMenu = MenuManager.Menu(new string[] { "Generate", "Manual input", "Exit" },
                                                     $"Key generating menu for user: {usersMenuChoice.choice}");

            while (KeygenMenu.choice != "Exit")
            {
                switch (KeygenMenu.choice_id)
                {
                case 0:
                    AddNewKey(user, GenerateKey(), cntx);
                    break;

                case 1:
                    AddNewKey(user, Core.GetInput("Enter key value"), cntx);
                    break;
                }

                Console.ReadKey(true);
                MenuManager.Menu(KeygenMenu);
            }
        }
Exemplo n.º 10
0
        public static void ExplicitLoading(BaseContext cntx)
        {
            // Загрузить одного пользователя
            Dictionary <int, string> users = new Dictionary <int, string>();

            foreach (Users cntxUser in cntx.Users)
            {
                users.Add(cntxUser.Id, cntxUser.Username);
            }

            string[] usernames = new string[users.Values.Count];
            users.Values.CopyTo(usernames, 0);
            MenuChoice usersMenuChoice = MenuManager.Menu(usernames, "Choose user to load");

            var user = (from e in cntx.Users
                        where e.Username == usersMenuChoice.choice
                        orderby e.Id ascending
                        select e).FirstOrDefault();

            // Загрузить связанные с ним ключи с помощью явной загрузки
            cntx.Entry(user)
            .Collection(c => c.Keys)
            .Load();

            Console.WriteLine($"Keys for user {user.Username}".DrawInConsoleBox() + "\r\n");
            if (user != null && user.Keys.Count >= 1)
            {
                foreach (var key in user.Keys)
                {
                    if (key.OwnerId == user.Id)
                    {
                        Console.WriteLine($"Key: {key.Key}");
                    }
                }
            }
        }
Exemplo n.º 11
0
        public void Init()
        {
            using (BaseContext cntx = new BaseContext())
            {
                //MenuChoice CodeFirstMenu = new MenuChoice();
                //MenuChoice MainMenu = MenuManager.Menu(new string[] { "Create new chat", "Print \"accounts\" table", "Transaction", "Exit" }, "Messenger example usage Linq to Sql");
                MenuChoice CodeFirstMenu = MenuManager.Menu(new string[]
                {
                    "Register user", "Get users", "Edit user", "Delete user", "Add key", "Связанная загрузка", "Sim",
                    "Sim2", "Sim3"
                });
                while (CodeFirstMenu.choice != "Exit")
                {
                    switch (CodeFirstMenu.choice)
                    {
                    case "Register user":
                        RegisterUser(cntx);
                        break;

                    case "Get users":
                        GetUsers(cntx);
                        break;

                    case "Edit user":
                        EditUser(cntx);
                        break;

                    case "Delete user":
                        DeleteUser(cntx);
                        break;

                    case "Add key":
                        AddKeyForUser(cntx);
                        break;

                    case "Sim":
                        SimpleMethod(cntx);
                        break;

                    case "Sim2":
                        SimpleMethodTwo(cntx);
                        break;

                    case "Sim3":
                        SimpleMethodTree(cntx);
                        break;

                    case "Связанная загрузка":
                    {
                        MenuChoice LoadMenu =
                            MenuManager.Menu(new string[]
                            {
                                "Отложенная загрузка (LazyLoad)", "Прямая загрузка (EagerLoading)",
                                "Явная загрузка (ExplicitLoading)", "Add new key method", "Выйти"
                            });
                        while (LoadMenu.choice != "Выйти")
                        {
                            switch (LoadMenu.choice_id)
                            {
                            case 0:
                                LazyLoading(cntx);
                                break;

                            case 1:
                                EagerLoading(cntx);
                                Console.WriteLine(
                                    "Press 'y' to Count keys for each user, any other key to cancel"
                                    .DrawInConsoleBox());
                                var key = Console.ReadKey(true);
                                if (key.KeyChar == char.Parse("y"))
                                {
                                    EagerLoading_KeysCount(cntx);
                                }
                                break;

                            case 2:
                                ExplicitLoading(cntx);
                                break;

                            case 3:
                                //AddNewKey_method(cntx);
                                testUpdate(cntx);
                                break;
                            }
                            Console.ReadKey(true);
                            MenuManager.Menu(LoadMenu);
                        }
                    }
                    break;
                    }

                    Console.ReadKey(true);
                    MenuManager.Menu(CodeFirstMenu);
                }
            }
        }