public async Task <IActionResult> Update([FromBody] CreateOrEditCommentDto dto, [FromRoute] long id, long slug)
        {
            // we need to load the user in order to let the Handler check it agains the owner if needed
            Comment comment = await _commentService.FetchCommentByIdAsync(id, includeUser : true);

            if (comment == null)
            {
                return(StatusCodeAndDtoWrapper.BuildGenericNotFound());
            }

            var result = await _authorizationService.AuthorizeAsync(User, comment,
                                                                    _configService.GetDeleteCommentPolicyName());

            if (result.Succeeded)
            {
                int result2 = await _commentService.UpdateAsync(comment, dto);

                return(StatusCodeAndDtoWrapper.BuildSuccess(CommentDetailsDto.Build(comment),
                                                            "Comment updated successfully"));
                //return StatusCodeAndDtoWrapper.BuildSuccess("Comment updated successfully");
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildUnauthorized("Permission denied"));
            }
        }
        public async Task <IActionResult> Create(string slug, [FromBody] CreateOrEditCommentDto model)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCodeAndDtoWrapper.BuilBadRequest(ModelState));
            }

            long            userId = Convert.ToInt64(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            ApplicationUser user   = await _usersService.GetCurrentUserAsync();

            Comment comment = await _commentService.CreateAsync(user, slug, model, userId);

            return(StatusCodeAndDtoWrapper.BuildSuccess(CommentDetailsDto.Build(comment)));
        }
        public async Task <IActionResult> Create(string slug, [FromBody] CreateOrEditCommentDto model)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCodeAndDtoWrapper.BuilBadRequest(ModelState));
            }

            string          userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
            ApplicationUser user   = await _usersService.GetCurrentUserAsync();

            Comment comment = await _commentService.CreateComment(user, slug, model.Content, userId);

            // return RedirectToAction("GetArticleBySlug", "Articles", new {slug = slug});
            return(StatusCodeAndDtoWrapper.BuildSuccess(CommentDetailsDto.Build(comment)));
        }
        public async Task <IActionResult> GetDetails(string slug, long commentId, CreateOrEditCommentDto model)
        {
            Comment comment = await _commentService.FetchCommentByIdAsync(commentId);

            return(StatusCodeAndDtoWrapper.BuildSuccess(CommentDetailsDto.Build(comment)));
        }
Beispiel #5
0
 public async Task <int> UpdateAsync(Comment comment, CreateOrEditCommentDto dto)
 {
     comment.Content = _htmlEncoder.Encode(dto.Content);
     comment.Rating  = dto.Rating;
     return(await _context.SaveChangesAsync());
 }
Beispiel #6
0
        public async Task <Comment> CreateAsync(ApplicationUser user, string productSlug, CreateOrEditCommentDto dto,
                                                long userId)
        {
            var product = await _productsService.FetchBySlug(productSlug);

            var comment = new Comment()
            {
                Product   = product,
                ProductId = product.Id,
                Content   = _htmlEncoder.Encode(dto.Content),
                Rating    = dto.Rating,
                UserId    = userId,
                User      = user,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };


            await _context.Comments.AddAsync(comment);

            product.Comments.Add(comment);
            await _context.SaveChangesAsync();

            return(comment);
        }