public async Task <IActionResult> AddComment(string userId, string postId, [FromForm] PostWithCommentToAdd postComment) { try { if (string.IsNullOrWhiteSpace(userId) || postComment == null) { return(NotFound()); } PostComment postCommentToAdd = new PostComment { WhenAdded = DateTime.Now, FromWho = Guid.Parse(_userId), PostId = Guid.Parse(postId), Text = postComment.Text }; await _postApiAccess.AddPostComment(userId, postCommentToAdd); return(RedirectToAction(nameof(ShowComments), new { userId, postId })); } catch (Exception ex) { _logger.LogError($"Something went wrong during adding comment on post: {postId} by user {_userId}"); _logger.LogError($"Exception info: {ex.Message} {ex.Source}"); return(RedirectToAction("Error", "Error")); } }
public async Task <ActionResult <PostWithCommentToAdd> > ShowComments(string userId, string postId) { try { if (string.IsNullOrWhiteSpace(userId) || string.IsNullOrWhiteSpace(postId)) { return(NotFound()); } Post post = await _postApiAccess.GetPost(userId, postId); if (post == null) { return(NotFound()); } ViewData["currentUserProfileId"] = userId.ToString(); ViewData["loggedUserId"] = _userId.ToString(); List <BasicUserData> user = new List <BasicUserData>(); foreach (PostComment userPost in post.PostComments) { user.Add(_mapper.Map <BasicUserData>(await _userApiAccess.GetUser(userPost.FromWho.ToString()))); } PostWithCommentToAdd postToReturn = new PostWithCommentToAdd { Post = post, Text = string.Empty, Users = user, User = _mapper.Map <BasicUserData>(await _userApiAccess.GetUser(userId)) }; return(View(postToReturn)); } catch (Exception ex) { _logger.LogError($"Something went wrong during loading comments on post: {postId} by user {_userId}"); _logger.LogError($"Exception info: {ex.Message} {ex.Source}"); return(RedirectToAction("Error", "Error")); } }