public IEnumerable Handle(PlaceVote c) { if (!Created) { throw new PollNotCreated(); } if (Ended) { throw new PollAlreadyEnded(); } if (!Options.Contains(c.Option)) { throw new InvalidOption(); } if (UsersThatVoted.Contains(c.User)) { throw new UserAlreadyVoted(); } yield return(new VotePlaced { Id = c.Id, User = c.User, Option = c.Option }); }
public async Task VoteAsync(int placeId, string userId, bool isUpVote) { var vote = this.votesRepository.All() .FirstOrDefault(x => x.PlaceId == placeId && x.UserId == userId); if (vote != null) { vote.Type = isUpVote ? VoteType.UpVote : VoteType.DownVote; } else { vote = new PlaceVote { PlaceId = placeId, UserId = userId, Type = isUpVote ? VoteType.UpVote : VoteType.DownVote, }; await this.votesRepository.AddAsync(vote); } await this.votesRepository.SaveChangesAsync(); }