コード例 #1
0
        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();
            }
        }
コード例 #2
0
        public async Task <IActionResult> CreateOrder([FromBody] CreateOrderDto form)
        {
            var order = await _orderService.Create(form, await _usersService.GetCurrentUserAsync());

            if (order != null)
            {
                return(StatusCodeAndDtoWrapper.BuildGeneric(OrderDetailsDto.Build(order)));
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse("Something went wrong"));
            }
        }
コード例 #3
0
        public async Task <object> Login([FromBody] LoginDtoRequest loginDto)
        {
            // Sign in the user, don't persis cookies, don't lockout on failure
            var result = await _signInManager.PasswordSignInAsync(loginDto.UserName, loginDto.Password,
                                                                  false, false);

            if (result.Succeeded)
            {
                var user = await _usersService.GetByUserNameAsync(loginDto.UserName);

                return(await GenerateJwtToken(user));
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse("Invalid credentials"));
            }
        }
コード例 #4
0
        public async Task <IActionResult> DeleteLike(string slug)
        {
            string userId = _usersService.GetCurrentUserId();

            Like like = await _likesService.GetLikeByArticleSlugNoInclude(userId, slug);

            // if (!(await _likesService.HasUserLikedArticleBySlug(slug, userId)))
            if (like == null)
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse("You are not liking this article"));
            }

            if (await _likesService.DeleteLikeByArticleSlug(userId, slug))
            {
                return(StatusCodeAndDtoWrapper.BuildSuccess("Article like removed successfully"));
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse("Something went wrong"));
            }
        }
コード例 #5
0
        public async Task <IActionResult> Unfollow(string username)
        {
            string       followerId  = _usersService.GetCurrentUserId();
            string       followingId = (await _usersService.GetByUserNameAsync(username)).Id;
            UserRelation ur          = await _userSubscriptions.GetUserSubscription(followingId, followerId);

            if (ur != null)
            {
                if (await _userSubscriptions.DeleteUserSubscription(ur))
                {
                    return(StatusCodeAndDtoWrapper.BuildSuccess($"Subscription deleted successfully"));
                }
                else
                {
                    return(StatusCodeAndDtoWrapper.BuildErrorResponse($"Something went wrong"));
                }
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse($"You are not subscribed to {username}"));
            }
        }
コード例 #6
0
        public async Task <IActionResult> CreateLike(string slug)
        {
            string userId = _usersService.GetCurrentUserId();
            bool   liked  = await _likesService.HasUserLikedArticleBySlug(slug, userId);

            Like like = await _likesService.GetLikeByArticleSlugNoInclude(userId, slug);

            if (like != null)
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse("You have already liked this article"));
            }

            ;

            if (await _likesService.CreateLikeByArticleSlug(userId, slug))
            {
                return(StatusCodeAndDtoWrapper.BuildSuccess("Article liked successfully"));
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse("You have already liked this article"));
            }
        }
コード例 #7
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"));
            }
        }
コード例 #8
0
        public async Task <IActionResult> Follow(string username)
        {
            // TODO: You should not trust the CurrentUserId, because it reads it from the valid jwt
            // You have to call GetCurrentUserAsync()?.Id because if user does not exist Get..Async() will return null
            string          followerId = _usersService.GetCurrentUserId();
            ApplicationUser following  = await _usersService.GetByUserNameAsync(username);

            if (_usersService.GetCurrentUserName() == username)
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse("You can not follow yourself"));
            }

            if (!(await _usersService.IsAuthor(following)))
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse("You can not follow a non-author user"));
            }

            string       followingId = following.Id;
            UserRelation ur          = await _userSubscriptions.GetUserSubscription(followingId, followerId);

            if (ur == null)
            {
                if (await _userSubscriptions.CreateUserRelation(followingId, followerId))
                {
                    return(StatusCodeAndDtoWrapper.BuildSuccess($"Now you are following {username}"));
                }
                else
                {
                    return(StatusCodeAndDtoWrapper.BuildErrorResponse($"Something went wrong"));
                }
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildErrorResponse($"You are already following {username}"));
            }
        }