Beispiel #1
0
        public async Task <IActionResult> Candidate(ElectionVotationViewModel model, int candidateId)
        {
            int postionVoted = 0;
            int?citizenId    = HttpContext.Session.GetInt32(Configuration.Citizen);

            if (!citizenId.HasValue)
            {
                return(RedirectToAction(nameof(Index)));
            }

            HttpContext.Session.SetInt32(model.CurrentPositionName, candidateId);

            model.Postions = (await _electionService.GetElectionByConditionAsync(e => e.IsActive == true).Result
                              .Include(e => e.ElectionPosition).ThenInclude(ep => ep.Position).FirstOrDefaultAsync())
                             .ElectionPosition.Select(ep => ep.Position).ToList();

            model.Postions.ForEach(p =>
            {
                if (HttpContext.Session.GetInt32(p.Name).HasValue)
                {
                    postionVoted++;
                }
            });

            if (postionVoted != model.Postions.Count())
            {
                return(RedirectToAction(nameof(Votation), model));
            }

            List <Result> results = new List <Result>();
            string        content = "Usted ha votado por los siguientes candidatos:";

            model.Postions.ForEach(p =>
            {
                Result result = new Result()
                {
                    ElectionId  = model.Id,
                    CandidateId = HttpContext.Session.GetInt32(p.Name).Value,
                    CitizenId   = citizenId.Value
                };
                Candidate candidate = _candidateService.GetCandidateById(HttpContext.Session.GetInt32(p.Name));
                content            += $"\n {p.Name}: {candidate.Name} {candidate.LastName}.";
                results.Add(result);
            });

            await _electionService.InsertElectionCitizenVote(new ElectionCitizen()
            {
                ElectionId = model.Id, CitizenId = citizenId.Value
            });

            await _electionService.InsertElectionResulAsync(results);

            var message = new Sistema_de_votacion.Mail.Message(new string[] { _citizenService.GetCitizenByIdAsync(citizenId.Value).Result.Email },
                                                               "RESULTADO DE VOTACION", content);
            await _emailSender.SendEmailAsync(message);

            HttpContext.Session.Clear();
            return(RedirectToAction("Index", "Elections", model));
        }