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()); }
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()); } }
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); } }
public VoteCommentCommand(string userId, string commentId, CommentVoteDto commentVoteDto) { UserId = userId; CommentId = commentId; CommentVoteDto = commentVoteDto; }