Ejemplo n.º 1
0
        public YYaccountnumber GetByAccountNumber(string accountNumber)
        {
            YYaccountnumber account = null;

            DbConnection.Instance().OpenConnection();
            var queryString = "select * from `accounts` where username = @accountnumber and status = 1";
            var cmd         = new MySqlCommand(queryString, DbConnection.Instance().Connection);

            cmd.Parameters.AddWithValue("@accountnumber", accountNumber);
            var reader  = cmd.ExecuteReader();
            var isExist = reader.Read();

            if (isExist)
            {
                account = new YYaccountnumber()
                {
                    Accountnumber = reader.GetString("accountNumber"),
                    Username      = reader.GetString("username"),
                    Password      = reader.GetString("password"),
                    Salt          = reader.GetString("salt"),
                    Fullname      = reader.GetString("fullName"),
                    Balance       = reader.GetInt32("balance")
                };
            }

            DbConnection.Instance().CloseConnection();
            return(account);
        }
Ejemplo n.º 2
0
        public Boolean Save(YYaccountnumber account)
        {
            DbConnection.Instance().OpenConnection();
            var sqlQuery =
                "insert into accounts (accountnumber,username,password,balance,identityCard,fullname,email,phoneNumber,address,dob,gender,createdAt,updateAt,status) " +
                "values (@accountnumber,@username,@password,@balance,@identityCard,@fullname,@email,@phoneNumber,@address,@dob,@gender,@createdAt,@updateAt,@status)";
            var cmd = new MySqlCommand(sqlQuery, DbConnection.Instance().Connection);

            cmd.Parameters.AddWithValue("@accountnumber", account.Accountnumber);
            cmd.Parameters.AddWithValue("@username", account.Username);
            cmd.Parameters.AddWithValue("@password", account.Password);
            cmd.Parameters.AddWithValue("@balance", account.Balance);
            cmd.Parameters.AddWithValue("@identityCard", account.IdentityCard);
            cmd.Parameters.AddWithValue("@fullname", account.Fullname);
            cmd.Parameters.AddWithValue("@email", account.Email);
            cmd.Parameters.AddWithValue("@phoneNumber", account.PhoneNumber);
            cmd.Parameters.AddWithValue("@address", account.Address);
            cmd.Parameters.AddWithValue("@dob", account.Dob);
            cmd.Parameters.AddWithValue("@gender", account.Gender);
            cmd.Parameters.AddWithValue("@createdAt", account.CreatedAt);
            cmd.Parameters.AddWithValue("@updateAt", account.UpdateAt);
            cmd.Parameters.AddWithValue("@status", account.Status);
            cmd.Parameters.AddWithValue("@salt", account.Salt);
            cmd.ExecuteNonQuery();
            DbConnection.Instance().CloseConnection();
            return(true);
        }
Ejemplo n.º 3
0
        public YYaccountnumber getUsername(string username)
        {
            DbConnection.Instance().OpenConnection();
            var queryString = "select * from accounts where username = @username";
            var cmd         = new MySqlCommand(queryString, DbConnection.Instance().Connection);

            cmd.Parameters.AddWithValue("@username", username);
            var             reader  = cmd.ExecuteReader();
            YYaccountnumber account = null;

            if (reader.Read())
            {
                string  accountnumber = reader.GetString("accountnumber");
                string  username1     = reader.GetString("username");
                string  password      = reader.GetString("password");
                decimal balance       = reader.GetDecimal("balance");
                string  identityCard  = reader.GetString("identityCard");
                string  fullName      = reader.GetString("fullName");
                string  email         = reader.GetString("email");
                string  phoneNumber   = reader.GetString("phoneNumber");
                string  address       = reader.GetString("address");
                int     gender        = reader.GetInt32("gender");
                account = new YYaccountnumber(accountnumber, username1, password, balance, identityCard, fullName,
                                              email, phoneNumber, address, gender);
            }

            DbConnection.Instance().CloseConnection();
            return(account);
        }
Ejemplo n.º 4
0
        private YYaccountnumber GetAccountInformation()
        {
            Console.WriteLine("----------------REGISTER INFORMATION----------------");
            Console.WriteLine("Username: "******"Password: "******"Confirm Password: "******"Balance: ");
            var balance = Utility.GetDecimalNumber();

            Console.WriteLine("Identity Card: ");
            var identityCard = Console.ReadLine();

            Console.WriteLine("Full Name: ");
            var fullName = Console.ReadLine();

            Console.WriteLine("Birthday: ");
            var birthday = Console.ReadLine();

            Console.WriteLine("Gender (1. Male |2. Female| 3.Others): ");
            var gender = Utility.GetInt32Number();

            Console.WriteLine("Email: ");
            var email = Console.ReadLine();

            Console.WriteLine("Phone Number: ");
            var phoneNumber = Console.ReadLine();

            Console.WriteLine("Address: ");
            var address = Console.ReadLine();
            var acc     = new YYaccountnumber()
            {
                Username     = username,
                Password     = password,
                Cpassword    = cpassword,
                IdentityCard = identityCard,
                Gender       = gender,
                Balance      = balance,
                Address      = address,
                Dob          = birthday,
                Fullname     = fullName,
                Email        = email,
                PhoneNumber  = phoneNumber
            };

            return(acc);
        }
