//
        // GET: /Account/

        public ActionResult SignUp()
        {
            var model = new SignUpModel();
            model.CreditCardTypes = RegisterCreditCardTypes(false);
            model.CreditCardType = string.Empty;
            model.CreditCardExpireMonths = RegisterCreditCardExpireMonths(false);
            model.CreditCardExpireMonth = string.Empty;
            model.CreditCardExpireYears = RegisterCreditCardExpireYears(false);
            model.CreditCardExpireYear = string.Empty;
            return View(model);
        }
 public CreditCard CreateCreditCard(SignUpModel model)
 {
     CreditCard card = null;
     CreditCard cardCredit = new CreditCard();
     cardCredit.number = model.CreditCardNumber.Trim();
     cardCredit.type = model.CreditCardType.Trim();
     cardCredit.cvv2 = model.CreditCardCVV2.Trim();
     cardCredit.expire_month = Convert.ToInt32(model.CreditCardExpireMonth.Trim());
     cardCredit.expire_year = Convert.ToInt32(model.CreditCardExpireYear.Trim());
     card = cardCredit.Create(Api);
     return card;
 }
 private SelectListItem[] RegisterCreditCardTypes(bool isValid)
 {
     var model = new SignUpModel();
     model.CreditCardTypes = 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.CreditCardTypes;
 }
        private SelectListItem[] RegisterCreditCardExpireMonths(bool isValid)
        {
            var model = new SignUpModel();

            model.CreditCardExpireMonths = 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.CreditCardExpireMonths);
        }
        private SelectListItem[] RegisterCreditCardExpireYears(bool isValid)
        {
            var model = new SignUpModel();

            model.CreditCardExpireYears = 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.CreditCardExpireYears);
        }
        public ActionResult SignUp(SignUpModel model)
        {
            if (ModelState.IsValid)
            {
                bool isExistingUser = CheckIsExistingUser(model);
                if (isExistingUser)
                {
                    ModelState.AddModelError(string.Empty, "Email already exists.");
                }
                else
                {
                    bool isSuccess = Insert(model);
                    if (isSuccess)
                    {
                        FormsAuthentication.SetAuthCookie(model.Email, false /* createPersistentCookie */);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Registration failed.");
                    }
                }
            }

            if (model.CreditCardTypes == null)
            {
                model.CreditCardTypes = RegisterCreditCardTypes(true);
            }

            if (model.CreditCardExpireMonths == null)
            {
                model.CreditCardExpireMonths = RegisterCreditCardExpireMonths(true);
            }

            if (model.CreditCardExpireYears == null)
            {
                model.CreditCardExpireYears = RegisterCreditCardExpireYears(true);
            }
            return View(model);
        }
        private bool CheckIsExistingUser(SignUpModel model)
        {
            bool          isExistingUser    = false;
            DataTable     datTable          = new DataTable();
            int           rows              = 0;
            var           email             = model.Email.Trim();
            StringBuilder sqliteQuerySelect = new StringBuilder();

            sqliteQuerySelect.Append("SELECT ");
            sqliteQuerySelect.Append("count(*) AS NumberOfUsers ");
            sqliteQuerySelect.Append("FROM users ");
            sqliteQuerySelect.Append("WHERE email = @email");
            SQLiteDataAdapter sqliteDataAdapterSelect = new SQLiteDataAdapter();

            sqliteDataAdapterSelect.SelectCommand = new SQLiteCommand();
            sqliteDataAdapterSelect.SelectCommand.Parameters.AddWithValue("@email", email);
            dataAccessObject = new DataAccessLayer();
            datTable         = dataAccessObject.Select(sqliteQuerySelect.ToString(), sqliteDataAdapterSelect);

            if (datTable != null && datTable.Rows.Count > 0)
            {
                var distinctRows = from DataRow dRow in datTable.Rows
                                   select new { column1 = dRow["NumberOfUsers"] };
                if (distinctRows != null)
                {
                    foreach (var row in distinctRows)
                    {
                        rows = Convert.ToInt32(row.column1);
                        break;
                    }
                }
            }
            if (rows == 1)
            {
                isExistingUser = true;
            }
            return(isExistingUser);
        }
 private SelectListItem[] RegisterCreditCardExpireYears(bool isValid)
 {
     var model = new SignUpModel();
     model.CreditCardExpireYears = 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.CreditCardExpireYears;
 }
 private SelectListItem[] RegisterCreditCardExpireMonths(bool isValid)
 {
     var model = new SignUpModel();
     model.CreditCardExpireMonths = 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.CreditCardExpireMonths;
 }
        private bool CheckIsExistingUser(SignUpModel model)
        {
            bool isExistingUser = false;
            DataTable datTable = new DataTable();
            int rows = 0;
            var email = model.Email.Trim();
            StringBuilder sqliteQuerySelect = new StringBuilder();
            sqliteQuerySelect.Append("SELECT ");
            sqliteQuerySelect.Append("count(*) AS NumberOfUsers ");
            sqliteQuerySelect.Append("FROM users ");
            sqliteQuerySelect.Append("WHERE email = @email");
            SQLiteDataAdapter sqliteDataAdapterSelect = new SQLiteDataAdapter();
            sqliteDataAdapterSelect.SelectCommand = new SQLiteCommand();
            sqliteDataAdapterSelect.SelectCommand.Parameters.AddWithValue("@email", email);
            dataAccessObject = new DataAccessLayer();
            datTable = dataAccessObject.Select(sqliteQuerySelect.ToString(), sqliteDataAdapterSelect);

            if (datTable != null && datTable.Rows.Count > 0)
            {
                var distinctRows = from DataRow dRow in datTable.Rows
                                   select new { column1 = dRow["NumberOfUsers"] };
                if (distinctRows != null)
                {
                    foreach (var row in distinctRows)
                    {
                        rows = Convert.ToInt32(row.column1);
                        break;
                    }
                }
            }
            if (rows == 1)
            {
                isExistingUser = true;
            }
            return isExistingUser;
        }
 private bool Insert(SignUpModel model)
 {
     bool isSuccess = false;
     int rowsAffacted = 0;
     var email = model.Email.Trim();
     var password = model.Password.Trim();
     var passwordConfirmation = model.ConfirmPassword.Trim();
     var encryptedPassword = Secure.Encrypt(password);
     var signInCount = 1;
     var dateTimeNow = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
     var currentSignInAt = dateTimeNow;
     var lastSignInAt = dateTimeNow;
     var signInIPAddress = string.Empty;
     var currentSignInIP = signInIPAddress;
     var lastSignInIP = signInIPAddress;
     var createdAt = dateTimeNow;
     var updatedAt = dateTimeNow;
     CreditCard credCard = CreateCreditCard(model);
     var creditCardID = credCard.id;
     var creditCardDescription = credCard.number;
     StringBuilder sqliteQueryInsert = new StringBuilder();
     sqliteQueryInsert.Append("INSERT INTO users");
     sqliteQueryInsert.Append("(");
     sqliteQueryInsert.Append("email, ");
     sqliteQueryInsert.Append("encrypted_password, ");
     sqliteQueryInsert.Append("sign_in_count, ");
     sqliteQueryInsert.Append("current_sign_in_at,");
     sqliteQueryInsert.Append("last_sign_in_at, ");
     sqliteQueryInsert.Append("current_sign_in_ip, ");
     sqliteQueryInsert.Append("last_sign_in_ip, ");
     sqliteQueryInsert.Append("created_at, ");
     sqliteQueryInsert.Append("updated_at, ");
     sqliteQueryInsert.Append("credit_card_id, ");
     sqliteQueryInsert.Append("credit_card_description ");
     sqliteQueryInsert.Append(") ");
     sqliteQueryInsert.Append("VALUES ");
     sqliteQueryInsert.Append("(");
     sqliteQueryInsert.Append("@email, ");
     sqliteQueryInsert.Append("@encrypted_password, ");
     sqliteQueryInsert.Append("@sign_in_count, ");
     sqliteQueryInsert.Append("@current_sign_in_at,");
     sqliteQueryInsert.Append("@last_sign_in_at, ");
     sqliteQueryInsert.Append("@current_sign_in_ip, ");
     sqliteQueryInsert.Append("@last_sign_in_ip, ");
     sqliteQueryInsert.Append("@created_at, ");
     sqliteQueryInsert.Append("@updated_at, ");
     sqliteQueryInsert.Append("@credit_card_id, ");
     sqliteQueryInsert.Append("@credit_card_description ");
     sqliteQueryInsert.Append(")");
     SQLiteDataAdapter sqliteDataAdapterInsert = new SQLiteDataAdapter();
     sqliteDataAdapterInsert.InsertCommand = new SQLiteCommand();
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@email", email);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@encrypted_password", encryptedPassword);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@sign_in_count", signInCount);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@current_sign_in_at", currentSignInAt);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@last_sign_in_at", lastSignInAt);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@current_sign_in_ip", currentSignInIP);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@last_sign_in_ip", lastSignInIP);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@created_at", createdAt);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@updated_at", updatedAt);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@credit_card_id", creditCardID);
     sqliteDataAdapterInsert.InsertCommand.Parameters.AddWithValue("@credit_card_description", creditCardDescription);
     dataAccessObject = new DataAccessLayer();
     rowsAffacted = dataAccessObject.Insert(sqliteQueryInsert.ToString(), sqliteDataAdapterInsert);
     if (rowsAffacted > 0)
     {
         isSuccess = true;
     }
     return isSuccess;
 }
        public ActionResult SignUp(SignUpModel model)
        {
            if (ModelState.IsValid)
            {
                var email = model.Email.Trim();
                bool isExistingUser = CheckIsExistingUser(email);
                if (isExistingUser)
                {
                    ModelState.AddModelError(string.Empty, "Email already exists.");
                }
                else
                {
                    var password = model.Password.Trim();
                    var passwordConfirmation = model.ConfirmPassword.Trim();
                    var creditCardNumber = model.CreditCardNumber.Trim();
                    var creditCardCVV2 = model.CreditCardCVV2.Trim();
                    var creditCardType = model.CreditCardType.Trim();
                    var creditCardExpireMonth = model.CreditCardExpireMonth.Trim();
                    var creditCardExpireYear = model.CreditCardExpireYear.Trim();
                    bool isSuccess = Insert(email, password, passwordConfirmation, creditCardType, creditCardNumber, creditCardCVV2, creditCardExpireMonth, creditCardExpireYear);
                    if (isSuccess)
                    {
                        FormsAuthentication.SetAuthCookie(model.Email, false /* createPersistentCookie */);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Registration failed.");
                    }
                }
            }

            if (model.CreditCardTypes == null)
            {
                model.CreditCardTypes = RegisterCreditCardTypes(true);
            }

            if (model.CreditCardExpireMonths == null)
            {
                model.CreditCardExpireMonths = RegisterCreditCardExpireMonths(true);
            }

            if (model.CreditCardExpireYears == null)
            {
                model.CreditCardExpireYears = RegisterCreditCardExpireYears(true);
            }
            return View(model);
        }