public ActionResult Profiles(String id, ProfileViewModel model) { var userName = System.Web.HttpContext.Current.User.Identity.Name; if (userName.Length == 0) { return Redirect(Request.Url.ToString()); } User loggedInUser = _userRepository.All.First(u => u.UserName == userName); User profileOwner = _userRepository.All.First(u => u.UserName == id); var isFollowing = loggedInUser.Following.Select(f => f.Followed.UserName).ToList().Contains(profileOwner.UserName); if (isFollowing) { var relationship = _userRelationshipRepository.All.First(u => u.Follower.UserName == userName && u.Followed.UserName == id); _userRelationshipRepository.Delete(relationship); _userRelationshipRepository.Save(); } else { var newRelationship = new UserRelationship { Follower = loggedInUser, Followed = profileOwner }; _userRelationshipRepository.Add(newRelationship); _userRelationshipRepository.Save(); } return Redirect(Request.Url.ToString()); }
public ActionResult Feed(ProfileViewModel model) { if (ModelState.IsValid) { var userName = System.Web.HttpContext.Current.User.Identity.Name; User user = _userRepository.All.First(u => u.UserName == userName); var post = new Post { User = user, Content = model.Tweet, CreatedAt = DateTime.UtcNow }; _postRepository.Add(post); _postRepository.Save(); return Redirect(Request.Url.ToString()); } // If we got this far, something failed, redisplay form return View(model); }
// GET: Profile/erikhirmo public ActionResult Profiles(String id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } User user = _userRepository.All.First(u => u.UserName == id); if (user == null) { return HttpNotFound(); } var userName = System.Web.HttpContext.Current.User.Identity.Name; Boolean isFollowing = false; Boolean ShowFollowButton = false; if (userName.Length > 0) { User loggedInUser = _userRepository.All.First(u => u.UserName == userName); isFollowing = loggedInUser.Following.Select(f => f.Followed.UserName).ToList().Contains(user.UserName); ShowFollowButton = userName != user.UserName; } var vm = new ProfileViewModel { User = user, Posts = user.Posts.ToList(), NumberOfTwoots = user.Posts.Count(), NumberOfFollowers = user.Followers.Count(), NumberOfFollowing = user.Following.Count(), ShowInput = false, ShowFollowButton = ShowFollowButton, Following = isFollowing }; return View(vm); }
public ActionResult Feed() { var userName = System.Web.HttpContext.Current.User.Identity.Name; User user = _userRepository.All.First(u => u.UserName == userName); var posts = new List<Post>(); user.Following.ForEach(f => f.Followed.Posts.ForEach(p => posts.Add(p))); user.Posts.ForEach(p => posts.Add(p)); posts = posts.OrderByDescending(o => o.CreatedAt).ToList(); var vm = new ProfileViewModel { User = user, Posts = posts, NumberOfTwoots = user.Posts.Count, NumberOfFollowers = user.Followers.Count(), NumberOfFollowing = user.Following.Count(), ShowInput = true, ShowFollowButton = false }; return View(vm); }