public async Task <IActionResult> Create([FromBody] CreateCommentRequest commentRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var existedPost = await _postsRepository.GetPostByIdAsync(commentRequest.PostId.Value); if (existedPost == null) { return(BadRequest(new { error = $"Post with Id {commentRequest.PostId} does not exist" })); } var comment = _mapper.Map <Comment>(commentRequest); comment.UserId = HttpContext.GetUserId(); var created = await _commentsRepository.CreateCommentAsync(comment); if (created) { var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}"; var locationUri = baseUrl + "/" + ApiRoutes.Comments.Get.Replace("{commentId}", comment.Id.ToString()); return(Created(locationUri, _mapper.Map <CommentResponse>(comment))); } return(NotFound()); }
public async Task <IActionResult> CreateComment([FromBody] Comment comment) { _logger.LogInformation("Create comment."); if (!ModelState.IsValid) { _logger.LogWarning("Create comment: model state is not valid."); return(BadRequest(ModelState)); } await _comments.CreateCommentAsync(comment, HttpContext.User); _logger.LogInformation($"Create comment: comment with Id equal {comment.Id} was created."); return(CreatedAtAction("GetFilm", new { id = comment.Id }, comment)); }