public async Task VotesServiceCountsVotesCorrectly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var votesRepository = new EfRepository <Vote>(new ApplicationDbContext(options.Options)); var service = new VotesService(votesRepository); await service.AddVote(1, "1", true); await service.AddVote(1, "2", true); await service.AddVote(1, "3", false); var votes = service.CountVotes(1); Assert.Equal(2, votes[0]); Assert.Equal(1, votes[1]); }
public IActionResult CastVote(Vote vote) { vote.ObjectId = User.Claims.First(claim => claim.Type == ClaimConstants.ObjectId).Value; vote.TenantId = User.Claims.First(claim => claim.Type == ClaimConstants.TenantId).Value; vote.CreatedAt = DateTimeOffset.Now; if (_votesService.HasVoted(vote.TenantId, vote.ObjectId)) { return(BadRequest("You have already casted a vote")); } if (_candidateService.IsACandidate(vote.Candidate) is false) { return(BadRequest("Not a valid candidate")); } _votesService.AddVote(vote); return(Ok()); }