public async Task <ActionResult <PostCommentDTO> > GetComment(int commentId) { var comment = await _postComment.GetASpecificComment(commentId); if (comment != null) { return(comment); } return(BadRequest()); }
/// <summary> /// Gets a specific post from the database /// </summary> /// <param name="postId">The Id of the post</param> /// <returns>A single PostDTO</returns> public async Task <UserPostDTO> GetASpecificPost(int postId) { var post = await _context.UserPosts.Where(x => x.ID == postId) .FirstOrDefaultAsync(); var comments = new List <PostCommentDTO>(); if (post.PostComments != null) { foreach (var item in post.PostComments) { comments.Add(await _postComment.GetASpecificComment(item.CommentId)); } } var images = new List <PostImageDTO>(); if (post.PostImages != null) { foreach (var item in post.PostImages) { images.Add(await _postImage.GetASpecificImage(item.ImageId)); } } var postDTO = new UserPostDTO() { Id = post.ID, UserId = post.UserId, Caption = post.Caption, Created = post.Created, Modified = post.Modified, PostComments = comments, PostImages = images, PostLikes = await GetPostLikes(postId, post.UserId) }; return(postDTO); }
public async Task <IActionResult> DeleteACommentFromPost(int postId, int commentId) { var post = await _userPost.GetASpecificPost(postId); var comment = await _postComment.GetASpecificComment(commentId); var usersRoles = UserClaimsGetters.GetUserRoles(User, _userManager); if (UserClaimsGetters.GetUserId(User) == post.UserId || UserClaimsGetters.GetUserId(User) == comment.UserId || usersRoles.Contains("Admin") || usersRoles.Contains("Owner")) { try { await _userPost.DeleteACommentFromAPost(postId, commentId); return(Ok()); } catch (Exception e) { throw new Exception($"Cannot delete the comment from the post: {e.Message}"); } } throw new Exception("You are not authorized to Delete that Post."); }