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> Delete([FromRoute] long id, string slug)
        {
            Comment comment = await _commentService.FetchCommentByIdAsync(id);

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

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

            if (result.Succeeded)
            {
                if ((await _commentService.DeleteAsync(id)) > 0)
                {
                    return(StatusCodeAndDtoWrapper.BuildSuccess("Comment deleted successfully"));
                }
                else
                {
                    return(StatusCodeAndDtoWrapper.BuildErrorResponse("An error occured, try later"));
                }
            }
            else
            {
                throw new PermissionDeniedException();
            }
        }
        public async Task <IActionResult> GetByAuthor(string name, int page = 1, int pageSize = 5)
        {
            Tuple <int, List <Article> > articles = await _articlesService.GetByAuthorName(name, page, pageSize);

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

            return(new StatusCodeAndDtoWrapper(ArticleListDtoResponse.Build(articles.Item2, "by_author/", page,
                                                                            pageSize, articles.Item1)));
        }
        public async Task <IActionResult> Delete([FromRoute] string id, [FromBody] string articleSlug)
        {
            Comment comment = await _commentService.GetCommentByIdAsync(id);

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

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

            if (result.Succeeded)
            {
                int result2 = await _commentService.DeleteCommentAsync(id);

                return(RedirectToAction("GetArticleBySlug", "Articles", new { slug = articleSlug }));
            }
            else
            {
                throw new PermissionDeniedException();
            }
        }
Example #5
0
        public async Task <IActionResult> Delete(long?id, string slug)
        {
            Product product;

            if (id != null)
            {
                product = await _productsService.FetchById(id.Value);
            }
            else
            {
                product = await _productsService.FetchBySlug(slug);
            }

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

            var result = await _authorizationService.AuthorizeAsync(User, product,
                                                                    _configurationService.GetManageProductPolicyName());

            if (result.Succeeded)
            {
                if ((await _productsService.Delete(product)) > 0)
                {
                    return(StatusCodeAndDtoWrapper.BuildSuccess("Product deleted successfully"));
                }
                else
                {
                    return(StatusCodeAndDtoWrapper.BuildErrorResponse("An error occured, try later"));
                }
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildUnauthorized("Access denied"));
            }
        }