Пример #1
0
        public ActionResult DoVote(int nominationId)
        {
            DisplayNomineeEntry displayNomineeEntry = GetNomineeForVoting(nominationId);

            if (displayNomineeEntry == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // Everything validated, checked, etc. Let's do it!
            NominationEntry nominationEntry = displayNomineeEntry.ModelEntry;

            // Record that the student has voted for this position
            db.StudentVoteRecords.Add(new StudentVoteRecord()
            {
                Position = nominationEntry.Position,
                Username = User.Identity.GetUserId()
            });

            // Record the vote for this candidate
            Vote vote = new Vote
            {
                NominationEntry = nominationEntry,
                VotedAt         = DateTime.Now
            };

            db.Votes.Add(vote);
            db.SaveChanges();

            AuditLogManager.RecordVoteCast(vote);

            return(RedirectToAction("Vote", new { id = nominationEntry.Position.Election.Id }));
        }
Пример #2
0
        private DisplayNomineeEntry GetNomineeForVoting(int nominationId)
        {
            NominationEntry nominationEntry = db.NominationEntries.Find(nominationId);

            if (nominationEntry == null)
            {
                return(null);
            }

            Election        election        = nominationEntry.Position.Election;
            VotablePosition position        = nominationEntry.Position;
            string          currentUsername = User.Identity.GetUserId();

            // ReSharper disable once SimplifyLinqExpression
            if (!election.EligibilityEntries.Any(entry => entry.Username == currentUsername))
            {
                // This student is not eligible for this election
                return(null);
            }

            IDictionary <VotablePosition, PositionDisplayDataForVoting> positionsData = new VotingManager()
                                                                                        .PrepareVotingDataFor(
                currentUsername, election,
                positions => positions.Where(p => p == position)
                );

            if (
                !positionsData.ContainsKey(position) ||
                positionsData[position].Status != PositionDisplayDataForVoting.PositionStatus.CanVote
                )
            {
                // Student doesn't have access to this position or cannot vote for it or has voted already
                return(null);
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            return(positionsData[position].NomineeEntries.First(entry => entry.ModelEntry == nominationEntry));
        }
Пример #3
0
        public ActionResult UpdateNominationsStatus(int positionId, bool newStatus)
        {
            VotablePosition position = db.VotablePositions.Find(positionId);

            if (position == null)
            {
                return(HttpNotFound());
            }

            Election election        = position.Election;
            string   currentUsername = User.Identity.GetUserId();

            // ReSharper disable once SimplifyLinqExpression
            if (!election.EligibilityEntries.Any(entry => entry.Username == currentUsername))
            {
                // This student is not eligible for this election - pretend it doesn't exist
                return(HttpNotFound());
            }

            PositionEligibilityEntry positionEligibilityEntry = db.PositionEligibilityEntries
                                                                .FirstOrDefault(entry => entry.PositionId == positionId && entry.Username == currentUsername);

            if (positionEligibilityEntry == null)
            {
                // This student is not eligible for this position - pretend it doesn't exist
                return(HttpNotFound());
            }

            // At this point we are sure that the position is visible to this student
            // Now we need to check whether (s)he can change the status (at this time)

            if (!ElectionLifecycleInfo.CanNominate(election) || !positionEligibilityEntry.CanNominate)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // At this point we are sure that the student can change the nomination status for this position
            // Now we just need to create or delete the record in database (or do nothing if it's already matching)

            NominationEntry exitingNomination = position.NominationEntries
                                                .FirstOrDefault(entry => entry.Username == currentUsername);

            if (exitingNomination != null)
            {
                if (newStatus)
                {
                    // Do nothing
                }
                else
                {
                    // There is an existing one, remove it
                    db.NominationEntries.Remove(exitingNomination);
                    db.SaveChanges();
                }
            }
            else
            {
                if (newStatus)
                {
                    // No nomination currently, create one
                    db.NominationEntries.Add(new NominationEntry()
                    {
                        Position = position, Username = currentUsername
                    });
                    db.SaveChanges();
                }
                else
                {
                    // Do nothing
                }
            }

            AuditLogManager.RecordNominationUpdate(User, position, newStatus);

            return(RedirectToAction("Nominations", new { id = election.Id }));
        }
Пример #4
0
 public DisplayNomineeEntry([NotNull] NominationEntry modelEntry, bool isFound, [CanBeNull] string fullName)
 {
     FullName   = fullName;
     ModelEntry = modelEntry;
     IsFound    = isFound;
 }