Ejemplo 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);
        }
Ejemplo n.º 2
0
        public void Setup()
        {
            var validCommand = new VoteToASuggestionCommand
            {
                UserId       = "any value",
                IsPositive   = true,
                SuggestionId = Guid.NewGuid()
            };

            _repository        = new SuggestionRepository(_dbContext);
            _validationContext = new VoteToASuggestionValidationContext(validCommand, _repository);
        }
 public VoteToASuggestionValidationContext(VoteToASuggestionCommand command, ISuggestionRepository repository)
 {
     Command = command;
     RepositoryValidationContext        = new CommonSuggestionRepositoryValidationContext(repository, Command.SuggestionId);
     RepositoryValidationContext.UserId = command.UserId;
 }