private void cmdAddToListMethod()
        {
            if (((UsersList)cboList.SelectedItem).Users.All(item => item.Iban != txtIban.Text.ToUpper()))
            {
                DatabaseManagement database = new DatabaseManagement();
                database.OpenConnection();


                database.AddUserList(((UsersList)cboList.SelectedItem).Name, txtIban.Text.Trim());
                userInfo = (EnterpriseUser)database.GetUser(userInfo.Email);

                database.CloseConnection();

                int selectedList = cboList.SelectedIndex;

                displayEnterpriseLists();
                cboList.SelectedIndex = selectedList;
                displayUsersLists();

                txtIban.Text         = "";
                lblNameUser.Text     = "L'utilisateur a bien été ajouté";
                cmdAddToList.Enabled = false;
            }
            else
            {
                MessageBox.Show("Cet utilisateur est déjà dans cette liste");
            }
        }
        /// <summary>
        /// List management form constructor
        /// </summary>
        /// <param name="userInfos">Inherit user informations to display from parent form</param>
        public FrmListManagement(EnterpriseUser userInfos)
        {
            InitializeComponent();
            cmdAddList.Enabled      = false;
            userInfo                = userInfos;
            cmdAddToList.Enabled    = false;
            cmdDeleteList.Enabled   = false;
            txtIban.Enabled         = false;
            cmdListToDelete.Enabled = false;

            displayEnterpriseLists();
        }
        private void cmdDeleteList_Click(object sender, EventArgs e)
        {
            DatabaseManagement database = new DatabaseManagement();

            database.OpenConnection();


            database.DeleteList(((UsersList)cboList.SelectedItem).Name);
            userInfo = (EnterpriseUser)database.GetUser(userInfo.Email);

            database.CloseConnection();

            displayEnterpriseLists();
            lstList.Items.Clear();
            cmdListToDelete.Enabled = false;
            cmdDeleteList.Enabled   = false;
            cmdAddToList.Enabled    = false;
            txtIban.Enabled         = false;
        }
        private void cmdListToDelete_Click(object sender, EventArgs e)
        {
            DatabaseManagement database = new DatabaseManagement();

            database.OpenConnection();

            database.DeleteUserList(((UsersList)cboList.SelectedItem).Name, ((User)lstList.SelectedItem).Iban);

            userInfo = (EnterpriseUser)database.GetUser(userInfo.Email);

            database.CloseConnection();

            int selectedList = cboList.SelectedIndex;

            displayEnterpriseLists();
            cboList.SelectedIndex = selectedList;
            displayUsersLists();

            cmdListToDelete.Enabled = false;
        }
        private void cmdAddList_Click(object sender, EventArgs e)
        {
            DatabaseManagement database = new DatabaseManagement();

            database.OpenConnection();

            try
            {
                database.CreateList(txtNameList.Text.Trim(), userInfo.Iban);
                userInfo = (EnterpriseUser)database.GetUser(userInfo.Email);

                database.CloseConnection();
            }
            catch (ListAlreadyExistsException)
            {
                MessageBox.Show("Ce nom de liste existe déjà");
            }

            displayEnterpriseLists();
            lstList.Items.Clear();
            txtNameList.Text = "";
        }
