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); }
public static SimpleValidationResult CanVote(Election election) { SimpleValidationResult result = new SimpleValidationResult(); result.RequireCurrentState(election, ElectionState.Voting); return(result); }
public static SimpleValidationResult CanDeactivate(Election election) { SimpleValidationResult result = new SimpleValidationResult(); result.RequireCurrentState(election, ElectionState.PreNominations); return(result); }
public static SimpleValidationResult CanEdit(Election election) { SimpleValidationResult result = new SimpleValidationResult(); result.ProhibitState(election, ElectionState.ResultsPublished); result.ProhibitState(election, ElectionState.Aborted); return(result); }
public static SimpleValidationResult CanForcePositionGeneration(Election election) { SimpleValidationResult result = new SimpleValidationResult(); result.RequireCurrentState(election, ElectionState.Disabled); result.RequireType(election, ElectionType.CourseRepresentative); return(result); }
private static void RequireType( this SimpleValidationResult result, Election election, ElectionType requiredType ) { if (election.Type != requiredType) { result.Violations.Add(new NotRequiredType(requiredType)); } }
private static void ProhibitState( this SimpleValidationResult result, Election election, ElectionState prohibitedState ) { if (election.State == prohibitedState) { result.Violations.Add(new ProhibitedState(prohibitedState)); } }
private static void RequireCurrentState( this SimpleValidationResult result, Election election, ElectionState requiredState ) { if (election.State != requiredState) { result.Violations.Add(new NotInRequiredState(requiredState)); } }
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); }
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); }