예제 #1
0
        public static SimpleValidationResult CanDelete(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.RequireCurrentState(election, ElectionState.Aborted);

            // Only check the time requirement when the state is correct
            if (result)
            {
                int daysRequired = GetConfiguration().MinDaysForDeletion;

                DateTime?abortedAt = new VotingDbContext().ElectionStateChanges
                                     .Where(change => change.ElectionId == election.Id)
                                     .Where(change => change.TargetState == ElectionState.Aborted)
                                     .OrderByDescending(change => change.CompletedAt)
                                     .Select(change => change.CompletedAt)
                                     .First();

                if (!abortedAt.HasValue)
                {
                    throw new Exception(
                              $"Transition to {ElectionState.Aborted} has no {nameof(ElectionStateChange.CompletedAt)} set"
                              );
                }

                if (abortedAt.Value.AddDays(daysRequired) > DateTime.Now)
                {
                    result.Violations.Add(new NotEnoughTimeSinceAbort(daysRequired));
                }
            }

            return(result);
        }
예제 #2
0
        public static SimpleValidationResult CanVote(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.RequireCurrentState(election, ElectionState.Voting);

            return(result);
        }
예제 #3
0
        public static SimpleValidationResult CanDeactivate(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.RequireCurrentState(election, ElectionState.PreNominations);

            return(result);
        }
예제 #4
0
        public static SimpleValidationResult CanEdit(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.ProhibitState(election, ElectionState.ResultsPublished);
            result.ProhibitState(election, ElectionState.Aborted);

            return(result);
        }
예제 #5
0
        public static SimpleValidationResult CanForcePositionGeneration(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.RequireCurrentState(election, ElectionState.Disabled);
            result.RequireType(election, ElectionType.CourseRepresentative);

            return(result);
        }
예제 #6
0
 private static void RequireType(
     this SimpleValidationResult result,
     Election election,
     ElectionType requiredType
     )
 {
     if (election.Type != requiredType)
     {
         result.Violations.Add(new NotRequiredType(requiredType));
     }
 }
예제 #7
0
 private static void ProhibitState(
     this SimpleValidationResult result,
     Election election,
     ElectionState prohibitedState
     )
 {
     if (election.State == prohibitedState)
     {
         result.Violations.Add(new ProhibitedState(prohibitedState));
     }
 }
예제 #8
0
 private static void RequireCurrentState(
     this SimpleValidationResult result,
     Election election,
     ElectionState requiredState
     )
 {
     if (election.State != requiredState)
     {
         result.Violations.Add(new NotInRequiredState(requiredState));
     }
 }
예제 #9
0
        public static SimpleValidationResult CanPublishResults(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.RequireCurrentState(election, ElectionState.Closed);

            if (election.ResultsText == null)
            {
                result.Violations.Add(new NoResultsEntered());
            }

            return(result);
        }
예제 #10
0
        public static SimpleValidationResult CanActivate(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.RequireCurrentState(election, ElectionState.Disabled);

            int hoursRequired = GetConfiguration().MinHoursUntilActivation;

            if (hoursRequired > 0 && election.Nominations.BeginsAt < DateTime.Now.AddHours(hoursRequired))
            {
                result.Violations.Add(new NominationsTooClose(hoursRequired));
            }

            if (election.PositionGenerationInProcess)
            {
                result.Violations.Add(new PositionGenerationInProcess());
            }

            return(result);
        }