コード例 #1
0
        public async Task <ActionResult <PostCommentDto> > AddComment(string userId, string postId, [FromBody] PostCommentToAddDto postComment)
        {
            if (Guid.TryParse(postId, out Guid gPostId) && Guid.TryParse(userId, out Guid gUserId))
            {
                try
                {
                    if (_postService.CheckIfPostExists(gPostId) && await _userService.CheckIfUserExists(gUserId))
                    {
                        PostCommentDto addedComment = await _postCommentService.AddCommentAsync(gPostId, postComment);

                        return(CreatedAtRoute("GetComment",
                                              new
                        {
                            userId,
                            postId = addedComment.PostId,
                            commentId = addedComment.Id
                        },
                                              addedComment));
                    }
                    else
                    {
                        return(NotFound($"User: {userId} or post {postId} not found."));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error occured during adding the comment. Post id: {postId}", postId);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            else
            {
                return(BadRequest($"{userId} or {postId} is not valid guid."));
            }
        }