コード例 #1
0
        public static string GetUserRole(string username)
        {
            SqlConnection connection = ConnectionToServer.Connection();
            string        userRole   = "";

            try
            {
                connection.Open();

                SqlCommand    command = new SqlCommand($"Select Role FROM Person WHERE username = '******'", connection);
                SqlDataReader reader  = command.ExecuteReader();
                while (reader.Read())
                {
                    userRole = reader["Role"].ToString();
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return(userRole);
        }
コード例 #2
0
        public static void ReadUser(string username)
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                connection.Open();

                SqlCommand    command = new SqlCommand($"SELECT Id, Nickname, Username, Password, Role FROM Person WHERE username = '******'", connection);
                SqlDataReader reader  = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(reader["Id"].ToString());
                    Console.WriteLine(reader["Nickname"].ToString());
                    Console.WriteLine(reader["Username"].ToString());
                    Console.WriteLine(reader["Password"].ToString());
                    Console.WriteLine(reader["Role"].ToString());
                }

                Console.WriteLine("Press enter to exit");
                Console.ReadLine();
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #3
0
        public static int GetUserId(string username)
        {
            SqlConnection connection = ConnectionToServer.Connection();
            int           id         = 0;

            try
            {
                connection.Open();
                SqlCommand    cmdLogin = new SqlCommand($"SELECT Id FROM Person WHERE username = '******'", connection);
                SqlDataReader reader   = cmdLogin.ExecuteReader();
                while (reader.Read())
                {
                    id = reader.GetInt32(0);
                }
                reader.Close();
                //Console.WriteLine(id);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return(id);
        }
コード例 #4
0
        public static void NewMessage(string sender, string receiver)
        {
            SqlConnection connection = ConnectionToServer.Connection();
            int           messageId;
            DateTime      date     = DateTime.Now;
            string        messDate = date.ToString("dd/MM/yyyy  HH:mm:ss");

            try
            {
                connection.Open();
                Console.Write("Message: ");
                string     newmessage = Console.ReadLine();
                int        senderId   = ConnectionToServer.GetUserId(sender);
                int        receiverId = ConnectionToServer.GetUserId(receiver);
                SqlCommand cmd        = new SqlCommand($"INSERT INTO Message (Date, Sender, Receiver, Text) VALUES('{messDate}','{senderId}','{receiverId}','{newmessage}');SELECT SCOPE_IDENTITY();", connection);
                messageId = System.Convert.ToInt32(cmd.ExecuteScalar());
                string userscontact = $"{sender} to {receiver}";
                TextFile.MessagesToText(messDate, sender, receiver, newmessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #5
0
        public static void DeleteMessage(int messageId)
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                using (connection)
                {
                    connection.Open();
                    SqlCommand command = new SqlCommand($"DELETE FROM Message WHERE Id = {messageId}", connection);
                    int        deleted = command.ExecuteNonQuery();
                    if (deleted > 0)
                    {
                        Console.WriteLine($"{deleted} rows deleted successfully");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #6
0
        public static void EditMessage(int messageId)
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                using (connection)
                {
                    Console.Write("Message: ");
                    string upMessage = Console.ReadLine();
                    connection.Open();
                    SqlCommand command = new SqlCommand($"UPDATE Message SET Text = '{upMessage}' WHERE Id = '{messageId}'", connection);
                    int        update  = command.ExecuteNonQuery();
                    if (update > 0)
                    {
                        Console.WriteLine("Update successfull");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #7
0
        public static void ChangeRole()
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                Console.Write("Enter Username: "******"Select user's role: Simple, Editor, Deleter, Admin");
                string newrole = Console.ReadLine().ToUpper();

                connection.Open();

                SqlCommand command    = new SqlCommand($"UPDATE Person SET Role = '{newrole}' WHERE userName = '******'", connection);
                int        updateRole = command.ExecuteNonQuery();
                if (updateRole > 0)
                {
                    Console.WriteLine("User's role updated successfully!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #8
0
        public static void ReadReceivedMessages(string username)
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                int userId = 0;
                using (connection)
                {
                    userId = ConnectionToServer.GetUserId(username);
                    connection.Open();
                    Console.WriteLine("Messages that received: \n");
                    SqlCommand    command = new SqlCommand($"SELECT Id, Date, Sender, Receiver, Text FROM Message WHERE Receiver = {userId}", connection);
                    SqlDataReader reader  = command.ExecuteReader();

                    while (reader.Read())
                    {
                        int    messId = reader.GetInt32(0);
                        string sender = ConnectionToServer.GetUserName(Int32.Parse(reader["Sender"].ToString()));
                        Console.WriteLine(String.Format("Message Id:" + $"{messId}\n" + "From:" + username + $"\nTo:{sender}\n" + "Message: " + $"{reader[4]}\n"));
                    }

                    Console.WriteLine("Press enter key to exit");
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #9
0
        public static bool UserExist(string username)
        {
            bool          exists     = false;
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand($"SELECT COUNT (*) Username FROM Person WHERE username = '******'", connection);
                cmd.Parameters.AddWithValue("@username", username);
                int match = (int)cmd.ExecuteScalar();

                if (match > 0)
                {
                    Console.WriteLine("Username exists!");
                    exists = true;
                }

                else
                {
                    Console.WriteLine("Username does not exist!");
                    exists = false;
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return(exists);
        }
コード例 #10
0
        public static string GetUserName(int Id)
        {
            SqlConnection connection = ConnectionToServer.Connection();
            string        UserName   = "";

            try
            {
                connection.Open();

                SqlCommand    command = new SqlCommand($"SELECT Username FROM Person WHERE Id = '{Id}'", connection);
                SqlDataReader reader  = command.ExecuteReader();
                while (reader.Read())
                {
                    UserName = reader["Username"].ToString();
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return(UserName);
        }
コード例 #11
0
        public static void UpdateUserPassWord(string username)
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                Console.Write("Enter new username: "******"UPDATE Person SET password = '******' WHERE username = '******'", connection);
                int        updatepass = command.ExecuteNonQuery();
                if (updatepass > 0)
                {
                    Console.WriteLine("Password updated successfully!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #12
0
        public static void UpdateUserName(string username)
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                Console.WriteLine($"Current username is {username}");
                Console.Write("Enter new username: "******"UPDATE Person SET username = '******' WHERE userName = '******'", connection);
                int        updatename = command.ExecuteNonQuery();
                if (updatename > 0)
                {
                    Console.WriteLine("Username updated successfully!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #13
0
        public static void CreateUser()
        {
            SqlConnection connection = ConnectionToServer.Connection();

            string username, role, nickname, password;

            try
            {
                Console.WriteLine("New user entry:");
                Console.Write("Enter Nickname: ");
                nickname = Console.ReadLine();

                do
                {
                    Console.Write("Enter username: "******"Enter password: "******"Select user's role: Simple, Editor, Deleter, Admin");
                    role = Console.ReadLine().ToUpper();

                    if (role == "SIMPLE" || role == "EDITOR" || role == "DELETER" || role == "ADMIN")
                    {
                        Console.WriteLine("Role selected: " + role);
                    }
                    else
                    {
                        Console.WriteLine("Try again");
                    }
                } while (!(role == "SIMPLE" || role == "EDITOR" || role == "DELETER" || role == "ADMIN"));


                connection.Open();
                SqlCommand userInput = new SqlCommand($"INSERT INTO Person(Nickname, Username, Password, Role) VALUES('{nickname}','{username}','{password}','{role}');SELECT SCOPE_IDENTITY();", connection);
                int        id        = System.Convert.ToInt32(userInput.ExecuteScalar());

                User user = new User(id, nickname, username, password, role);

                connection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #14
0
        static void Main(string[] args)
        {
            SqlConnection connection = ConnectionToServer.Connection();

            string loggedUser = Login.LoginCheck();

            if (loggedUser != "")
            {
                Menu.DisplayMenu(loggedUser);
            }
        }
コード例 #15
0
        public static void ReadAllUsers()
        {
            SqlConnection connection = ConnectionToServer.Connection();
            List <User>   users      = new List <User>();

            try
            {
                connection.Open();

                SqlCommand    command = new SqlCommand($"SELECT Id, Nickname, Username, Password, Role FROM Person", connection);
                SqlDataReader reader  = command.ExecuteReader();
                Console.WriteLine("Users: ");

                while (reader.Read())
                {
                    User user = new User()
                    {
                        Id       = reader.GetInt32(0),
                        NickName = reader["Nickname"].ToString(),
                        UserName = reader["Username"].ToString(),
                        Password = reader["Password"].ToString(),
                        Role     = reader["Role"].ToString()
                    };
                    users.Add(user);
                }
                foreach (User u in users)
                {
                    Console.WriteLine($"Id: {u.Id}");
                    Console.WriteLine($"Username: {u.UserName}");
                    Console.WriteLine($"Nickname: {u.NickName}");
                    Console.WriteLine($"Password: {u.Password}");
                    Console.WriteLine($"Role: {u.Role}");
                }
                reader.Close();

                Console.WriteLine("Press enter to exit");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #16
0
        public static void DeleteUser(string username)
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand($"DELETE FROM Person WHERE Username = '******'", connection);
                int        del     = command.ExecuteNonQuery();
                if (del > 0)
                {
                    Console.WriteLine("User successfully deleted!");
                }

                connection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #17
0
        public static string LoginCheck()
        {
            SqlConnection connection = ConnectionToServer.Connection();

            try
            {
                connection.Open();
                Console.WriteLine("Login Screen \n------------\n");

                Console.Write("Enter Username: "******"Enter Password: "******"SELECT COUNT (*) FROM Person WHERE Username = @uusername AND Password = @password", connection);
                command.Parameters.AddWithValue("@uusername", entername);
                command.Parameters.AddWithValue("@password", enterpass);
                int result = (int)command.ExecuteScalar();

                connection.Close();
                if (result > 0)
                {
                    Console.WriteLine("Login Successful!");
                    return(entername);
                }
                else
                {
                    Console.WriteLine("Incorrect login. Please try again...");
                    return("");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return("");
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #18
0
        static public void DisplayMenu(string loggedUser)
        {
            System.Threading.Thread.Sleep(1500);
            Console.Clear();
            Console.WriteLine("Hello " + loggedUser);
            Console.WriteLine();

            string userRole = ConnectionToServer.GetUserRole(loggedUser);

            switch (userRole)
            {
            case "SIMPLE":
                Console.WriteLine("Simple user's Menu");
                Console.WriteLine();
                Console.WriteLine("1. Read your sented Message");
                Console.WriteLine("2. Read your recieved Messages");
                Console.WriteLine("3. Send new Message");
                Console.WriteLine("4. Exit");
                int resultSimp;

                if (!int.TryParse(Console.ReadLine(), out resultSimp) || resultSimp > 4 || resultSimp < 1)
                {
                    Console.WriteLine("Invalid Entry, wait a sec and try Again.");
                    System.Threading.Thread.Sleep(1000);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultSimp == 1)
                {
                    Console.Clear();
                    Message.ReadUserMessages(loggedUser);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultSimp == 2)
                {
                    Console.Clear();
                    Message.ReadReceivedMessages(loggedUser);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultSimp == 3)
                {
                    Console.Clear();
                    Console.Write("Enter username: "******"Goodbye!");
                    System.Threading.Thread.Sleep(1000);
                    Environment.Exit(0);
                }

                break;

            case "EDITOR":
                Console.WriteLine("Editor's Menu");
                Console.WriteLine();
                Console.WriteLine("1. Read sended messages");
                Console.WriteLine("2. Read received messases ");
                Console.WriteLine("3. Send a new Message");
                Console.WriteLine("4. Edit Message");
                Console.WriteLine("5. Exit");
                int resultEdit;

                if (!int.TryParse(Console.ReadLine(), out resultEdit) || resultEdit > 5 || resultEdit < 1)
                {
                    Console.WriteLine("Invalid Entry, wait a sec and try Again.");
                    System.Threading.Thread.Sleep(1000);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }


                if (resultEdit == 1)
                {
                    Console.Clear();
                    Message.ReadUserMessages(loggedUser);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultEdit == 2)
                {
                    Console.Clear();
                    Message.ReadReceivedMessages(loggedUser);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultEdit == 3)
                {
                    Console.Clear();
                    Console.Write("Enter The username you want to send to: ");
                    string receiver = Console.ReadLine();
                    Message.NewMessage(loggedUser, receiver);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultEdit == 4)
                {
                    Console.Write("Enter Messages Id: ");
                    int MessId;
                    if (!int.TryParse(Console.ReadLine(), out MessId))
                    {
                        Console.WriteLine("Invalid Entry, Please try again..");
                    }
                    Message.EditMessage(MessId);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultEdit == 5)
                {
                    Console.Clear();
                    Console.WriteLine("Goodbye!");
                    System.Threading.Thread.Sleep(1000);
                    Environment.Exit(0);
                }


                break;

            case "DELETER":
                Console.WriteLine("Deleter's Menu");
                Console.WriteLine();
                Console.WriteLine("1. Read sended messages");
                Console.WriteLine("2. Read received messases ");
                Console.WriteLine("3. Send a new Message");
                Console.WriteLine("4. Edit Message");
                Console.WriteLine("5. Delete Message");
                Console.WriteLine("6. Exit");
                int resultDel;

                if (!int.TryParse(Console.ReadLine(), out resultDel) || resultDel > 6 || resultDel < 1)
                {
                    Console.WriteLine("Invalid Entry, wait a sec and try Again.");
                    System.Threading.Thread.Sleep(1000);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultDel == 1)
                {
                    Console.Clear();
                    Message.ReadUserMessages(loggedUser);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultDel == 2)
                {
                    Console.Clear();
                    Message.ReadReceivedMessages(loggedUser);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultDel == 3)
                {
                    Console.Clear();
                    Console.Write("Enter The username you want to send to: ");
                    string receiver = Console.ReadLine();
                    Message.NewMessage(loggedUser, receiver);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultDel == 4)
                {
                    Console.Write("Enter Messages Id: ");
                    int MessId;
                    if (!int.TryParse(Console.ReadLine(), out MessId))
                    {
                        Console.WriteLine("Invalid Entry, Please try again..");
                    }
                    Message.EditMessage(MessId);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultDel == 5)
                {
                    Console.WriteLine("Enter message's Id: ");
                    int MessId;
                    if (!int.TryParse(Console.ReadLine(), out MessId))
                    {
                        Console.WriteLine("Invalid Entry, Please try again..");
                    }
                    Message.DeleteMessage(MessId);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultDel == 6)
                {
                    Console.Clear();
                    Console.WriteLine("Goodbye!");
                    System.Threading.Thread.Sleep(1000);
                    Environment.Exit(0);
                }

                break;

            case "ADMIN":
                Console.WriteLine("Administrator Menu");
                Console.WriteLine();
                Console.WriteLine("1. Message's Options");
                Console.WriteLine("2. User's Options");
                Console.WriteLine("3. Exit");
                int resultAdm;

                if (!int.TryParse(Console.ReadLine(), out resultAdm) || resultAdm > 3 || resultAdm < 1)
                {
                    Console.WriteLine("Invalid Entry, wait a sec and try Again.");
                    System.Threading.Thread.Sleep(1000);
                    Console.Clear();
                    Menu.DisplayMenu(loggedUser);
                }

                if (resultAdm == 1)
                {
                    Console.Clear();
                    Console.WriteLine("Administrator's Message's Menu");
                    Console.WriteLine();
                    Console.WriteLine("1. Read sended messages");
                    Console.WriteLine("2. Read received messases ");
                    Console.WriteLine("3. Send a new Message");
                    Console.WriteLine("4. Edit Message");
                    Console.WriteLine("5. Delete Message");
                    Console.WriteLine("6. Exit");
                    int resultAdmMess;

                    if (!int.TryParse(Console.ReadLine(), out resultAdmMess) || resultAdmMess > 6 || resultAdmMess < 1)
                    {
                        Console.WriteLine("Invalid Entry, wait a sec and try Again.");
                        System.Threading.Thread.Sleep(1000);
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmMess == 1)
                    {
                        Console.Clear();
                        Message.ReadUserMessages(loggedUser);
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmMess == 2)
                    {
                        Console.Clear();
                        Message.ReadReceivedMessages(loggedUser);
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmMess == 3)
                    {
                        Console.Clear();
                        Console.Write("Enter The username you want to send to: ");
                        string receiver = Console.ReadLine();
                        Message.NewMessage(loggedUser, receiver);
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmMess == 4)
                    {
                        Console.Write("Enter Messages Id: ");
                        int MessId;
                        if (!int.TryParse(Console.ReadLine(), out MessId))
                        {
                            Console.WriteLine("Invalid Entry, Please try again..");
                        }
                        Message.EditMessage(MessId);
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmMess == 5)
                    {
                        Console.WriteLine("Enter message's Id: ");
                        int MessId;
                        if (!int.TryParse(Console.ReadLine(), out MessId))
                        {
                            Console.WriteLine("Invalid Entry, Please try again..");
                        }
                        Message.DeleteMessage(MessId);
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmMess == 6)
                    {
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }
                }

                if (resultAdm == 2)
                {
                    Console.Clear();
                    Console.WriteLine("Administrator's User's Menu");
                    Console.WriteLine();
                    Console.WriteLine("1. Create new User");
                    Console.WriteLine("2. Update User's Name");
                    Console.WriteLine("3. Update User's Password");
                    Console.WriteLine("4. View User");
                    Console.WriteLine("5. View All Users");
                    Console.WriteLine("6. Delete User");
                    Console.WriteLine("7. Exit");

                    int resultAdmUser;

                    if (!int.TryParse(Console.ReadLine(), out resultAdmUser) || resultAdmUser > 7 || resultAdmUser < 1)
                    {
                        Console.WriteLine("Invalid Entry, wait a sec and try Again.");
                        System.Threading.Thread.Sleep(1000);
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmUser == 1)
                    {
                        Console.Clear();
                        User.CreateUser();
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmUser == 2)
                    {
                        Console.Clear();
                        Console.Write("Enter User name: ");
                        string usernameOld = Console.ReadLine();
                        User.UpdateUserName(usernameOld);
                        Console.Clear();
                        Menu.DisplayMenu(loggedUser);
                    }

                    if (resultAdmUser == 3)
                    {
                        Console.Clear();
                        Console.Write("Enter username: "******"Enter username: "******"Goodbye!");
                    System.Threading.Thread.Sleep(1000);
                    Environment.Exit(0);
                }
                //return Convert.ToInt32(result);
                break;

            default:
                break;
            }
        }