public async Task <IActionResult> CommentOnPost(ReturnPostVM forComment) { var user = await _userManager.GetUserAsync(HttpContext.User); if (user == null) { return(RedirectToAction("Login", "Account")); } else { if (!ModelState.IsValid) { var returnedPost = await CreatePostVM(forComment.PostId); return(RedirectToAction("Index", new { returnedPost.PostId })); } var post = await _postService.GetPost(forComment.PostId); forComment.addComment.UserFullName = user.FirstName + " " + user.LastName; forComment.addComment.Post = post; forComment.addComment.ImagePath = user.ProfilePhoto; var comment = PostMappers.MapCommentVMToComment(forComment.addComment); await _commentService.AddComment(comment); var returnPost = await CreatePostVM(forComment.PostId); return(RedirectToAction("Index", new { returnPost.PostId })); } }
/// <summary> /// Creates a returnPost view model from a single post entity and a list of posts /// </summary> /// <param name="post"></param> /// <param name="recentPosts"></param> /// <returns></returns> public static ReturnPostVM ReturnPosts(Post post, List <Post> recentPosts) { ReturnPostVM returnPost = new ReturnPostVM { PostTitle = post.PostTitle, PostCategories = string.Join(" ,", ReturnCategories(post.PostCategories)), PostCreator = post.PostCreator.FirstName + " " + post.PostCreator.LastName, PostDetails = post.PostDetails, ImagePath = post.ImagePath, commentsForThisPost = ReturnComments(post.Comments), CommentCount = post.Comments.Count, Likes = post.Likes.Count, PostId = post.PostId, MostRecentPosts = ReturnRecentPosts(recentPosts), CreationDate = post.CreationDate }; return(returnPost); }
public async Task <IActionResult> LikePost(ReturnPostVM postToLike) { var user = await _userManager.GetUserAsync(HttpContext.User); if (user == null) { return(RedirectToAction("Login", "Account")); } else { if (!ModelState.IsValid) { var returnedPost = await CreatePostVM(postToLike.PostId); return(RedirectToAction("Index", new { returnedPost.PostId })); } var allLikes = await _likeService.GetAllLikes(); var checkNonRepeat = allLikes.Any(like => like.LikedPost.PostId == postToLike.PostId && like.UserWhoLiked == user); if (checkNonRepeat) { var returnPost = await CreatePostVM(postToLike.PostId); return(View("Index", returnPost)); } else { var post = await _postService.GetPost(postToLike.PostId); var Like = PostMappers.AddLike(post, user); //Change to getuser method and remove Blogit.models namespace. await _likeService.LikePost(Like); var returnPost = await CreatePostVM(postToLike.PostId); return(RedirectToAction("Index", new { returnPost.PostId })); } } }