Esempio n. 1
0
        private static void TestUserDB()
        {
            Console.WriteLine("It works!");

            var db = new UserDBHelper();
            {
                bool run = true;
                while (run)
                {
                    Console.WriteLine(
                        "1. Add user\n" +
                        "2. Remove user by id\n" +
                        "3. Add to friends\n" +
                        "4. Remove from friends\n" +
                        "5. Print users\n" +
                        "6. Print relations\n" +
                        "7. Print user info");
                    var key = Console.ReadKey().Key;
                    Console.WriteLine();
                    switch (key)
                    {
                    case ConsoleKey.D1:
                    {
                        Console.WriteLine();
                        RandomUserRegistration(db.RegisterUserToTable,
                                               db.RemoveUserByIdFromTable,
                                               db.FindUserByNickname);
                        break;
                    }

                    case ConsoleKey.D2:
                    {
                        Console.WriteLine();
                        System.Int32 id;
                        Console.Write("Write id to delete: ");
                        var idStr = Console.ReadLine();
                        if (Int32.TryParse(idStr, out id))
                        {
                            db.RemoveUserByIdFromTable(id);
                        }
                        break;
                    }

                    case ConsoleKey.D3:
                    {
                        Int32 thisIdInt;
                        Int32 idToAddInt;

                        Console.Write("Write userid [who want to add]: ");
                        string thisIdString = Console.ReadLine();
                        if (thisIdString.Length > 0)
                        {
                            if (Int32.TryParse(thisIdString, out thisIdInt))
                            {
                                Console.Write("Write userid [whom want to add]: ");
                                string idToAddString = Console.ReadLine();
                                if (idToAddString.Length > 0)
                                {
                                    if (Int32.TryParse(idToAddString, out idToAddInt))
                                    {
                                        if (thisIdInt != 0 && idToAddInt != 0)
                                        {
                                            db.AddUserToFriends(thisIdInt, idToAddInt);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine($"Can't pasrse idToAddString: {idToAddString}");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"Can't pasrse thisIdString: {thisIdString}");
                            }
                        }
                        break;
                    }

                    case ConsoleKey.D4:
                    {
                        Int32 thisIdInt;
                        Int32 idToRemoveInt;

                        Console.Write("Write userid [who want to remove]: ");
                        string thisIdString = Console.ReadLine();
                        if (thisIdString.Length > 0)
                        {
                            if (Int32.TryParse(thisIdString, out thisIdInt))
                            {
                                Console.Write("Write userid [whom want to remove]: ");
                                string idToRemoveString = Console.ReadLine();
                                if (idToRemoveString.Length > 0)
                                {
                                    if (Int32.TryParse(idToRemoveString, out idToRemoveInt))
                                    {
                                        if (thisIdInt != 0 && idToRemoveInt != 0)
                                        {
                                            db.RemoveUserFromFriends(thisIdInt, idToRemoveInt);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine($"Can't pasrse idToAddString: {idToRemoveString}");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"Can't pasrse thisIdString: {thisIdString}");
                            }
                        }
                        break;
                    }

                    case ConsoleKey.D5:
                    {
                        Console.Clear();
                        Console.WriteLine("Users:");
                        var usersList = db.GetAllUsers();
                        foreach (var el in usersList)
                        {
                            Console.WriteLine($"{el.UserId} {el.FullName} {el.Nickname} {el.Email}");
                        }
                        break;
                    }

                    case ConsoleKey.D6:
                    {
                        Console.Clear();
                        Console.WriteLine("\nRelations:");
                        var friends = db.GetUsersFriendsTable(1);
                        Console.WriteLine("Frineds: ");
                        foreach (var friend in friends)
                        {
                            Console.WriteLine($"{friend.UserId} - {friend.FriendId}");
                        }

                        var followers = db.GetUsersFollowersTable(1);
                        Console.WriteLine("Followers: ");
                        foreach (var follower in followers)
                        {
                            Console.WriteLine($"{follower.UserId} - {follower.FollowerId}");
                        }

                        var followings = db.GetUsersFollowingsTable(1);
                        Console.WriteLine("Followings: ");
                        foreach (var following in followings)
                        {
                            Console.WriteLine($"{following.UserId} - {following.FollowingId}");
                        }
                        Console.WriteLine();
                        break;
                    }

                    case ConsoleKey.D7:
                    {
                        var usersInfo = db.GetAllUsersInfo();
                        //foreach (var user in usersInfo)
                        //{
                        //    Console.WriteLine($"{user.Nickname} {user.UserId} {user.CoreId}");
                        //}
                        break;
                    }

                    case ConsoleKey.C: { Console.Clear(); break; }

                    case ConsoleKey.Escape: { run = false; break; }

                    default: { break; }
                    }
                }
            }
        }