public IActionResult Create(Users userRegister, string password) { Users userLog = _context.users.SingleOrDefault(user => user.email == userRegister.email); if (userLog != null) { return(RedirectToAction("Index")); } else if (ModelState.IsValid && userRegister.password == password) { PasswordHasher <Users> hasher = new PasswordHasher <Users>(); userRegister.password = hasher.HashPassword(userRegister, userRegister.password); userRegister.created_at = DateTime.Now; _context.Add(userRegister); _context.SaveChanges(); userLog = _context.users.SingleOrDefault(user => user.email == userRegister.email); HttpContext.Session.SetInt32("userId", userLog.id); return(RedirectToAction("AllWeddings")); } else { return(RedirectToAction("Index")); } }
public IActionResult Register(UserViewModel model) { List <User> AllUsers = _context.Users.OrderByDescending(user => user.CreatedAt).ToList(); ViewBag.Users = AllUsers; if (ModelState.IsValid) { if (_context.Users.Any(user => String.Equals(user.Name, model.Name, StringComparison.CurrentCultureIgnoreCase))) { ViewBag.userExists = "A user with this name already exists"; return(View("Accounts")); } else { User newUser = new User { Name = model.Name, CreatedAt = DateTime.Now }; _context.Add(newUser); _context.SaveChanges(); return(RedirectToAction("Accounts")); } } return(View("Accounts")); }
public IActionResult CreateWedding(CreWedding nW) { int?LogId = HttpContext.Session.GetInt32("UserId"); if (LogId == null) { return(Redirect("/")); } if (ModelState.IsValid) { if (nW.Date < DateTime.Now) { ViewBag.InvalidDate = true; return(View("../WeddingPlanner/NewWedding")); } else { User user = _context.Users.Where(u => u.UserId == LogId).FirstOrDefault(); Wedding wedding = new Wedding { Wedder1 = nW.Wedder1, Wedder2 = nW.Wedder2, Date = nW.Date, Address = nW.Address, CreatorId = user.UserId }; _context.Weddings.Add(wedding); _context.SaveChanges(); } } return(Redirect("/dashboard")); }
public IActionResult Register(RegisterViewUser user) { User ExistingUser = _context.Users.SingleOrDefault(u => u.Email == user.Email); if (ExistingUser != null) { ModelState.AddModelError("Email", "This Email already exists"); return(View("Index", user)); } if (ModelState.IsValid) { PasswordHasher <RegisterViewUser> Hasher = new PasswordHasher <RegisterViewUser>(); user.Password = Hasher.HashPassword(user, user.Password); User newuser = new User { fName = user.fName, lName = user.lName, Email = user.Email, Password = user.Password }; _context.Users.Add(newuser); _context.SaveChanges(); User ReturnedUser = _context.Users.SingleOrDefault(userID => userID.Email == user.Email); HttpContext.Session.SetInt32("Id", ReturnedUser.Id); return(RedirectToAction("Dashboard")); } return(View("Index", user)); }
public IActionResult Register(RegisterViewModel user) { if (ModelState.IsValid) { User findEmail = _context.Users.Where(e => e.Email == user.Email).SingleOrDefault(); if (findEmail == null) { User NewUser = new User { FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, Password = user.Password, // CreatedAt = DateTime.Now, // UpdatedAt = DateTime.Now }; _context.Add(NewUser); _context.SaveChanges(); int intID = (int)NewUser.UserId; HttpContext.Session.SetInt32("UserId", intID); return(RedirectToAction("Success")); } else { ViewBag.Email = "Email is already exists in the system. Use another email or login."; return(View("Index")); } } return(View("Index")); }
public IActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { User EmailCheck = _weddingPlannerContext.User.SingleOrDefault(User => User.Email == model.Email); if (EmailCheck == null) { User newUser = new User { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Password = model.Password, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; _weddingPlannerContext.Add(newUser); _weddingPlannerContext.SaveChanges(); int ActiveUserId = _weddingPlannerContext.User.Last().UserId; HttpContext.Session.SetInt32("ActiveUserId", ActiveUserId); return(RedirectToAction("Dashboard")); } else { ViewBag.RegisterMessages = "Email Taken!"; } } return(View("Index")); }
public IActionResult Register(User user) { if (ModelState.IsValid) { if (dbContext.Users.Any(u => u.Email == user.Email)) { ModelState.AddModelError("Email", "Email is already in use!"); } PasswordHasher <User> Hasher = new PasswordHasher <User>(); user.Password = Hasher.HashPassword(user, user.Password); // Save your user object to the database User NewUser = new User { First_Name = @user.First_Name, Last_Name = @user.Last_Name, Email = @user.Email, Password = @user.Password, }; var userEnity = dbContext.Add(NewUser).Entity; dbContext.SaveChanges(); HttpContext.Session.SetInt32("UserId", NewUser.UserId); return(RedirectToAction("Welcome")); } return(View("Index")); }
public IActionResult AddWedding(Wedding model) { List <string> allErrors = new List <string>(); User CurrentUser = _context.Users.SingleOrDefault(person => person.UserId == (int)HttpContext.Session.GetInt32("CurrUserId")); // Input into db if (ModelState.IsValid) { Wedding newWedding = new Wedding { Bride = model.Bride, Groom = model.Groom, Date = model.Date, Address = model.Address, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, UserId = CurrentUser.UserId }; _context.Add(newWedding); _context.SaveChanges(); return(RedirectToAction("Index")); } // If there are validation errors: foreach (var i in ModelState.Values) { if (i.Errors.Count > 0) { allErrors.Add(i.Errors[0].ErrorMessage.ToString()); } } TempData["Errors"] = allErrors; return(RedirectToAction("NewWedding")); }
public IActionResult AddWedding(AddWedding wedding) { System.Console.WriteLine("Add Wedding Works!"); if (ActiveUser == null) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { Wedding Wedding = new Wedding { UserId = ActiveUser.id, WedderOne = wedding.WedderOne, WedderTwo = wedding.WedderTwo, DateOfEvent = wedding.DateOfEvent, Address = wedding.Address, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; _context.Weddings.Add(Wedding); _context.SaveChanges(); return(RedirectToAction("Dashboard")); } ViewBag.UserInfo = ActiveUser; return(View("NewWedding")); }
public IActionResult Register(LoginRegViewModel modelData) { User creatingUser = modelData.newUser; if (ModelState.IsValid) { if (dbContext.users.Any(u => u.Email == creatingUser.Email)) { ModelState.AddModelError("Email", "Email is already in use!"); return(View("Index")); } else { PasswordHasher <User> Hasher = new PasswordHasher <User>(); creatingUser.Password = Hasher.HashPassword(creatingUser, creatingUser.Password); dbContext.Add(creatingUser); dbContext.SaveChanges(); if (HttpContext.Session.GetInt32("UserId") == null) { HttpContext.Session.SetInt32("UserId", creatingUser.UserId); } return(RedirectToAction("Dashboard", new { userID = creatingUser.UserId })); } } else { System.Console.WriteLine("*******************"); System.Console.WriteLine("REGISTRATION NOT WORKING!!!!"); System.Console.WriteLine(creatingUser.FirstName); System.Console.WriteLine(creatingUser.LastName); System.Console.WriteLine(creatingUser.Email); System.Console.WriteLine("*******************"); return(View("Index")); } }
public IActionResult AddWedding(Wedding newWedding) { // if passes validations if (ModelState.IsValid) { // see if the wedding already exists in the DB if (db.Weddings.Any(w => w.WedderOne == newWedding.WedderOne && w.WedderTwo == newWedding.WedderTwo)) { ModelState.AddModelError("WedderOne", "Wedding already exists!"); return(View("WeddingForm")); } // check to see if wedding date is in the future, if not, return to form to show error if (!(DateTime.Now < newWedding.Date)) { ModelState.AddModelError("Date", "Date must be in the future"); return(View("WeddingForm")); } // if the wedding doesn't exist, // then add it to the DB // need to associate the UserId Foriegn Key // in newWedding Object to userid making wedding newWedding.UserId = (int)HttpContext.Session.GetInt32("LoggedUserId"); db.Weddings.Add(newWedding); db.SaveChanges(); // send the newWedding Id to the individual wedding page return(RedirectToAction("Wedding", new{ weddingid = newWedding.WeddingId })); } // if does not pass validations // Return to form and show validation errors return(View("WeddingForm")); }
public IActionResult CreateWedding(WeddingVM model) { int?UserId = HttpContext.Session.GetInt32("UserId"); if (UserId == null) { return(RedirectToAction("Index", "Users")); } else { if (ModelState.IsValid) { Wedding NewCreation = new Wedding(); NewCreation.UserId = (int)UserId; NewCreation.WedderOne = model.Wedder1; NewCreation.WedderTwo = model.Wedder2; NewCreation.Date = model.Date; NewCreation.Address = model.Address; _context.Weddings.Add(NewCreation); _context.SaveChanges(); int WeddingId = _context.Weddings.Last().WeddingId; // HttpContext.Session.SetInt32("WeddingId",WeddingId); return(Redirect($"SingleWedding/{WeddingId}")); } } return(View("PlanWedding", model)); }
public IActionResult AddWedding(WeddingViewModel model) { List <string> allErrors = new List <string>(); User CurrentUser = _context.Users.SingleOrDefault(person => person.id == (int)HttpContext.Session.GetInt32("CurrUserId")); if (ModelState.IsValid) { Wedding newWedding = new Wedding { Wedder1_Name = model.Wedder1_Name, Wedder2_Name = model.Wedder2_Name, Wedding_Date = (DateTime)model.Wedding_Date, Wedding_Address = model.Wedding_Address, created_at = DateTime.Now, updated_at = DateTime.Now, user_id = CurrentUser.id }; _context.Add(newWedding); _context.SaveChanges(); return(RedirectToAction("Index")); } List <string> errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .Select(z => z[0].ErrorMessage.ToString()) .ToList(); foreach (var i in errors) { System.Console.WriteLine(i); } TempData["Errors"] = errors; return(RedirectToAction("NewWedding")); }
public IActionResult Ursvp(int weddingId) { Guest g = dbContext.Guests .FirstOrDefault(gu => gu.WeddingId == weddingId); dbContext.Guests.Remove(g); dbContext.SaveChanges(); return(RedirectToAction("guest")); }
public IActionResult CreateWedding(Wedding wedding) { if (ModelState.IsValid) { db.Add(wedding); db.SaveChanges(); return(RedirectToAction("WeddingPlanner")); } return(View("CreateWedding")); }
public IActionResult Delete(int weddingId) { //query to wedding with wedding id Wedding toDelete = dbContext.Weddings .FirstOrDefault(w => w.WeddingId == weddingId); dbContext.Weddings.Remove(toDelete); dbContext.SaveChanges(); //delete it return(RedirectToAction("Index")); }
public IActionResult Register(User newUser) { if (ModelState.IsValid) { // check that email is unique if (dbContext.Users.Any(u => u.Email == newUser.Email)) { ModelState.AddModelError("Email", "Email already registered!"); return(View("Index")); } else { // hash password PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); // save to db dbContext.Add(newUser); dbContext.SaveChanges(); // login user HttpContext.Session.SetInt32(ID, newUser.UserId); return(RedirectToAction("Success")); } } else { return(View("Index")); } }
public IActionResult CreateRegistration(User register) { // Check initial ModelState if (ModelState.IsValid) { // If a User exists with provided email if (db.Users.Any(r => r.Email == register.Email)) { // Manually add a ModelState error to the Email field, with provided // error message ModelState.AddModelError("Email", "Email already in use!"); return(View("Index"));// You may consider returning to the View at this point } PasswordHasher <User> Hasher = new PasswordHasher <User>(); register.Password = Hasher.HashPassword(register, register.Password); db.Add(register); db.SaveChanges(); HttpContext.Session.SetInt32("UserId", register.UserId); return(RedirectToAction("Dashboard", "WeddingPlanner", new { userid = register.UserId })); } else { return(View("Index"));// other code } }
public IActionResult Register(User newUser) { if (ModelState.IsValid) { if (db.Users.Any(u => u.Email == newUser.Email)) { ModelState.AddModelError("Email", "is taken."); } } if (ModelState.IsValid == false) { return(View("Index")); } PasswordHasher <User> hasher = new PasswordHasher <User>(); newUser.Password = hasher.HashPassword(newUser, newUser.Password); db.Users.Add(newUser); db.SaveChanges(); HttpContext.Session.SetInt32("UserId", newUser.UserId); HttpContext.Session.SetString("FullName", newUser.FullName()); return(RedirectToAction("Success", "Wedding")); }
public IActionResult Register(RegisterUser newUser) { System.Console.WriteLine("I'm Here!!!"); if (_context.users.Where(u => u.Email == newUser.Email).SingleOrDefault() != null) { ModelState.AddModelError("Email", "Email is already in use!"); //checks to see if the new email is already in use. } if (ModelState.IsValid) { PasswordHasher <RegisterUser> hasher = new PasswordHasher <RegisterUser>(); User User = new User { FirstName = newUser.FirstName, LastName = newUser.LastName, Email = newUser.Email, Password = hasher.HashPassword(newUser, newUser.Password), CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; User theUser = _context.Add(User).Entity; _context.SaveChanges(); System.Console.WriteLine("YAY MADE IT!"); HttpContext.Session.SetInt32("id", theUser.id); return(RedirectToAction("Dashboard", "Wedding")); } return(View("Index")); }
public IActionResult CreateUser(RegisterUser u) { if (ModelState.IsValid) { List <User> users = _context.Users.Where(nu => nu.Email == u.Email).ToList(); if (users.Count > 0) { ViewBag.InvalidEmail = true; return(View("Index")); } PasswordHasher <User> Hasher = new PasswordHasher <User>(); User nUser = new User(); nUser.Password = Hasher.HashPassword(nUser, u.Password); nUser.FirstName = u.FirstName; nUser.LastName = u.LastName; nUser.Email = u.Email; _context.Add(nUser); _context.SaveChanges(); HttpContext.Session.SetInt32("UserId", nUser.UserId); return(Redirect("/dashboard")); } return(View("Index")); }
public IActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { User user = _context.user.SingleOrDefault(u => u.Email == model.Email); model.Unique = (user == null)?0:1; TryValidateModel(model); if (!ModelState.IsValid) { return(View("Register")); } else { User newUser = model.createUser(); PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); _context.user.Add(newUser); _context.SaveChanges(); HttpContext.Session.SetInt32("UserId", (int)newUser.UserId); HttpContext.Session.SetString("UserName", (string)newUser.FirstName); return(RedirectToAction("Dashboard", "Dashboard")); } } return(View("Register")); }
public IActionResult Processreg(Regis newregis) { ViewBag.errors = ""; TryValidateModel(newregis); if (ModelState.IsValid) { User NewUser = new User { FName = newregis.FName, LName = newregis.LName, Email = newregis.Email, Password = newregis.Password }; _context.Add(NewUser); _context.SaveChanges(); HttpContext.Session.SetInt32("userid", (int)NewUser.UserId); HttpContext.Session.SetString("FName", (string)NewUser.FName); ViewBag.Fname = NewUser.FName; return(Redirect("Dashbd")); } else { //ViewBag.errors = ModelState.Values; return(View("Register")); } }
public IActionResult RegisterUser(Wrapper regModel) { RegisterVM user = regModel.Register; if (ModelState.IsValid) { List <User> CurrentUser = _context.Users.Where(u => u.Email == user.RegEmail).ToList(); if (CurrentUser.Count > 0) { TempData["EmailError"] = "Email already been used"; return(View("Index", regModel)); } else { PasswordHasher <RegisterVM> Hasher = new PasswordHasher <RegisterVM>(); User newUser = new User(); newUser.Password = Hasher.HashPassword(user, user.RegPassword); newUser.FirstName = user.FirstName; newUser.LastName = user.LastName; newUser.Email = user.RegEmail; _context.Users.Add(newUser); _context.SaveChanges(); int UserId = _context.Users.Last().UserId; HttpContext.Session.SetInt32("UserId", UserId); return(RedirectToAction("DisplayAll", "Weddings")); } } return(View("Register", regModel)); }
public IActionResult Register(User user) { if (ModelState.IsValid) { // check for uniqueness of email! if (dbContext.Users.Any(u => u.Email == user.Email)) { ModelState.AddModelError("Email", "Email already in use."); return(View("Index")); } // hash pw PasswordHasher <User> hasher = new PasswordHasher <User>(); string hashedPw = hasher.HashPassword(user, user.Password); user.Password = hashedPw; dbContext.Add(user); // once we save changes... // user.UserId will be updated! dbContext.SaveChanges(); HttpContext.Session.SetInt32("UserId", user.UserId); return(RedirectToAction("Index", "Wedding")); } return(View("Index")); }
public IActionResult Delete(int id) { List <Guests> RemoveFirst = _context.Guests.Where(w => w.WeddingId == id).Include(p => p.Wedding).ToList(); if (RemoveFirst != null) { foreach (Guests item in RemoveFirst) { _context.Remove(item); } } Wedding RemoveSecond = _context.Weddings.SingleOrDefault(u => u.WeddingId == id); _context.Weddings.Remove(RemoveSecond); _context.SaveChanges(); return(RedirectToAction("Dashboard")); }
public IActionResult CreateNewWedding(Wedding newWedding) { User userinsession = db.Users.FirstOrDefault(user => user.UserId == HttpContext.Session.GetInt32("UserId")); if (!isLoggedIn) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { newWedding.Creator = userinsession; db.Add(newWedding); db.SaveChanges(); return(RedirectToAction("Dashboard", newWedding)); } return(View("NewWedding")); }
public IActionResult Register(User newUser) { if (ModelState.IsValid) { if (db.Users.Any(u => u.Email == newUser.Email)) { ModelState.AddModelError("Email", "is already taken!"); } } // Reload with error messages if (ModelState.IsValid == false) { return(View("Index")); } // Overwrite password with hashed version PasswordHasher <User> hasher = new PasswordHasher <User>(); newUser.Password = hasher.HashPassword(newUser, newUser.Password); // Add attributes newUser.CreatedAt = DateTime.Now; newUser.UpdatedAt = DateTime.Now; // Add and save db.Users.Add(newUser); db.SaveChanges(); HttpContext.Session.SetInt32("UserId", newUser.UserId); return(RedirectToAction("Dashboard")); }
public IActionResult Create(Wedding newWedding) { if (!isLoggedIn) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid == false) { // Send back to the page with the form to show errors. return(View("New")); } // ModelState IS valid... newWedding.UserId = (int)uid; db.Weddings.Add(newWedding); db.SaveChanges(); return(RedirectToAction("Success")); }
public IActionResult register(User newUser) { if (ModelState.IsValid) { PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); dbContext.Add(newUser); dbContext.SaveChanges(); User oneUser = dbContext.Users.Last(); int x = oneUser.UserId; HttpContext.Session.SetInt32("uid", x); return(RedirectToAction("guest")); } else { return(View("Index")); } }