Example #1
0
        public IList <VoteResultVM> GetCatRanking()
        {
            var cats  = this._context.Cats.ToList();
            var votes = this._context.Votes.Include(v => v.Winner).ToList();
            List <VoteResultVM> voteResults = new List <VoteResultVM>();

            foreach (var cat in cats)
            {
                VoteResultVM resultVote = new VoteResultVM
                {
                    Cat = cat.ToCatVM()
                };

                resultVote.TotalVote = (votes != null) ? votes.Where(x => x.Winner?.Id == cat.Id).Count() : 0;
                voteResults.Add(resultVote);
            }

            return(voteResults.OrderByDescending(x => x.TotalVote).ToList());
        }
Example #2
0
        public PartialViewResult VoteFor(int positionId, string positionName)
        {
            // check if vote is finished
            if (positionId == 0 && positionName.ToLower() == "finished")
            {
                // get all votes for user
                var userVotes = db.Votes.Where(x => x.VoterId == User.VoterId).ToList();

                // init voteResult
                List <VoteResultVM> voteResults = new List <VoteResultVM>();

                foreach (var vote in userVotes)
                {
                    var result = new VoteResultVM
                    {
                        PositionName = db.Position.Where(x => x.Id == vote.PositionId).Select(x => x.Name).FirstOrDefault()
                    };

                    if (vote.CandidateId == null)
                    {
                        result.CandidateName = "NO VOTE";
                    }
                    else
                    {
                        result.CandidateName = db.VoteUsers.Where(x => x.VoterId == vote.CandidateId).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault();
                    }

                    //add result
                    voteResults.Add(result);
                }

                // construct result mail message
                string resultMail = "";

                foreach (var result in voteResults)
                {
                    string votedFor = "<tr>" +
                                      "<td style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\">" + result.PositionName +
                                      "</td>" +
                                      "<td style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\">" + result.CandidateName +
                                      "</td>" +
                                      "</tr>";

                    resultMail += votedFor;
                }

                // get username and date
                string   userName = db.VoteUsers.Where(x => x.Email == User.Email).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault();
                DateTime?voteDate = db.VoteUsers.Where(x => x.Email == User.Email).Select(x => x.LastVoteDateUtc).FirstOrDefault();

                string date;
                if (voteDate != null)
                {
                    DateTime updatedVoteDate = voteDate ?? DateTime.Now;

                    var day       = updatedVoteDate.Day;
                    var dayOfWeek = updatedVoteDate.DayOfWeek;
                    var month     = updatedVoteDate.ToString("MMMM");
                    var year      = updatedVoteDate.Year;

                    date = day + " " + month + ", " + year;
                }
                else
                {
                    date = "-";
                }

                string message = "<html><body>" +
                                 "<div style=\"text-align:center;max-width:600px;\">" +

                                 "<h3 style=\"background-color: #64608e;margin-bottom: 0px;padding: 10px 0px;color: #fff;\">" +
                                 "My Voting Summary - LAGSOBA '94  " +
                                 "</h3>" +

                                 "<div style=\"border:2px solid #64608e\">" +
                                 "<table style=\"width: 100%;margin-bottom: 1rem;\">" +

                                 "<tr>" +
                                 "<th style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\"> Position</th>" +
                                 "<th style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\"> Candidate Voted For</th>" +
                                 "</tr>" + resultMail +

                                 "</table>" +

                                 "<div style=\"margin-bottom: 20px;\">" +
                                 "<h6>The Vote Summary Above was casted by:</h6>" +
                                 "<p style=\"margin: 0px;\"><strong>Name: </strong> " + userName + "</p>" +
                                 "<p><strong>Last Vote On: </strong> " + date + "</p>" +
                                 "</div>" +

                                 "<p style=\"color: #846d6dc7;\"><em>Thanks For Voting</em></p>" +

                                 "</div>" +
                                 "</div>" +
                                 "</body></html>";

                ViewBag.MyVoteResult = message;

                return(PartialView("_MyVoteResult"));
            }


            // get user Id
            string voterId = db.VoteUsers.Where(x => x.Email == User.Email).Select(x => x.VoterId).FirstOrDefault();

            VotingVM model = new VotingVM();

            // get position
            var postion = db.Position.Where(x => x.Id == positionId).FirstOrDefault();

            // get candidates
            IEnumerable <CandidateVM> candidates = User.GetCandidatesFor(postion.Id);

            // init model.candidates
            model.Candidates = candidates.Select(x => new Candidates
            {
                Id         = x.VoterId,
                Name       = x.FirstName + " " + x.LastName,
                ImageUrl   = x.ImageString,
                PositionId = x.PositionId
            }).ToList();

            // get postion name
            model.PostionName = db.Position.Where(x => x.Id == postion.Id).Select(x => x.Name).FirstOrDefault();

            // check if user has casted votes for this position
            model.VoteCasted = false;
            var voteCasted = db.Votes.Where(x => x.VoterId == voterId && x.PositionId == postion.Id).FirstOrDefault();

            if (voteCasted != null)
            {
                model.VoteCasted       = true;
                model.VotedCandidateId = voteCasted.CandidateId;
            }

            // admin privileges
            model.IsAdmin = User.HasRole("Admin");

            // set user name
            ViewBag.VoterName = db.VoteUsers.Where(x => x.VoterId == voterId).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault();

            return(PartialView("_VoteForPosition", model));
        }
