Files
chapter-organizer/Core/Validation/Rules/BaseRules/EventCountThresholdRankingRuleBase.cs
T
poprhythm 1c7e704ad3 Add comprehensive validation system with runtime configuration
Implements a flexible validation framework for student rankings, team assignments, and team composition with administrator-configurable rules and thresholds.

Validation System:
- Reflection-based rule discovery eliminates manual registration
- Base classes (RequiredEventTypeRuleBase, EventCountThresholdRuleBase) reduce code duplication by ~250 lines
- 12 validation rules covering event requirements, counts, and team constraints
- Configurable severity levels (Warning/Error) per rule type
- ValidationService with caching for optimal performance
- Rules apply contextually (StudentRanking, StudentAssignment, TeamComposition)

Configuration & Admin UI:
- ValidationSettings admin page for editing thresholds and severity levels
- ChapterSettings admin page for editing chapter information
- Settings stored in Data/appsettings.json for runtime configuration
- JSON config works in both development and production environments
- Auto-creates Data directory and config template on first run

User Experience:
- ValidationWarnings component displays inline warnings/errors
- Integrated in Event Ranking, Registration, and Team Assignment pages
- Color-coded severity indicators (warning yellow, error red)
- Includes "Too Many Regional Events" rule (max 3 recommended)

Technical Improvements:
- Template Method pattern for rule base classes
- Singleton rule instances with lazy initialization
- Configuration loaded via IConfiguration with fallback to defaults
- Safe JSON updates preserve other appsettings sections
2025-12-13 22:15:16 -05:00

89 lines
2.7 KiB
C#

using Core.Entities;
namespace Core.Validation.Rules.BaseRules;
/// <summary>
/// Base class for validation rules that check event counts against thresholds for student rankings
/// Similar to EventCountThresholdRuleBase but works with Student entities instead of StudentEventStatistics
/// </summary>
public abstract class EventCountThresholdRankingRuleBase : IValidationRule<Student>
{
/// <summary>
/// Get the actual count to validate
/// </summary>
protected abstract int GetCount(Student student);
/// <summary>
/// Get the threshold value from configuration
/// </summary>
protected abstract int GetThreshold(ValidationConfiguration config);
/// <summary>
/// Check if the count violates the threshold
/// </summary>
protected abstract bool ViolatesThreshold(int count, int threshold);
/// <summary>
/// Get the severity from configuration
/// </summary>
protected abstract ValidationSeverity GetSeverity(ValidationConfiguration config);
/// <summary>
/// The validation warning code
/// </summary>
protected abstract string Code { get; }
/// <summary>
/// Build the display message
/// </summary>
protected abstract string GetMessage(int count, int threshold);
/// <summary>
/// Icon identifier for the warning (can be null)
/// </summary>
protected virtual string? IconIdentifier => null;
/// <summary>
/// Build additional metadata beyond the standard fields
/// </summary>
protected virtual Dictionary<string, object> BuildAdditionalMetadata(Student student, ValidationConfiguration config)
{
return new Dictionary<string, object>();
}
public ValidationWarning? Validate(Student entity, ValidationConfiguration config)
{
var count = GetCount(entity);
var threshold = GetThreshold(config);
if (!ViolatesThreshold(count, threshold))
return null;
var metadata = new Dictionary<string, object>
{
{ "StudentId", entity.Id },
{ "StudentName", entity.FirstNameLastName }
};
// Add any additional metadata from derived class
foreach (var kvp in BuildAdditionalMetadata(entity, config))
{
metadata[kvp.Key] = kvp.Value;
}
return new ValidationWarning
{
Code = Code,
Message = GetMessage(count, threshold),
Severity = GetSeverity(config),
Context = ValidationContext.StudentRanking,
IconIdentifier = IconIdentifier,
Metadata = metadata
};
}
public bool AppliesTo(ValidationContext context) =>
context == ValidationContext.StudentRanking ||
context == ValidationContext.StudentRegistration;
}