Ejemplo n.º 1
0
        public async Task <IActionResult> Vote([FromRoute] string commentId, [FromBody] CommentVoteDto commentVoteDto)
        {
            string userId = _httpContext.User.FindFirstValue("sub");

            await _mediator.Send(new VoteCommentCommand(userId, commentId, commentVoteDto));

            return(Ok());
        }
Ejemplo n.º 2
0
        public ActionResult AddCommentVote(CommentVoteDto commentVoteDto)
        {
            try
            {
                commentService.UpdateCommentVotes(commentVoteDto.CommentId, commentVoteDto.Flag);

                var author = authorService.GetAuthorByCommentId(commentVoteDto.CommentId);

                //authorService.UpdateAuthorPostVotes(author, commentVoteDto.Flag);

                authorService.UpdateAuthorVotes(author.Id);

                return(Ok(new { commentId = commentVoteDto.CommentId }));
            }
            catch
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 3
0
        public async Task VoteCommentAsync(string commentId, string userId, CommentVoteDto commentVoteDto)
        {
            try
            {
                User user = await _userManager.FindByIdAsync(userId);

                Guard.Against.NullItem(user, nameof(user));

                Comment comment = await _commentRepository.GetByConditionAsync(x => x.Id == commentId);

                Guard.Against.NullItem(comment, nameof(comment));

                var commentVote = await _voteRepository.GetEntityVoteAsync <CommentVote>(
                    x => (x.CommentId == comment.Id) && (x.UserId == user.Id),
                    x => x
                    );

                if (commentVote != null)
                {
                    commentVote.Direction =
                        commentVote.Direction == commentVoteDto.Direction ? 0 : commentVoteDto.Direction;

                    await _voteRepository.UpdateEntityVoteAsync(commentVote);
                }
                else
                {
                    var newCommentVote = new CommentVote
                    {
                        CommentId = comment.Id,
                        UserId    = user.Id,
                        Direction = commentVoteDto.Direction,
                    };

                    await _voteRepository.VoteEntityAsync(newCommentVote);
                }
            }
            catch (ValidationException e)
            {
                throw new HttpStatusCodeException(HttpStatusCode.UnprocessableEntity, e.Message);
            }
        }
Ejemplo n.º 4
0
 public VoteCommentCommand(string userId, string commentId, CommentVoteDto commentVoteDto)
 {
     UserId         = userId;
     CommentId      = commentId;
     CommentVoteDto = commentVoteDto;
 }