Example #1
0
        public async Task Edit(VotingViewModel voting)
        {
            var _voting = await _votingRepository.GetVoteById(voting.ID);

            _voting.Name        = voting.Name;
            _voting.Description = voting.Description;
            _voting.CategoryId  = voting.CategoryId;
            _voting.DueDate     = voting.DueDate;

            await _votingRepository.UpdateAsync(_voting);
        }
Example #2
0
        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;
            }
        }