Esempio n. 1
0
        public IActionResult CreateComment(long id, [FromBody] CreateCommentRequestDto body)
        {
            // Validate
            var validation = body.Validate();

            if (validation != null)
            {
                return(BadRequest(validation));
            }

            // Authorize user
            long authorId = body.author;
            long tokenId  = int.Parse(User.Claims.First(x => x.Type == "uid").Value);

            if (authorId != tokenId)
            {
                return new ObjectResult(new ForbiddenDto())
                       {
                           StatusCode = 403
                       }
            }
            ;

            // Check is user with this id exists in db
            if (!_userService.IsUserExists(authorId))
            {
                return(BadRequest(new ValidateErrorDto(new ValidateErrorElement(
                                                           "author", "NotFound", "user with this id not found"
                                                           ))));
            }

            // check is review with this id exists in db
            if (!_reviewService.Exists(body.review))
            {
                return(BadRequest(new ValidateErrorDto(new ValidateErrorElement(
                                                           "review", "NotFound", "review with this id not found"
                                                           ))));
            }

            _reviewService.WriteComment(id, body.ToDomain());

            return(Ok(new EmptyOkDto()));
        }
    }
Esempio n. 2
0
        public async Task <IActionResult> Create(Guid postId, CreateCommentRequestDto createRequestDto)
        {
            var request = _mapper.Map <CreateCommentRequest>(createRequestDto);

            request.OwnerUsername = User.Identity.Name;
            request.PostId        = postId;

            var response = await _comments.CreateAsync(request);

            if (!response.Success)
            {
                return(BadRequest(response.ErrorMessage));
            }
            var comment = response.Result;

            var readDto = _mapper.Map <CommentReadDto>(comment);

            return(CreatedAtAction(nameof(Get), new { id = comment.Id }, readDto));
        }
Esempio n. 3
0
        public async Task <IActionResult> CreateCommentAsync(
            [FromBody] CreateCommentRequestDto createCommentRequestDto,
            string postId,
            [FromRoute] string commentId = null,
            CancellationToken token      = default(CancellationToken))
        {
            var createCommentBo = this._autoMapperService.Map <CreateCommentRequestBo>(createCommentRequestDto);

            var serviceResult = await this._commentService.CreateCommentAsync(postId, commentId, createCommentBo, token);

            if (serviceResult.HasErrors)
            {
                return(new BadRequestObjectResult(serviceResult.Errors));
            }

            var mappedResult = this._autoMapperService.Map <CommentResponseDto>(serviceResult.Value);

            return(this.CreatedAtRoute(nameof(this.GetCommentsAsync), new { postId = postId, commentId = mappedResult.Data.Id }, mappedResult));
        }
        public async Task <IActionResult> CreateCommentAsync(string postId, string commentId, [FromBody] CreateCommentRequestDto createCommentRequestDto)
        {
            var createCommentBo = this._mapper.Map <CreateCommentRequestBo>(createCommentRequestDto);

            var serviceResult = await this._commentService.CreateCommentAsync(postId, commentId, createCommentBo);

            if (serviceResult.HasErrors)
            {
                return(new BadRequestObjectResult(serviceResult.Errors));
            }

            return(new OkObjectResult(this._mapper.Map <CommentItemDto>(serviceResult.Value)));
        }