Exemple #1
0
        public IActionResult Create(Post newPost)
        {
            if (ModelState.IsValid == false)
            {
                return(View("New"));
            }

            // ModelState IS valid
            newPost.UserId = (int)uid;
            db.Posts.Add(newPost);

            db.SaveChanges();

            return(RedirectToAction("Dashboard"));
        }
        public IActionResult Register(User newUser)
        {
            if (ModelState.IsValid)
            {
                if (db.Users.Any(u => u.Email == newUser.Email))
                {
                    ModelState.AddModelError("Email", "is taken");
                }
            }

            // in case any above custom errors were added
            if (ModelState.IsValid == false)
            {
                // so error messages will be displayed
                return(View("Index"));
            }

            // hash pw
            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("Dashboard", "Posts"));
        }
        public IActionResult Create(ForumPost newForumPost)
        {
            if (ModelState.IsValid == false)
            {
                // send back to the page with the form so error messages are displayed
                return(View("FamForum"));
            }

            // ModelState IS valid
            newForumPost.UserId = (int)uid;
            db.ForumPosts.Add(newForumPost);

            db.SaveChanges();


            return(RedirectToAction("FamForum"));
        }
Exemple #4
0
        public IActionResult Create(ProfilePost newProfilePost, int userProfileId)
        {
            if (ModelState.IsValid == false)
            {
                User selectedProfileId = db.Users
                                         .Include(user => user.ProfilePosts)
                                         .ThenInclude(profpost => profpost.Author)
                                         .FirstOrDefault(user => user.UserId == userProfileId);

                // send back to the page with the form so error messages are displayed
                return(View("Profile", selectedProfileId));
            }

            db.ProfilePosts.Add(newProfilePost);

            db.SaveChanges();


            return(RedirectToAction("Profile", new { id = newProfilePost.UserProfileId }));
        }