コード例 #1
0
        public async Task <Response> Create(string postId, string commentId, CreateResponseDto dto)
        {
            User user = await _sessionService.GetUser();

            Validate("create", dto.Content, user);

            Comment comment = await _commentRepository.GetById(commentId);

            if (comment == null)
            {
                _logger.LogWarning($"There is no comment {postId}");
                throw HttpError.NotFound($"There is no comment {postId}");
            }

            if (comment.PostId != postId)
            {
                _logger.LogWarning($"Comment {commentId} does not belong to post {postId}");
                throw HttpError.BadRequest("Comment does not belong to post");
            }

            var response = new Response
            {
                Content        = dto.Content,
                CreationTime   = DateTime.Now,
                LastUpdateTime = DateTime.Now,
                AuthorId       = user.Id,
                Likes          = 0,
                Dislikes       = 0,
                CommentId      = commentId,
                Id             = Guid.NewGuid().ToString()
            };

            await _responseRepository.Create(response);

            _logger.LogInformation($"Response {response.Id} has been created");

            return(response);
        }
コード例 #2
0
        public async Task <IActionResult> Create([FromRoute] string postId, string commentId, [FromBody] CreateResponseDto dto)
        {
            Response response = await _responseService.Create(postId, commentId, dto);

            return(CreatedAtAction(nameof(Get), new { postId, commentId, id = response.Id }, response));
        }