public IActionResult Register(User newUser) { if (ModelState.IsValid) { if (dbContext.Users.Any(u => u.Email == newUser.Email)) { ModelState.AddModelError("Email", "Email already in use!"); return(View("Index")); } else { dbContext.Add(newUser); PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); dbContext.SaveChanges(); HttpContext.Session.SetInt32("LoggedInUserId", newUser.UserId); HttpContext.Session.SetString("LoggedInUserName", newUser.Name); return(RedirectToAction("Home")); } } else { return(View("Index")); } }
public IActionResult Register(RegisterUser newUser) { System.Console.WriteLine("Register 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", "Activity")); } return(View("Index")); }
public IActionResult RegisterUser(RegisterUser newUser) { if (DbContext.Users.Where(user => user.Email == newUser.Email).SingleOrDefault() != null) { ModelState.AddModelError("Email", "Email already in use"); } PasswordHasher <RegisterUser> hasher = new PasswordHasher <RegisterUser>(); if (ModelState.IsValid) { User theUser = new User { FirstName = newUser.FirstName, LastName = newUser.LastName, Email = newUser.Email, Password = hasher.HashPassword(newUser, newUser.Password), CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, }; User loggedUser = DbContext.Add(theUser).Entity; DbContext.SaveChanges(); HttpContext.Session.SetInt32("id", loggedUser.UserId); return(RedirectToAction("Index", "Activity")); } ; return(View("Index")); }
public IActionResult Register(RegisterViewModels model) { if (_context.Users.Where(u => u.Email == model.Email).SingleOrDefault() != null) { ModelState.AddModelError("Username", "Username in use"); } if (ModelState.IsValid) { PasswordHasher <User> Hasher = new PasswordHasher <User>(); User NewUser = new User { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; NewUser.Password = Hasher.HashPassword(NewUser, model.Password); _context.Add(NewUser); _context.SaveChanges(); HttpContext.Session.SetInt32("UserId", NewUser.UserId); return(RedirectToAction("Index", "Activity")); } else { return(View("Index")); } }
public IActionResult AddActivity(NewActivity newActivity) { if (HttpContext.Session.GetInt32("id") == null) { return(RedirectToAction("Index", "User")); } List <Activity> ParticipatingActivities = DbContext.Participate.Where(p => p.UserId == ActiveUser.UserId).Select(u => u.ParticipatingActivities).ToList(); foreach (var activity in ParticipatingActivities) { Console.WriteLine(activity.Date); DateTime start_date = activity.Date; DateTime end_date = start_date.AddMinutes((double)activity.Duration); Console.WriteLine(end_date); DateTime activity_date = newActivity.Date; DateTime activity_end_date = activity_date.AddMinutes((double)newActivity.Duration); if (activity_date < start_date && activity_end_date < start_date) { newActivity.Date = newActivity.Date; newActivity.Duration = newActivity.Duration; } else if (activity_date > start_date && activity_date > end_date) { newActivity.Date = newActivity.Date; newActivity.Duration = newActivity.Duration; } else { ModelState.AddModelError("Date", "Activity conflicts with another activity"); } } List <Activity> CreatedActivities = DbContext.Activities.Where(u => u.UserId == ActiveUser.UserId).Include(a => a.Coordinator).ToList(); foreach (var activity in CreatedActivities) { DateTime start_date = activity.Date; DateTime end_date = start_date.AddMinutes((double)activity.Duration); Console.WriteLine(end_date); DateTime activity_date = newActivity.Date; DateTime activity_end_date = activity_date.AddMinutes((double)newActivity.Duration); if (activity_date < start_date && activity_end_date < start_date) { newActivity.Date = newActivity.Date; newActivity.Duration = newActivity.Duration; } else if (activity_date > start_date && activity_date > end_date) { newActivity.Date = newActivity.Date; newActivity.Duration = newActivity.Duration; } else { ModelState.AddModelError("Date", "Activity conflicts with another activity"); } } if (Request.Form["dur"] == "minutes") { newActivity.Duration = newActivity.Duration; } if (Request.Form["dur"] == "hours") { newActivity.Duration = newActivity.Duration * 60; } if (Request.Form["dur"] == "days") { newActivity.Duration = newActivity.Duration * 1440; } if (ModelState.IsValid) { Activity theActivity = new Activity() { Title = newActivity.Title, Date = newActivity.Date, Time = newActivity.Time, Duration = newActivity.Duration, Description = newActivity.Description, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, UserId = ActiveUser.UserId, }; DbContext.Add(theActivity); DbContext.SaveChanges(); Activity currentActivity = DbContext.Activities.Where(a => a.UserId == ActiveUser.UserId).Last(); int currentActivityId = currentActivity.ActivityId; return(Redirect($"ActivityDetails/{currentActivityId}")); } return(View("NewActivity")); }