Пример #1
0
        private void updateInfos()
        {
            lblEmail.Text = userInformations.Email;

            datHistory.Rows.Clear();
            datAllUsers.Rows.Clear();
            datList.Rows.Clear();

            if (txtSearch.Text.Trim() == "")
            {
                for (int i = 0; i < 30 && i < userInformations.Users.Count; i++)
                {
                    BankUserInfos user = userInformations.Users[i];

                    string[] row = { user.ActiveAccount ? "Actif" : "Inactif", user.UserType.ToString(), user.Email, user.Iban, $"{user.Money.ToString("0.00")} CHF" };
                    datAllUsers.Rows.Add(row);
                }

                for (int i = 0; i < 30 && i < userInformations.Transactions.Count; i++)
                {
                    Transaction transaction = userInformations.Transactions[i];

                    string[] row = { transaction.SenderDefine + " \n" + transaction.SenderIban, transaction.ReceiverDefine + " \n" + transaction.ReceiverIban, $"{transaction.Amount.ToString("0.00")} CHF", transaction.Reason, transaction.Date };
                    datHistory.Rows.Add(row);
                }

                int maxCount = 0;
                foreach (AdminUsersList list in userInformations.Lists)
                {
                    if (list.Users.Any())
                    {
                        foreach (User userInList in list.Users)
                        {
                            if (maxCount < 30)
                            {
                                string[] row = { list.Owner, list.ToString(), userInList.Email };
                                datList.Rows.Add(row);
                            }
                            else
                            {
                                break;
                            }

                            maxCount++;
                        }
                    }
                    else if (maxCount < 30)
                    {
                        string[] row = { list.Owner, list.ToString(), "Vide" };
                        datList.Rows.Add(row);

                        maxCount++;
                    }
                }
            }
            else
            {
                foreach (BankUserInfos user in userInformations.Users)
                {
                    if (user.Email.Contains(txtSearch.Text.Trim()) || user.Iban.Contains(txtSearch.Text.Trim()))
                    {
                        string[] row = { user.ActiveAccount ? "Actif" : "Inactif", user.UserType.ToString(), user.Email, user.Iban, $"{user.Money.ToString("0.00")} CHF" };
                        datAllUsers.Rows.Add(row);
                    }
                }

                foreach (Transaction transaction in userInformations.Transactions)
                {
                    if (transaction.SenderDefine.Contains(txtSearch.Text.Trim()) || transaction.ReceiverDefine.Contains(txtSearch.Text.Trim()))
                    {
                        string[] row = { transaction.SenderDefine + " \n" + transaction.SenderIban, transaction.ReceiverDefine + " \n" + transaction.ReceiverIban, $"{transaction.Amount.ToString("0.00")} CHF", transaction.Reason, transaction.Date };
                        datHistory.Rows.Add(row);
                    }
                }

                foreach (AdminUsersList list in userInformations.Lists)
                {
                    if (list.Users.Any())
                    {
                        foreach (User userInList in list.Users)
                        {
                            if (userInList.Email.Contains(txtSearch.Text.Trim()) ||
                                list.Owner.Contains(txtSearch.Text.Trim()) || list.Name.Contains(txtSearch.Text.Trim()))
                            {
                                string[] row = { list.Owner, list.ToString(), userInList.Email };
                                datList.Rows.Add(row);
                            }
                        }
                    }
                    else
                    {
                        if (list.Owner.Contains(txtSearch.Text.Trim()) || list.Name.Contains(txtSearch.Text.Trim()))
                        {
                            string[] row = { list.Owner, list.ToString(), "Vide" };
                            datList.Rows.Add(row);
                        }
                    }
                }
            }
        }
Пример #2
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);
        }