[AllowAnonymous] // This needs to allow anonymous because the user accessing this page won't have an account yet. public async Task <IActionResult> Create(CreateModel model) { // Make controller base class do security check on itself, and validate account creation fields. if (ModelState.IsValid && ValidateCreateModel(model)) { IdentityUser user = new IdentityUser // Satisfy both UserName and Email field of IdentityUser with the provided email. { UserName = model.Email, Email = model.Email }; // Attempt to create account and get hold onto result. IdentityResult result = await userManager.CreateAsync(user, model.Password); if (result.Succeeded) // Account creation succesful. { // Retrieve the user that was just created. IdentityUser id = await userManager.FindByEmailAsync(model.Email); // Create Profile instance with primary key ID of IdentityUser for password, provided name, and default values. // The user's Profile is retrieved using the Id of their IdentityUser. Profile profile = new Profile { Password = id.Id, // The user will not enter this number when logging in, this will get passed behind the scenes. FirstName = FirstLetterToUpper(model.FirstName), // provideded LastName = FirstLetterToUpper(model.LastName), // provideded Bio = "", // default ProfilePicture = 0, // default DateTime = DateTime.UtcNow // default }; // Add profile to the database. profileRepo.SaveProfile(profile); // Sign them in with the profile that was just added to the database. currentProfile.SetProfile(profileRepo.ById(profile.ProfileId)); // Return home page. return(RedirectToAction("Index", "Home")); } else // Account creation failed. { // Transfer errors from account creation results to controller model state so they can be displayed on error page. foreach (IdentityError error in result.Errors) { ModelState.AddModelError("", error.Description); } } } // If controller model state or credentials were invalid, send back provided account details and or account creation result errors. return(View(model)); }
/* * Returns home page to user. */ public IActionResult Index() { // If someone is logged in, bring them home. if (currentProfile.profile == null) { return(RedirectToAction("Login", "Account")); } currentProfile.SetProfile(profileRepo.ById(currentProfile.profile.ProfileId)); // Refreshes current profile // Returns home page to user with their ProfileID attached. return(View(currentProfile.id)); }
public void DeleteImage(int imageId) { // Get a handle on the image record by ImageID provided. Models.Image image = imageRepo.ById(imageId); // Validate image ownership. if (image.ProfileId == currentProfile.id) { // Burrow into the nested dependencies and delete them on the way out. // Pattern: (1)prep list, (2)fill list, (3)loop list, (4)repeat pattern on dependencies, (5)delete record. List <Post> posts = new List <Post>(); // (1)Prep list. foreach (Post p in postRepo.Posts.Where(p => p.ImageId == imageId)) { posts.Add(p); } // (2)Fill list. foreach (Post p in posts) // (3)Loop list. { // (4)Repeat pattern on dependencies. List <Comment> comments = new List <Comment>(); foreach (Comment c in commentRepo.ByPostId(imageId)) { comments.Add(c); } foreach (Comment c in comments) { List <Like> commentLikes = new List <Like>(); foreach (Like l in likeRepo.ByTypeAndId(2, c.CommentId)) { commentLikes.Add(l); } foreach (Like l in commentLikes) { likeRepo.DeleteLike(l); } commentRepo.DeleteComment(c); } List <Like> postLikes = new List <Like>(); foreach (Like l in likeRepo.ByTypeAndId(1, p.PostId)) { postLikes.Add(l); } foreach (Like l in postLikes) { likeRepo.DeleteLike(l); } // (5)Delete record. postRepo.DeletePost(p); } // If the user is using the image to be deleted as a profile picture, give them their default profile picture. if (currentProfile.profile.ProfilePicture == imageId) { // Get profile by current user's ProfileID. // (By doing this, instead of using currentUser.profile, it gaurentees that this is the latest version of the profile.) Profile profile = profileRepo.ById(currentProfile.id); profile.ProfilePicture = 0; // Give default profile picture ImageID. profileRepo.SaveProfile(profile); // Commit profile changes. currentProfile.SetProfile(profile); // Update CurrentProfile in session. } // Remove fullsize and thumbnail images from file system. DeleteFromFileSystem(image.Name, env.WebRootPath); // Delete image record from database. imageRepo.DeleteImage(image); } }