Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public static SimpleValidationResult CanVote(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.RequireCurrentState(election, ElectionState.Voting);

            return(result);
        }
Exemplo n.º 3
0
        public static SimpleValidationResult CanDeactivate(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

            result.RequireCurrentState(election, ElectionState.PreNominations);

            return(result);
        }
Exemplo n.º 4
0
        public static SimpleValidationResult CanEdit(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

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

            return(result);
        }
Exemplo n.º 5
0
        public static SimpleValidationResult CanForcePositionGeneration(Election election)
        {
            SimpleValidationResult result = new SimpleValidationResult();

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

            return(result);
        }
Exemplo n.º 6
0
 private static void RequireType(
     this SimpleValidationResult result,
     Election election,
     ElectionType requiredType
     )
 {
     if (election.Type != requiredType)
     {
         result.Violations.Add(new NotRequiredType(requiredType));
     }
 }
Exemplo n.º 7
0
 private static void ProhibitState(
     this SimpleValidationResult result,
     Election election,
     ElectionState prohibitedState
     )
 {
     if (election.State == prohibitedState)
     {
         result.Violations.Add(new ProhibitedState(prohibitedState));
     }
 }
Exemplo n.º 8
0
 private static void RequireCurrentState(
     this SimpleValidationResult result,
     Election election,
     ElectionState requiredState
     )
 {
     if (election.State != requiredState)
     {
         result.Violations.Add(new NotInRequiredState(requiredState));
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }