Exemplo n.º 1
0
        public async Task <Suggestion> Handle(VoteToASuggestionCommand voteToASuggestionCommand, CancellationToken cancellationToken = default)
        {
            var validationContext = new VoteToASuggestionValidationContext(voteToASuggestionCommand, _repository);

            _voteValidator.Validate(validationContext);

            var suggestion           = _repository.Get(voteToASuggestionCommand.SuggestionId);
            var suggestionVote       = _repository.GetUserVoteOrDefault(suggestion, voteToASuggestionCommand.UserId);
            var suggestionVoteExists = suggestionVote != null;

            if (suggestionVoteExists)
            {
                _repository.RemoveVote(suggestion, suggestionVote);
            }
            else
            {
                suggestionVote = _mapper.Map <SuggestionVote>(voteToASuggestionCommand);

                _repository.AddVote(suggestion, suggestionVote);
            }

            await _dbContext.SaveChangesAsync();

            return(suggestion);
        }