Exemplo n.º 1
0
        public async Task <PostCommentDto> AddCommentAsync(Guid postId, PostCommentToAddDto postComment)
        {
            _logger.LogDebug("Trying to add comment: {postComment} to post.", postComment);
            if (postComment is null)
            {
                throw new ArgumentNullException(nameof(postComment));
            }
            if (postId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(postId));
            }
            try
            {
                PostComment commentToAdd = _mapper.Map <PostComment>(postComment);
                commentToAdd.PostId = postId;
                PostComment addedComment = await _postCommentRepository.AddAsync(commentToAdd);

                await _postCommentRepository.SaveAsync();

                return(_mapper.Map <PostCommentDto>(addedComment));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error occured during adding comment to the post.");
                throw;
            }
        }
 public PostCommentsControllerAddCommentTests() : base()
 {
     _commentToAdd = _fixture.Create <PostCommentToAddDto>();
 }
Exemplo n.º 3
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."));
            }
        }