public async Task <VotingDto> Vote(VoteRequestDto request, Guid userId) { try { var vote = await _votingRepository.GetVoteById(request.VoteID); if (vote.Voters != null && vote.Voters.Any(v => v.ID == userId)) { throw new HttpExceptionBase("User already voted"); } if (vote.DueDate < DateTime.Now) { throw new HttpExceptionBase("Vote has passed due date"); } if (vote.Voters == null) { vote.Voters = new List <User>(); } var user = await _userRepository.GetUserById(userId); vote.Voters.Add(user); vote.VotersCount++; await _votingRepository.UpdateAsync(vote); return(new VotingDto { ID = vote.ID, Name = vote.Name, Description = vote.Description, DueDate = vote.DueDate, CreatedDate = vote.CreatedDate, VotersCount = vote.VotersCount }); } catch (Exception ex) { throw ex; } }
public async Task <VotingViewModel> Details(Guid id) { var voting = await _votingRepository.GetVoteById(id); if (voting == null) { return(null); } return(new VotingViewModel { Name = voting.Name, Description = voting.Description, DueDate = voting.DueDate, Category = voting.Category.Name, CreatedDate = voting.CreatedDate, VotersCount = voting.VotersCount, CategoryId = voting.CategoryId }); }