Example #3
0
        public ActionResult SendMeACopy()
        {
            // get userId
            var voterId = User.VoterId;

            // get all votes for user
            var userVotes = db.Votes.Where(x => x.VoterId == voterId).ToList();

            // init voteResult
            List <VoteResultVM> voteResults = new List <VoteResultVM>();

            foreach (var vote in userVotes)
            {
                var result = new VoteResultVM
                {
                    PositionName = db.Position.Where(x => x.Id == vote.PositionId).Select(x => x.Name).FirstOrDefault()
                };

                if (vote.CandidateId == null)
                {
                    result.CandidateName = "NO VOTE";
                }
                else
                {
                    result.CandidateName = db.VoteUsers.Where(x => x.VoterId == vote.CandidateId).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault();
                }

                //add result
                voteResults.Add(result);
            }

            // construct result mail message
            string resultMail = "";

            foreach (var result in voteResults)
            {
                string votedFor = "<tr>" +
                                  "<td style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\">" + result.PositionName +
                                  "</td>" +
                                  "<td style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\">" + result.CandidateName +
                                  "</td>" +
                                  "</tr>";

                resultMail += votedFor;
            }

            // Send an email with this link
            var mailModel = new EmailMessageVM();

            mailModel.ToAddress = User.Email;
            mailModel.Subject   = "Voting Result - LAGSOBA '94";

            // get username and date
            string   userName = db.VoteUsers.Where(x => x.Email == User.Email).Select(x => x.FirstName + " " + x.LastName).FirstOrDefault();
            DateTime?voteDate = db.VoteUsers.Where(x => x.Email == User.Email).Select(x => x.LastVoteDateUtc).FirstOrDefault();

            string date;

            if (voteDate != null)
            {
                DateTime updatedVoteDate = voteDate ?? DateTime.Now;

                var day       = updatedVoteDate.Day;
                var dayOfWeek = updatedVoteDate.DayOfWeek;
                var month     = updatedVoteDate.ToString("MMMM");
                var year      = updatedVoteDate.Year;

                date = day + " " + month + ", " + year;
            }
            else
            {
                date = "-";
            }

            string message = "<html><body>" +
                             "<div style=\"text-align:center;max-width:600px;\">" +

                             "<h3 style=\"background-color: #64608e;margin-bottom: 0px;padding: 10px 0px;color: #fff;\">" +
                             "My Voting Summary - LAGSOBA '94  " +
                             "</h3>" +

                             "<div style=\"border:2px solid #64608e\">" +
                             "<table style=\"width: 100%;margin-bottom: 1rem;\">" +

                             "<tr>" +
                             "<th style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\"> Position</th>" +
                             "<th style=\"padding: .75rem;vertical-align: top;border-top: 1px solid #dee2e6;\"> Candidate Voted For</th>" +
                             "</tr>" + resultMail +

                             "</table>" +

                             "<div style=\"margin-bottom: 20px;\">" +
                             "<h6>The Vote Summary Above was casted by:</h6>" +
                             "<p style=\"margin: 0px;\"><strong>Name: </strong> " + userName + "</p>" +
                             "<p><strong>Last Vote On: </strong> " + date + "</p>" +
                             "</div>" +


                             "<p style=\"color: #846d6dc7;\"><em>Thanks For Voting</em></p>" +
                             "</div>" +
                             "</div>" +
                             "</body></html>";

            mailModel.Message = message;

            // send email
            MailClient.SendEmail(mailModel);

            return(Json(new { Status = 1, Message = "Your vote summary has been forwarded to your email" }, JsonRequestBehavior.AllowGet));
        }