示例#1
0
 public IActionResult Index(UserModelView model)
 {
     if(ModelState.IsValid)
     {
         User loginuser = _context.Users.SingleOrDefault(user => user.Username == model.Username);
         if (loginuser != null)
         {
             ViewBag.RegDup = "This e-mail already belongs to a registered user";
             return View("Index");
         }
         else
         {
             User newUser = new User 
             {
                 FirstName = model.FirstName,
                 LastName = model.LastName,
                 Wallet = 1000,
                 Username = model.Username,
                 Created_at = DateTime.Now,
                 Updated_at = DateTime.Now,
             };
             PasswordHasher<User> hasher = new PasswordHasher<User>();
             newUser.Password = hasher.HashPassword(newUser, model.Password);
             _context.Add(newUser);
             _context.SaveChanges();
             HttpContext.Session.SetInt32("currentUserId", newUser.UserId);
             HttpContext.Session.SetString("currentUserName", newUser.FirstName);
             return RedirectToAction("Home");
             }
     }
     else
     {
         return View("Index");
     }
 }
        public IActionResult RegisterUser(User user)
        {
            if (ModelState.IsValid)
            {
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    ModelState.AddModelError("Email", "Email already in use!");
                    return(View("Index"));
                }
                else
                {
                    PasswordHasher <User> Hasher = new PasswordHasher <User>();
                    user.Password = Hasher.HashPassword(user, user.Password);

                    dbContext.Add(user);
                    dbContext.SaveChanges();
                    var AddedUser = dbContext.Users.FirstOrDefault(u => u.Email == user.Email);
                    HttpContext.Session.SetInt32("UserId", AddedUser.UserId);
                    int?LoggedInUserId = HttpContext.Session.GetInt32("UserId");

                    return(RedirectToAction("ShowHome", "UserAndActivity"));
                }
            }
            else
            {
                return(View("Index"));
            }
        }
示例#3
0
        public IActionResult CreateActivity(Activity NewActivity, double Hour, double Minute, string ampm, string FirstName, string LastName)
        {
            if (ModelState.IsValid)
            {
                NewActivity.CreatorName = FirstName + " " + LastName;

                if (ampm == "am")
                {
                    Hour += 12;
                }
                NewActivity.Date = NewActivity.Date.AddHours(Hour).AddMinutes(Minute);
                if (NewActivity.Date < DateTime.Now)
                {
                    ModelState.AddModelError("Date", "Date Must be after today");
                    return(View("ShowCreateActivity"));
                }
                dbContext.Add(NewActivity);
                dbContext.SaveChanges();
                return(RedirectToAction("ShowHome", "UserAndActivity"));
            }
            int?LoggedInUserId = HttpContext.Session.GetInt32("UserId");

            ViewBag.LoggedInUserId = LoggedInUserId;
            var LoggedInUser = dbContext.Users.FirstOrDefault(u => u.UserId == LoggedInUserId);

            ViewBag.LoggedInUser = LoggedInUser;


            return(View("ShowCreateActivity"));
        }
示例#4
0
        public IActionResult Like(int id)
        {
            if (HttpContext.Session.GetInt32("UserId") == null)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                List <Liker> validLike = dbContext.likers.Where(l => (l.IdeaId == id) && (l.UserId == HttpContext.Session.GetInt32("UserId"))).ToList();


                Liker newLiker = new Liker();
                User  user     = dbContext.users.SingleOrDefault(u => u.UserId == (int)HttpContext.Session.GetInt32("UserId"));
                Idea  idea     = dbContext.ideas.SingleOrDefault(i => i.IdeaId == id);
                newLiker.Idea = idea;
                newLiker.User = user;
                dbContext.Add(newLiker);
                dbContext.SaveChanges();



                return(Redirect("/bright_ideas"));
            }
        }
示例#5
0
 public IActionResult CreateUserAndActivity(UserAndActivity NewUserAndActivity)
 {
     dbContext.Add(NewUserAndActivity);
     dbContext.SaveChanges();
     return(RedirectToAction("ShowHome"));
 }