Exemple #1
0
        public static int ElectionId(int userId)
        {
            if (userId == 0)
                return 0;

            using (var electionRepository = new ElectionRepository())
            {
                var election = electionRepository.GetElectionByUserId(userId);

                return election.ElectionId;
            }
        }
        public JsonResult ChangeElectionStatus(int newStatus)
        {
            using (var electionRepository = new ElectionRepository())
            using (var candidateRepository = new CandidateRepository())
            {
                var election = electionRepository.GetElectionByElectionId(ElectionConductor.ElectionId((int)Session["UserId"]));

                if ((newStatus == 1) && election.NoVote)
                {
                    var path = Server.MapPath("~/Content/Banners/NoVote.jpg");
                    var noVoteImage = System.IO.File.ReadAllBytes(path);
                    candidateRepository.CreateCandidateWithPhoto(new Candidate { ElectionId = ElectionConductor.ElectionId((int)Session["UserId"]), Name = "No Vote" }, noVoteImage);
                }

                var result = electionRepository.ChangeElectionStatus(ElectionConductor.ElectionId((int)Session["UserId"]), newStatus);

                return new JsonResult
                {
                    Data = new { isOk = result == 1 }
                };
            }
        }
        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);
            }
        }
        public JsonResult UpdateElection(ElectionSetupModel election)
        {
            using (var electionRepository = new ElectionRepository())
            {
                var electionToUpdate = new Election
                {
                    ElectionId = ElectionConductor.ElectionId((int) Session["UserId"]),
                    Name = election.Name,
                    MinVotes = election.MinVotes,
                    MaxVotes = election.MaxVotes,
                    NoVote = election.NoVote
                };

                var result = electionRepository.UpdateElectionSetup(electionToUpdate);

                return new JsonResult
                {
                    Data = new { isOk = result }
                };
            }
        }
        public ActionResult UpdateBanner(HttpPostedFileBase file)
        {
            using (var electionRepository = new ElectionRepository())
            {
                if (file != null && file.ContentLength > 0)
                {
                    var filePath = Path.Combine(Server.MapPath("~/Content/Banners"), file.FileName);
                    file.SaveAs(filePath);

                    var result = electionRepository.UpdateBanner(file.FileName, ElectionConductor.ElectionId((int)Session["UserId"]));
                }

                return RedirectToAction("Index");
            }
        }
        public ActionResult Results()
        {
            if (Session["UserId"] == null)
                return RedirectToAction("Index", "Home");

            using (var electionRepository = new ElectionRepository())
            {
                var election = electionRepository.GetElectionByElectionId(ElectionConductor.ElectionId((int)Session["UserId"]));

                var resultsModel = new ResultsModel
                {
                    MaxVotes = election.MaxVotes
                };

                return View(resultsModel);
            }
        }