public async Task <IActionResult> VoteForResult(ResultVoteViewModel model)
        {
            if (model.ProjectId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var voterId = GetAuthenticatedUser().Email;

            var vote = await _resultVoteRepository.GetAsync(model.ProjectId, model.ParticipantId, voterId);

            if (vote == null)
            {
                model.VoterUserId = voterId;

                await _resultVoteRepository.SaveAsync(model);

                var votes = await _resultVoteRepository.GetProjectResultVotesAsync(model.ProjectId);

                var totalVotes = votes.Count();
                var result     = await _resultRepository.GetAsync(model.ProjectId, model.ParticipantId);

                result.Votes += 1;

                await _resultRepository.UpdateAsync(result);
                await CalculateScores(totalVotes, model.ProjectId);
            }

            return(RedirectToAction("ProjectDetails", "Project", new { id = model.ProjectId }));
        }
        public async Task <IActionResult> VoteForResult(ResultVoteViewModel model)
        {
            if (model.ProjectId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var project = await _projectRepository.GetAsync(model.ProjectId);

            var voterId = GetAuthenticatedUser().Email;

            model.UserAgent = HttpContext.Request.Headers["User-Agent"].ToString();

            string voteType = null;
            var    role     = await _userRolesRepository.GetAsync(voterId);

            if (voterId == project.AuthorId)
            {
                voteType = "AUTHOR";
            }
            else if (role != null && role.Role == "ADMIN")
            {
                voteType = "ADMIN";
            }

            var vote = await _resultVoteRepository.GetAsync(model.ProjectId, model.ParticipantId, voterId);

            if (vote == null)
            {
                model.VoterUserId = voterId;
                model.Type        = voteType;

                await _resultVoteRepository.SaveAsync(model);

                var votes = await _resultVoteRepository.GetProjectResultVotesAsync(model.ProjectId);

                var totalVotes = votes.Count();
                var result     = await _resultRepository.GetAsync(model.ProjectId, model.ParticipantId);

                result.Votes += 1;

                await _resultRepository.UpdateAsync(result);
                await CalculateScores(totalVotes, model.ProjectId);

                return(RedirectToAction("ProjectDetails", "Project", new { id = model.ProjectId, resultsActive = true, votedForResult = true }));
            }
            else
            {
                await _resultVoteRepository.DeleteAsync(model.ProjectId, model.ParticipantId, voterId);

                var votes = await _resultVoteRepository.GetProjectResultVotesAsync(model.ProjectId);

                var totalVotes = votes.Count();
                var result     = await _resultRepository.GetAsync(model.ProjectId, model.ParticipantId);

                result.Votes -= 1;

                await _resultRepository.UpdateAsync(result);
                await CalculateScores(totalVotes, model.ProjectId);

                return(RedirectToAction("ProjectDetails", "Project", new { id = model.ProjectId, resultsActive = true, votedTwice = true }));
            }
        }
        public async Task <IActionResult> VoteForResult(ResultVoteViewModel model)
        {
            if (model.ProjectId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var project = await _projectRepository.GetAsync(model.ProjectId);

            var voterId = UserModel.GetAuthenticatedUser(User.Identity).Email;

            model.UserAgent = HttpContext.Request.Headers["User-Agent"].ToString();

            string voteType = null;
            var    role     = await _userRolesRepository.GetAsync(voterId);

            if (voterId == project.AuthorId)
            {
                voteType = ResultVoteTypes.Author;
            }
            else if (role != null && role.Role == StreamsRoles.Admin)
            {
                voteType = ResultVoteTypes.Admin;
            }

            var vote = await _resultVoteRepository.GetAsync(model.ProjectId, model.ParticipantId, voterId);

            if (vote == null)
            {
                model.VoterUserId = voterId;
                model.Type        = voteType;

                await _resultVoteRepository.SaveAsync(model);

                var votes = await _resultVoteRepository.GetProjectResultVotesAsync(model.ProjectId);

                var totalVotes = votes.Count();
                var result     = await _resultRepository.GetAsync(model.ProjectId, model.ParticipantId);

                result.Votes += 1;

                await _resultRepository.UpdateAsync(result);
                await CalculateScores(totalVotes, model.ProjectId);

                TempData["ShowVotedForResultModal"] = true;

                return(RedirectToAction("ProjectDetails", "Project", new { id = model.ProjectId }, "tab_Results"));
            }
            else
            {
                await _resultVoteRepository.DeleteAsync(model.ProjectId, model.ParticipantId, voterId);

                var votes = await _resultVoteRepository.GetProjectResultVotesAsync(model.ProjectId);

                var totalVotes = votes.Count();
                var result     = await _resultRepository.GetAsync(model.ProjectId, model.ParticipantId);

                result.Votes -= 1;

                await _resultRepository.UpdateAsync(result);
                await CalculateScores(totalVotes, model.ProjectId);

                TempData["ShowVotedTwiceModal"] = true;

                return(RedirectToAction("ProjectDetails", "Project", new { id = model.ProjectId }, "tab_Results"));
            }
        }