public ActionResult Profile()
        {
            var model = new ProfileModel();
            model.NewCreditCardTypes = RegisterNewCreditCardTypes(false);
            model.NewCreditCardType = string.Empty;
            model.NewCreditCardExpireMonths = RegisterNewCreditCardExpireMonths(false);
            model.NewCreditCardExpireMonth = string.Empty;
            model.NewCreditCardExpireYears = RegisterNewCreditCardExpireYears(false);
            model.NewCreditCardExpireYear = string.Empty;

            if (User.Identity.IsAuthenticated)
            {
                var email = User.Identity.Name.Trim();
                bool isSuccess = DataBind(model, email);
            }
            else
            {
                RedirectToAction("Index", "Home");
            }

            return View(model);
        }
        public ActionResult Profile(ProfileModel model)
        {
            if (ModelState.IsValid)
            {
                bool changeProfileSucceeded = false;
                try
                {
                    var email = User.Identity.Name.Trim();
                    bool isValid = IsPasswordValid(email, model.CurrentPassword.Trim());
                    if (isValid)
                    {
                        bool isSuccess = Update(model, email);
                        if (isSuccess)
                        {
                            changeProfileSucceeded = true;
                        }
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "The current password provided is incorrect.");
                    }
                }
                catch (Exception)
                {
                    changeProfileSucceeded = false;
                }

                if (changeProfileSucceeded)
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Profile update failed.");
                }
            }

            if (model.NewCreditCardTypes == null)
            {
                model.NewCreditCardTypes = RegisterNewCreditCardTypes(true);
            }

            if (model.NewCreditCardExpireMonths == null)
            {
                model.NewCreditCardExpireMonths = RegisterNewCreditCardExpireMonths(true);
            }

            if (model.NewCreditCardExpireYears == null)
            {
                model.NewCreditCardExpireYears = RegisterNewCreditCardExpireYears(true);
            }
            return View(model);
        }
        private SelectListItem[] RegisterNewCreditCardExpireYears(bool isValid)
        {
            var model = new ProfileModel();

            model.NewCreditCardExpireYears = new[]
            {
                new SelectListItem { Selected = isValid, Text = "--Select--", Value = string.Empty }, 
                new SelectListItem { Text = "2013", Value = "2013" }, 
                new SelectListItem { Text = "2014", Value = "2014" }, 
                new SelectListItem { Text = "2015", Value = "2015" }, 
                new SelectListItem { Text = "2016", Value = "2016" }, 
                new SelectListItem { Text = "2017", Value = "2017" }, 
                new SelectListItem { Text = "2018", Value = "2018" }, 
                new SelectListItem { Text = "2019", Value = "2019" }, 
                new SelectListItem { Text = "2020", Value = "2020" }, 
                new SelectListItem { Text = "2021", Value = "2021" }, 
                new SelectListItem { Text = "2022", Value = "2022" }, 
                new SelectListItem { Text = "2023", Value = "2023" }, 
            };
            return model.NewCreditCardExpireYears;
        }
        private SelectListItem[] RegisterNewCreditCardExpireMonths(bool isValid)
        {
            var model = new ProfileModel();

            model.NewCreditCardExpireMonths = new[]
            {
                new SelectListItem { Selected = isValid, Text = "--Select--", Value = string.Empty }, 
                new SelectListItem { Text = "01", Value = "01" }, 
                new SelectListItem { Text = "02", Value = "02" },
                new SelectListItem { Text = "03", Value = "03" },
                new SelectListItem { Text = "04", Value = "04" },
                new SelectListItem { Text = "05", Value = "05" }, 
                new SelectListItem { Text = "06", Value = "06" },
                new SelectListItem { Text = "07", Value = "07" },
                new SelectListItem { Text = "08", Value = "08" },
                new SelectListItem { Text = "09", Value = "09" },
                new SelectListItem { Text = "10", Value = "10" },
                new SelectListItem { Text = "11", Value = "11" },
                new SelectListItem { Text = "12", Value = "12" },
            };
            return model.NewCreditCardExpireMonths;
        }
        private SelectListItem[] RegisterNewCreditCardTypes(bool isValid)
        {
            var model = new ProfileModel();

            model.NewCreditCardTypes = new[]
            {
                new SelectListItem { Selected = isValid, Text = "--Select--", Value = string.Empty }, 
                new SelectListItem { Text = "visa", Value = "visa" }, 
                new SelectListItem { Text = "mastercard", Value = "mastercard" },
                new SelectListItem { Text = "discover", Value = "discover" },
                new SelectListItem { Text = "amex", Value = "amex" },
            };
            return model.NewCreditCardTypes;
        }
 public CreditCard CreateCreditCard(ProfileModel model)
 {
     CreditCard card = null;
     CreditCard cardCredit = new CreditCard();
     cardCredit.number = model.NewCreditCardNumber.Trim();
     cardCredit.type = model.NewCreditCardType.Trim();
     cardCredit.cvv2 = model.NewCreditCardCVV2.Trim();
     cardCredit.expire_month = Convert.ToInt32(model.NewCreditCardExpireMonth.Trim());
     cardCredit.expire_year = Convert.ToInt32(model.NewCreditCardExpireYear.Trim());
     card = cardCredit.Create(Api);
     return card;
 }
 private bool DataBind(ProfileModel model, string email)
 {
     bool isSuccess = false;
     model.Email = email;
     DataTable datTable = GetUser(email);
     if (datTable != null && datTable.Rows.Count > 0)
     {
         string creditCardId = string.Empty;
         if (datTable.Rows[0]["credit_card_id"] != DBNull.Value)
         {
             creditCardId = Convert.ToString(datTable.Rows[0]["credit_card_id"]);
         }
         CreditCard crdtCard = CreditCard.Get(Api, creditCardId);
         model.CurrentCreditCardNumber = crdtCard.number.Trim();
         isSuccess = true;
     }
     return isSuccess;
 }
        private bool Update(ProfileModel model, string email)
        {
            bool isSuccess = false;
            int rowsAffacted = 0;
            var newPassword = model.NewPassword.Trim();
            var confirmNewPassword = model.ConfirmPassword.Trim();
            var encryptedNewPassword = Secure.Encrypt(newPassword);
            var signInCount = 0;
            var dateTimeNow = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
            var currentSignInAt = dateTimeNow;
            var lastSignInAt = string.Empty;
            var signInIPAddress = string.Empty;
            var currentSignInIP = string.Empty;
            var lastSignInIP = string.Empty;
            var createdAt = string.Empty;
            var updatedAt = dateTimeNow;

            DataTable datTable = GetUser(email);
            if (datTable != null && datTable.Rows.Count > 0)
            {
                var distinctRows = from DataRow dRow in datTable.Rows
                                   where dRow.Field<string>("email") == email
                                   select new { column1 = dRow["sign_in_count"], column2 = dRow["last_sign_in_at"], column3 = dRow["last_sign_in_ip"], column4 = dRow["created_at"] };
                if (distinctRows != null)
                {
                    foreach (var row in distinctRows)
                    {
                        signInCount = Convert.ToInt32(row.column1.ToString());
                        signInCount++;
                        DateTime lastSignInAtDateTime = Convert.ToDateTime(row.column2);
                        lastSignInAt = lastSignInAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                        lastSignInIP = Convert.ToString(row.column3);
                        DateTime createdAtDateTime = Convert.ToDateTime(row.column4);
                        createdAt = createdAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                        break;
                    }
                }
                CreditCard credCard = CreateCreditCard(model);
                var creditCardID = credCard.id;
                var creditCardDescription = credCard.number;
                StringBuilder sqliteQueryUpdate = new StringBuilder();
                sqliteQueryUpdate.Append("UPDATE Users ");
                sqliteQueryUpdate.Append("SET ");
                sqliteQueryUpdate.Append("encrypted_password = @encrypted_password, ");
                sqliteQueryUpdate.Append("sign_in_count = @sign_in_count, ");
                sqliteQueryUpdate.Append("current_sign_in_at = @current_sign_in_at, ");
                sqliteQueryUpdate.Append("last_sign_in_at = @last_sign_in_at, ");
                sqliteQueryUpdate.Append("current_sign_in_ip = @current_sign_in_ip, ");
                sqliteQueryUpdate.Append("last_sign_in_ip = @last_sign_in_ip, ");
                sqliteQueryUpdate.Append("created_at = @created_at, ");
                sqliteQueryUpdate.Append("credit_card_id = @credit_card_id, ");
                sqliteQueryUpdate.Append("credit_card_description = @credit_card_description ");
                sqliteQueryUpdate.Append("WHERE ");
                sqliteQueryUpdate.Append("email = @email");
                SQLiteDataAdapter sqliteDataAdapterUpdate = new SQLiteDataAdapter();
                sqliteDataAdapterUpdate.UpdateCommand = new SQLiteCommand();
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@email", email);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@encrypted_password", encryptedNewPassword);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@sign_in_count", signInCount);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@current_sign_in_at", currentSignInAt);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@last_sign_in_at", lastSignInAt);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@current_sign_in_ip", currentSignInIP);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@last_sign_in_ip", lastSignInIP);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@created_at", createdAt);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@updated_at", updatedAt);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@credit_card_id", creditCardID);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@credit_card_description", creditCardDescription);
                dataAccessObject = new DataAccessLayer();
                rowsAffacted = dataAccessObject.Update(sqliteQueryUpdate.ToString(), sqliteDataAdapterUpdate);
            }
            if (rowsAffacted > 0)
            {
                isSuccess = true;
            }
            return isSuccess;
        }
        private bool Update(ProfileModel model, string email)
        {
            bool isSuccess            = false;
            int  rowsAffacted         = 0;
            var  newPassword          = model.NewPassword.Trim();
            var  confirmNewPassword   = model.ConfirmPassword.Trim();
            var  encryptedNewPassword = Secure.Encrypt(newPassword);
            var  signInCount          = 0;
            var  dateTimeNow          = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
            var  currentSignInAt      = dateTimeNow;
            var  lastSignInAt         = string.Empty;
            var  signInIPAddress      = string.Empty;
            var  currentSignInIP      = string.Empty;
            var  lastSignInIP         = string.Empty;
            var  createdAt            = string.Empty;
            var  updatedAt            = dateTimeNow;

            DataTable datTable = GetUser(email);

            if (datTable != null && datTable.Rows.Count > 0)
            {
                var distinctRows = from DataRow dRow in datTable.Rows
                                   where dRow.Field <string>("email") == email
                                   select new { column1 = dRow["sign_in_count"], column2 = dRow["last_sign_in_at"], column3 = dRow["last_sign_in_ip"], column4 = dRow["created_at"] };
                if (distinctRows != null)
                {
                    foreach (var row in distinctRows)
                    {
                        signInCount = Convert.ToInt32(row.column1.ToString());
                        signInCount++;
                        DateTime lastSignInAtDateTime = Convert.ToDateTime(row.column2);
                        lastSignInAt = lastSignInAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                        lastSignInIP = Convert.ToString(row.column3);
                        DateTime createdAtDateTime = Convert.ToDateTime(row.column4);
                        createdAt = createdAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                        break;
                    }
                }
                CreditCard    credCard              = CreateCreditCard(model);
                var           creditCardID          = credCard.id;
                var           creditCardDescription = credCard.number;
                StringBuilder sqliteQueryUpdate     = new StringBuilder();
                sqliteQueryUpdate.Append("UPDATE Users ");
                sqliteQueryUpdate.Append("SET ");
                sqliteQueryUpdate.Append("encrypted_password = @encrypted_password, ");
                sqliteQueryUpdate.Append("sign_in_count = @sign_in_count, ");
                sqliteQueryUpdate.Append("current_sign_in_at = @current_sign_in_at, ");
                sqliteQueryUpdate.Append("last_sign_in_at = @last_sign_in_at, ");
                sqliteQueryUpdate.Append("current_sign_in_ip = @current_sign_in_ip, ");
                sqliteQueryUpdate.Append("last_sign_in_ip = @last_sign_in_ip, ");
                sqliteQueryUpdate.Append("created_at = @created_at, ");
                sqliteQueryUpdate.Append("credit_card_id = @credit_card_id, ");
                sqliteQueryUpdate.Append("credit_card_description = @credit_card_description ");
                sqliteQueryUpdate.Append("WHERE ");
                sqliteQueryUpdate.Append("email = @email");
                SQLiteDataAdapter sqliteDataAdapterUpdate = new SQLiteDataAdapter();
                sqliteDataAdapterUpdate.UpdateCommand = new SQLiteCommand();
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@email", email);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@encrypted_password", encryptedNewPassword);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@sign_in_count", signInCount);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@current_sign_in_at", currentSignInAt);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@last_sign_in_at", lastSignInAt);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@current_sign_in_ip", currentSignInIP);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@last_sign_in_ip", lastSignInIP);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@created_at", createdAt);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@updated_at", updatedAt);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@credit_card_id", creditCardID);
                sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@credit_card_description", creditCardDescription);
                dataAccessObject = new DataAccessLayer();
                rowsAffacted     = dataAccessObject.Update(sqliteQueryUpdate.ToString(), sqliteDataAdapterUpdate);
            }
            if (rowsAffacted > 0)
            {
                isSuccess = true;
            }
            return(isSuccess);
        }