Ejemplo n.º 5
0
        public bool Register()
        {
            YYaccountnumber             account = GetAccountInformation();
            Dictionary <string, string> errors  = account.CheckValidate();

            if (errors.Count > 0)
            {
                Console.WriteLine("Please fix errros below and try again.");
                foreach (var error in errors)
                {
                    Console.WriteLine(error);
                }
                return(false);
            }
            else
            {
                // Lưu vào database.
                account.EncryptPassword();
                model.Save(account);
                return(true);
            }
        }
Ejemplo n.º 6
0
        public bool Login()
        {
            Console.WriteLine("----------------LOGIN INFORMATION----------------");
            Console.WriteLine("Username: "******"Password: ");
            var             password        = Console.ReadLine();
            YYaccountnumber existingAccount = model.getUsername(username);

            if (existingAccount == null)
            {
                return(false);
            }

            if (!existingAccount.CheckEncryptedPassword(password))
            {
                return(false);
            }

            Program.currentLoggedInYyAccount = existingAccount;
            return(true);
        }
Ejemplo n.º 7
0
        public bool TransferAmount(YYaccountnumber account, YYtransaction historyTransaction)
        {
            DbConnection.Instance().OpenConnection();
            var transaction = DbConnection.Instance().Connection.BeginTransaction();

            try
            {
                // Kiểm tra số tài khoản mới nhất
                var          queryBalance        = "select `balance` from `account` where username = @username and status = 1";
                MySqlCommand queryBalanceCommand = new MySqlCommand(queryBalance, DbConnection.Instance().Connection);
                queryBalanceCommand.Parameters.AddWithValue("@username", account.Username);
                var balanceReader = queryBalanceCommand.ExecuteReader();
                // Không tìm thấy tài khoản tương ứng, throw lỗi.
                if (!balanceReader.Read())
                {
                    throw new TransactionException("Invalid username");
                }

                var currentBalance = balanceReader.GetDecimal("balance");
                currentBalance -= historyTransaction.Amount;
                balanceReader.Close();

                // Update số dư vào database.
                var updateAccountResult       = 0;
                var queryUpdateAccountBalance =
                    "update `accounts` set balance = @balance where username = @username and status = 1";
                var cmdUpdateAccountBalance =
                    new MySqlCommand(queryUpdateAccountBalance, DbConnection.Instance().Connection);
                cmdUpdateAccountBalance.Parameters.AddWithValue("@username", account.Username);
                cmdUpdateAccountBalance.Parameters.AddWithValue("@balance", currentBalance);
                updateAccountResult = cmdUpdateAccountBalance.ExecuteNonQuery();

                // Lưu thông tin transaction vào bảng transaction.
                var insertTransactionResult = 0;
                var queryInsertTransaction  = "insert into `transaction` " +
                                              "(id, fromAccountNumber, amount, content, toAccountNumber, type, status) " +
                                              "values (@id, @fromAccountNumber, @amount, @content, @toAccountNumber, @type, @status)";
                var cmdInsertTransaction =
                    new MySqlCommand(queryInsertTransaction, DbConnection.Instance().Connection);
                cmdInsertTransaction.Parameters.AddWithValue("@id", historyTransaction.Id);
                cmdInsertTransaction.Parameters.AddWithValue("@fromAccountNumber",
                                                             historyTransaction.SenderAccountnumber);
                cmdInsertTransaction.Parameters.AddWithValue("@amount", historyTransaction.Amount);
                cmdInsertTransaction.Parameters.AddWithValue("@content", historyTransaction.Content);
                cmdInsertTransaction.Parameters.AddWithValue("@toAccountNumber",
                                                             historyTransaction.ReceiverAccountnumber);
                cmdInsertTransaction.Parameters.AddWithValue("@type", historyTransaction.Type);
                cmdInsertTransaction.Parameters.AddWithValue("@status", historyTransaction.Status);
                insertTransactionResult = cmdInsertTransaction.ExecuteNonQuery();

                // Kiểm tra lại câu lệnh
                if (updateAccountResult == 1 && insertTransactionResult == 1)
                {
                    transaction.Commit();
                    return(true);
                }
            }
            catch (TransactionException e)
            {
                transaction.Rollback();
                return(false);
            }

            DbConnection.Instance().CloseConnection();
            return(false);
        }