public async Task DownVote(int linkId, string userId) { var link = await _linksRepository.GetById(linkId); if (await _votesRepository.AlreadyVoted(linkId, userId)) { var vote = await _votesRepository.GetByLinkId(linkId); switch (vote.VoteType) { // Down - return case VoteType.Down: return; // Up - none vote case VoteType.Up: { vote.VoteType = VoteType.NonVote; link.Ups--; break; } // none vote - down case VoteType.NonVote: { vote.VoteType = VoteType.Down; link.Downs++; break; } } // TODO: Save vote and link to database await _linksRepository.Update(link); await _votesRepository.Update(vote); } else { // create new down vote var user = await _userRepository.GetById(userId); var newVote = new Vote(link, user, VoteType.Down); link.Downs++; await _votesRepository.Add(newVote); await _linksRepository.Update(link); } }
/// <summary> /// Records the votes sent via the IVR system. /// </summary> /// <param name="voter"> /// An instance of the <see cref="Voter"/> class containing the /// 'current' voter. /// </param> /// <param name="votesToRecord"> /// A List<Vote> containing the votes to save to the database. /// </param> /// <returns> /// A boolean value indicating whether (true) or not (false) the Votes /// were all recorded. /// </returns> public virtual bool RecordIvrVotes( Voter voter, List <Vote> votesToRecord) { var votesToRecordCount = 0; foreach (var vote in votesToRecord) { var newVote = vote; _votesRepository.Add(newVote); votesToRecordCount++; } // Mark Voting as Complete! voter.VoteCompleted = true; _votersRepository.Update(voter); // since all of the tables are within the single DbContext // (IVotingSiteAPIDbCtx/VotingSiteAPIDbCtx), it's okay to // use the Votes repository [in this context] in order to // get/use an instance of same. // This should write the Votes AND the Voter to the database // within a transaction. So, if either write fails, both should. var numChanges = _votesRepository.GetDbContext().SaveChanges(); // the /+ 1/ is for the Voter return(numChanges == votesToRecordCount + 1); }
public ICommandResult Handle(RegisterVotesCommand command) { var votes = new Votes(command.UserId, command.CardId, command.UserStoryId); AddNotifications(votes.Notifications); if (Invalid) return new CommandResult(false, "Por favor, corrija os campos abaixo", Notifications); var connectionFactory = _rabbitMQHelper.GetConnectionFactory(); using (_connection = _rabbitMQHelper.CreateConnection(connectionFactory)) { _rabbitMQHelper.WriteMessageOnQueue(votes.ToString(), queueName, _connection); } _votesRepository.Add(votes); return new CommandResult(true, "Cadastro realizado com sucesso", votes); }