Beispiel #6
0
        /// <summary>
        /// Get all user's informations and transactions from his email
        /// </summary>
        /// <param name="email">User's email</param>
        /// <returns>Return user's informations</returns>
        public User GetUser(string email)
        {
            MySqlCommand query;

            #region Account type management

            // Create a command object
            query             = connection.CreateCommand();
            query.CommandText = "select iban, type+0 as type, email, money from USERS where email = (@email)";

            //Add parameters to query
            query.Parameters.AddWithValue("@email", email);

            //Get user's money from the database
            DbDataReader reader = query.ExecuteReader();
            reader.Read();

            User user;
            if ((User.AccountType)reader.GetInt32(1) == User.AccountType.Enterprise)
            {
                user = new EnterpriseUser(reader.GetString(0), reader.GetString(2), reader.GetDouble(3));
            }
            else if ((User.AccountType)reader.GetInt32(1) == User.AccountType.Admin)
            {
                user = new AdminUser(reader.GetString(0), reader.GetString(2));
            }
            else
            {
                user = new PublicUser(reader.GetString(0), reader.GetString(2), reader.GetDouble(3));
            }

            reader.Close();
            #endregion


            #region Transactions management

            // Create a command object
            query = connection.CreateCommand();
            if (user.GetType() == typeof(AdminUser))
            {
                query.CommandText = @"select TRANSACTIONS.date, TRANSACTIONS.amount, TRANSACTIONS.reason, 
                                    USER_RECEIVER.email, USER_RECEIVER.iban, USER_SENDER.email, USER_SENDER.iban from TRANSACTIONS
                                    left join USERS as USER_RECEIVER on USER_RECEIVER.id = TRANSACTIONS.idReceiver
                                    left join USERS as USER_SENDER on USER_SENDER.id = TRANSACTIONS.idSender
                                    order by TRANSACTIONS.date desc";
            }
            else
            {
                query.CommandText = @"select TRANSACTIONS.date, TRANSACTIONS.amount, TRANSACTIONS.reason, 
                                    USER_RECEIVER.email, USER_RECEIVER.iban, USER_SENDER.email, USER_SENDER.iban from TRANSACTIONS
                                    left join USERS as USER_RECEIVER on USER_RECEIVER.id = TRANSACTIONS.idReceiver
                                    left join USERS as USER_SENDER on USER_SENDER.id = TRANSACTIONS.idSender
                                    where USER_RECEIVER.email = (@concerned1) OR USER_SENDER.email  = (@concerned2)
                                    order by TRANSACTIONS.date desc";

                //Add parameters to query
                query.Parameters.AddWithValue("@concerned1", email);
                query.Parameters.AddWithValue("@concerned2", email);
            }

            //Get user's money from the database
            reader = query.ExecuteReader();

            if (reader.HasRows)
            {
                //Add each transactions linked to the user
                while (reader.Read())
                {
                    Transaction newTransaction = new Transaction(
                        reader.GetDateTime(0).ToString(),
                        reader.GetDouble(1),
                        reader.GetString(2),
                        reader.GetString(3),
                        reader.GetString(4),
                        reader.GetString(5),
                        reader.GetString(6));
                    ((PublicUser)user).Transactions.Add(newTransaction);
                }
            }
            reader.Close();
            #endregion


            #region Lists management (only enterprise and admin users)

            if (user.GetType() == typeof(EnterpriseUser) || user.GetType() == typeof(AdminUser))
            {
                // Create a command object
                query = connection.CreateCommand();
                if (user.GetType() == typeof(AdminUser))
                {
                    query.CommandText = @"select lists.name, COALESCE(USER_INSIDE.iban,''), COALESCE(USER_INSIDE.email,''), LIST_OWNER.email from lists
                                        left join users_lists on users_lists.idList = lists.id
                                        left join users as LIST_OWNER on lists.idUser = LIST_OWNER.id
                                        left join users as USER_INSIDE on users_lists.idUser = USER_INSIDE.id
                                        order by lists.name asc";
                }
                else
                {
                    query.CommandText = @"select lists.name, COALESCE(USER_INSIDE.iban,''), COALESCE(USER_INSIDE.email,'') from lists
                                        left join users_lists on users_lists.idList = lists.id
                                        left join users as LIST_OWNER on lists.idUser = LIST_OWNER.id
                                        left join users as USER_INSIDE on users_lists.idUser = USER_INSIDE.id
                                        where LIST_OWNER.email = (@owner)
                                        order by lists.name asc";

                    //Add parameters to query
                    query.Parameters.AddWithValue("@owner", email);
                }

                //Get user's money from the database
                reader = query.ExecuteReader();

                bool endFlag = false;
                if (reader.HasRows)
                {
                    reader.Read();

                    while (!endFlag)
                    {
                        UsersList usersList = new UsersList(reader.GetString(0));

                        if (user.GetType() == typeof(AdminUser))
                        {
                            usersList = new AdminUsersList(reader.GetString(0), reader.GetString(3));
                        }
                        else
                        {
                            usersList = new UsersList(reader.GetString(0));
                        }

                        if (reader.GetString(1) != "" && reader.GetString(2) != "")
                        {
                            usersList.Users.Add(new User(reader.GetString(1), reader.GetString(2)));

                            while (true)
                            {
                                if (!reader.Read())
                                {
                                    endFlag = true;
                                    break;
                                }

                                if (reader.GetString(0) == usersList.Name)
                                {
                                    usersList.Users.Add(new User(reader.GetString(1), reader.GetString(2)));
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (!reader.Read())
                            {
                                endFlag = true;
                            }
                        }
                        ((EnterpriseUser)user).Lists.Add(usersList);
                    }
                }
                reader.Close();
            }
            #endregion


            #region Users management (only admin users)

            if (user.GetType() == typeof(AdminUser))
            {
                // Create a command object
                query             = connection.CreateCommand();
                query.CommandText = "select iban, email, money, active, type+0 as type from USERS order by iban asc";

                //Get user's money from the database
                reader = query.ExecuteReader();

                if (reader.HasRows)
                {
                    //Add each transactions linked to the user
                    while (reader.Read())
                    {
                        BankUserInfos newBankUser = new BankUserInfos(
                            reader.GetString(0),
                            reader.GetString(1),
                            reader.GetDouble(2),
                            reader.GetBoolean(3),
                            (User.AccountType)reader.GetInt32(4));
                        ((AdminUser)user).Users.Add(newBankUser);
                    }
                }
                reader.Close();
            }
            #endregion

            return(user);
        }