public async Task <IActionResult> MyPosts(string userId) { ViewBag.ActivePage = "MyPosts"; if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Home")); } currentUser = await MainUtility.FindAnActiveUser(_userManager, User.Identity.Name); CustomUser customUserFromDb = await _userManager.FindByIdAsync(userId); if (currentUser == customUserFromDb) { ViewBag.AuthorOfPosts = 1; } ViewBag.CurrentUser = currentUser.Id; MyPostsVM myPostsVM = new MyPostsVM { Posts = customUserFromDb.Posts.Where(po => po.IsDeleted == false).OrderByDescending(p => p.UpdatedAtTime > p.CreatedAtTime ? p.UpdatedAtTime : p.CreatedAtTime) }; return(View(myPostsVM)); }
public async Task <IActionResult> CreatePost(MyPostsVM myPostsVM) { currentUser = await MainUtility.FindAnActiveUser(_userManager, User.Identity.Name); if (!ModelState.IsValid) { return(RedirectToAction("MyPosts", "Profile", new { userId = currentUser.Id })); } Post post = new Post { PostContent = myPostsVM.Post.PostContent.Trim(), CreatedAtTime = DateTime.Now, CustomUserId = currentUser.Id }; _context.Posts.Add(post); await _context.SaveChangesAsync(); return(RedirectToAction("MyPosts", "Profile", new { userId = currentUser.Id })); }