Example #1
0
        public ActionResult Index()
        {
            if (Session["UserId"] == null)
                return RedirectToAction("Index", "Home");

            using (var electionRepository = new ElectionRepository())
            using (var candidateRepository = new CandidateRepository())
            using (var voterRepository = new VoterRepository())
            {
                if (electionRepository.GetElectionByUserId((int)Session["UserId"]) == null)
                {
                    electionRepository.CreateElection(new Election
                    {
                        UserId = (int)Session["UserId"],
                        Name = "My Election"
                    });
                }

                var election = electionRepository.GetElectionByUserId((int)Session["UserId"]);

                var electionIndexModel = new ElectionIndexModel
                {
                    Setup = new ElectionSetupModel
                    {
                        Name = election.Name,
                        Status = election.Status,
                        MinVotes = election.MinVotes,
                        MaxVotes = election.MaxVotes,
                        NoVote = election.NoVote
                    },
                    Candidates = candidateRepository.GetCandidatesForElectionId(ElectionConductor.ElectionId((int)Session["UserId"])).OrderBy(x => x.Name).ToList(),
                    VotersCount = voterRepository.GetVotersForElectionId(ElectionConductor.ElectionId((int)Session["UserId"])).Count
                };

                return View(electionIndexModel);
            }
        }
Example #2
0
        /*public JsonResult GetCandidates()
        {
            using (var candidateRepository = new CandidateRepository())
            {
                var candidateListModel = new CandidateListModel { CandidateList = candidateRepository.GetCandidatesForElectionId(ElectionConductor.ElectionId((int)Session["UserId"])).OrderBy(x => x.Name).ToList() };

                return new JsonResult
                {
                    Data = new { isOk = true, CandidateList = candidateListModel }
                };
            }
        }*/
        /*public JsonResult GetVoters()
        {
            using (var voterRepository = new VoterRepository())
            {
                var voterListModel = new VoterListModel { VoterList = voterRepository.GetVotersForElectionId(ElectionConductor.ElectionId((int)Session["UserId"])) };

                return new JsonResult
                {
                    Data = new { isOk = true, VoterList = voterListModel }
                };
            }
        }*/
        public JsonResult ViewVotes()
        {
            using (var votesRepository = new VotesRepository())
            using (var candidateRepository = new CandidateRepository())
            {
                var voteList = new List<CandidateVotes>();
                var candidateList = candidateRepository.GetCandidatesForElectionId(ElectionConductor.ElectionId((int)Session["UserId"]));

                foreach (var candidate in candidateList)
                {
                    var votes = votesRepository.GetVotesForElectionIdAndCandidateId(ElectionConductor.ElectionId((int)Session["UserId"]), candidate.CandidateId);
                    voteList.Add(new CandidateVotes { Candidate = candidate, Votes = votes });
                }

                var voteListModel = new VoteListModel { VoteList = voteList.OrderByDescending(x => x.Votes).ToList() };

                return new JsonResult
                {
                    Data = new { isOk = true, VoteList = voteListModel }
                };
            }
        }