public IActionResult Registering(Register model)
 {
     if (ModelState.IsValid)
     {
         if (dbContext.users.Any(u => u.Email == model.Email))
         {
             ModelState.AddModelError("Email", "This email is already in use");
             return(View("LoginReg"));
         }
         Users new_user = new Users
         {
             Email     = model.Email,
             FirstName = model.FirstName,
             LastName  = model.LastName,
         };
         PasswordHasher <Users> Hasher = new PasswordHasher <Users>();
         new_user.Password = Hasher.HashPassword(new_user, model.Password);
         dbContext.Add(new_user);
         dbContext.SaveChanges();
         Users user = dbContext.users.FirstOrDefault(u => u.Email == new_user.Email);
         HttpContext.Session.SetInt32("UserId", user.UsersId);
         return(RedirectToAction("Dashboard", "Home"));
     }
     return(View("LoginReg"));
 }
示例#2
0
 public IActionResult Create(NewActivity model)
 {
     if (HttpContext.Session.GetInt32("UserId") != null)
     {
         if (dbContext.users.Any(u => u.UsersId == (int)HttpContext.Session.GetInt32("UserId")))
         {
             if (ModelState.IsValid)
             {
                 if (DateTime.Compare(model.Date.Date.Add(model.Time.TimeOfDay), DateTime.Now) > 0)
                 {
                     if (!dbContext.activities.Any(a => a.Title == model.Title && a.CreatorId == (int)HttpContext.Session.GetInt32("UserID") && a.DateTime == model.Date.Date.Add(model.Time.TimeOfDay)))
                     {
                         Activities new_activity = new Activities
                         {
                             Title        = model.Title,
                             DateTime     = model.Date.Date.Add(model.Time.TimeOfDay),
                             Duration     = model.Duration,
                             DurationUnit = model.DurationUnit,
                             Description  = model.Description,
                             CreatorId    = (int)HttpContext.Session.GetInt32("UserId"),
                         };
                         dbContext.Add(new_activity);
                         dbContext.SaveChanges();
                         int activityid = dbContext.activities.FirstOrDefault(a => a.Title == model.Title).ActivitiesId;
                         return(RedirectToAction("Activity", new { activity_id = activityid }));
                     }
                     ModelState.AddModelError("Title", "This activity already exists");
                 }
                 ModelState.AddModelError("Date", "This date and time have already passed.");
             }
             return(View("New", model));
         }
         return(RedirectToAction("Logout", "LoginReg"));
     }
     return(RedirectToAction("LoginReg", "LoginReg"));
 }