Exemplo n.º 1
0
 public IActionResult GetVoteCount([FromBody] Common.Models.NewVote voteSearch)
 {
     return(this.Ok(this.voteRepository.GetNumberVotes(voteSearch.Email, voteSearch.Event)));
 }
Exemplo n.º 2
0
        public async Task <IActionResult> PostVote([FromBody] Common.Models.NewVote vote)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            var user = await this.userHelper.GetUserByEmailAsync(vote.Email);

            if (user == null)
            {
                return(this.BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "Invalid user"
                }));
            }

            var @event = await this.eventRepository.GetByIdAsync(vote.Event);

            if (@event == null)
            {
                return(this.BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "Invalid event"
                }));
            }

            if (@event.StartDate > DateTime.Now)
            {
                return(this.BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "The voting event has not started yet"
                }));
            }

            if (@event.FinishDate < DateTime.Now)
            {
                return(this.BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "The voting event has closed"
                }));
            }

            int userVote = this.voteRepository.GetNumberVotes(user.Email, @event.Id);

            if (userVote > 0)
            {
                return(this.BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "You already voted in this event"
                }));
            }

            var candidate = await this.eventRepository.GetCandidateAsync(vote.Candidate);

            if (candidate == null)
            {
                return(this.BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "Invalid candidate"
                }));
            }

            candidate.TotalVotes++;
            await this.eventRepository.UpdateCandidateAsync(candidate);

            var entityVote = new Data.Entities.Vote
            {
                User      = user,
                Event     = @event,
                Candidate = candidate
            };

            await this.voteRepository.CreateAsync(entityVote);

            return(Ok(new Response
            {
                IsSuccess = true,
                Message = $"Registered vote. The candidate \"{entityVote.Candidate.Name}\" has {candidate.TotalVotes} votes"
            }));
        }
Exemplo n.º 3
0
 public IActionResult GetVoteOfUser([FromBody] Common.Models.NewVote voteSearch)
 {
     return(this.Ok(this.voteRepository.GetVotesOfUser(voteSearch.Email)));
 }