1c7e704ad3
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
285 lines
12 KiB
Plaintext
285 lines
12 KiB
Plaintext
@page "/settings/validation"
|
|
@attribute [Authorize(Roles = AuthRoles.Administrator)]
|
|
@using Core.Validation
|
|
@using WebApp.Authentication
|
|
@using System.Text.Json
|
|
@inject IWebHostEnvironment Environment
|
|
@inject IConfiguration Configuration
|
|
|
|
@rendermode InteractiveServer
|
|
|
|
<PageTitle>Validation Settings</PageTitle>
|
|
|
|
<MudContainer MaxWidth="MaxWidth.Large" Class="mt-4">
|
|
<MudText Typo="Typo.h3" Class="mb-4">Validation Settings</MudText>
|
|
<MudText Typo="Typo.body1" Class="mb-6">Configure validation rules and thresholds. Changes take effect on next application restart.</MudText>
|
|
|
|
@if (_config != null)
|
|
{
|
|
<MudPaper Class="pa-6 mb-4">
|
|
<MudText Typo="Typo.h5" Class="mb-4">Event Count Thresholds</MudText>
|
|
<MudGrid>
|
|
<MudItem xs="12" md="6">
|
|
<MudNumericField @bind-Value="_config.MinRecommendedEvents"
|
|
Label="Minimum Recommended Events"
|
|
Variant="Variant.Outlined"
|
|
HelperText="Warning if student has fewer events"
|
|
Min="0" Max="10" />
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudNumericField @bind-Value="_config.MaxRecommendedEvents"
|
|
Label="Maximum Recommended Events"
|
|
Variant="Variant.Outlined"
|
|
HelperText="Warning if student has more events"
|
|
Min="0" Max="10" />
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudNumericField @bind-Value="_config.MinCriticalEvents"
|
|
Label="Minimum Critical Events"
|
|
Variant="Variant.Outlined"
|
|
HelperText="Error if student has fewer events"
|
|
Min="0" Max="10" />
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudNumericField @bind-Value="_config.MaxCriticalEvents"
|
|
Label="Maximum Critical Events"
|
|
Variant="Variant.Outlined"
|
|
HelperText="Error if student has more events"
|
|
Min="0" Max="10" />
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudNumericField @bind-Value="_config.MaxRegionalEvents"
|
|
Label="Maximum Regional Events"
|
|
Variant="Variant.Outlined"
|
|
HelperText="Warning if student has more regional events"
|
|
Min="0" Max="10" />
|
|
</MudItem>
|
|
</MudGrid>
|
|
</MudPaper>
|
|
|
|
<MudPaper Class="pa-6 mb-4">
|
|
<MudText Typo="Typo.h5" Class="mb-4">Required Event Types</MudText>
|
|
<MudGrid>
|
|
<MudItem xs="12" md="6">
|
|
<MudSwitch @bind-Value="_config.RequireRegionalEvent"
|
|
Label="Require Regional Event"
|
|
Color="Color.Primary" />
|
|
<MudText Typo="Typo.body2" Class="mud-text-secondary ml-10">
|
|
Students must have at least one regional event
|
|
</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSwitch @bind-Value="_config.RequireOnSiteActivity"
|
|
Label="Require On-Site Activity"
|
|
Color="Color.Primary" />
|
|
<MudText Typo="Typo.body2" Class="mud-text-secondary ml-10">
|
|
Students must have at least one on-site activity
|
|
</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSwitch @bind-Value="_config.RequireIndividualEvent"
|
|
Label="Require Individual Event"
|
|
Color="Color.Primary" />
|
|
<MudText Typo="Typo.body2" Class="mud-text-secondary ml-10">
|
|
Students must have at least one individual event
|
|
</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSwitch @bind-Value="_config.RequireTeamCaptain"
|
|
Label="Require Team Captain"
|
|
Color="Color.Primary" />
|
|
<MudText Typo="Typo.body2" Class="mud-text-secondary ml-10">
|
|
Team events must have an assigned captain
|
|
</MudText>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</MudPaper>
|
|
|
|
<MudPaper Class="pa-6 mb-4">
|
|
<MudText Typo="Typo.h5" Class="mb-4">Validation Severity Levels</MudText>
|
|
<MudGrid>
|
|
<MudItem xs="12" md="6">
|
|
<MudSelect @bind-Value="_config.NoRegionalEventSeverity"
|
|
Label="No Regional Event"
|
|
Variant="Variant.Outlined">
|
|
<MudSelectItem Value="ValidationSeverity.Warning">Warning</MudSelectItem>
|
|
<MudSelectItem Value="ValidationSeverity.Error">Error</MudSelectItem>
|
|
</MudSelect>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSelect @bind-Value="_config.NoOnSiteActivitySeverity"
|
|
Label="No On-Site Activity"
|
|
Variant="Variant.Outlined">
|
|
<MudSelectItem Value="ValidationSeverity.Warning">Warning</MudSelectItem>
|
|
<MudSelectItem Value="ValidationSeverity.Error">Error</MudSelectItem>
|
|
</MudSelect>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSelect @bind-Value="_config.NoIndividualEventSeverity"
|
|
Label="No Individual Event"
|
|
Variant="Variant.Outlined">
|
|
<MudSelectItem Value="ValidationSeverity.Warning">Warning</MudSelectItem>
|
|
<MudSelectItem Value="ValidationSeverity.Error">Error</MudSelectItem>
|
|
</MudSelect>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSelect @bind-Value="_config.TeamSizeSeverity"
|
|
Label="Team Size Issues"
|
|
Variant="Variant.Outlined">
|
|
<MudSelectItem Value="ValidationSeverity.Warning">Warning</MudSelectItem>
|
|
<MudSelectItem Value="ValidationSeverity.Error">Error</MudSelectItem>
|
|
</MudSelect>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSelect @bind-Value="_config.EventCountSeverity"
|
|
Label="Event Count Issues"
|
|
Variant="Variant.Outlined">
|
|
<MudSelectItem Value="ValidationSeverity.Warning">Warning</MudSelectItem>
|
|
<MudSelectItem Value="ValidationSeverity.Error">Error</MudSelectItem>
|
|
</MudSelect>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSelect @bind-Value="_config.MissingCaptainSeverity"
|
|
Label="Missing Team Captain"
|
|
Variant="Variant.Outlined">
|
|
<MudSelectItem Value="ValidationSeverity.Warning">Warning</MudSelectItem>
|
|
<MudSelectItem Value="ValidationSeverity.Error">Error</MudSelectItem>
|
|
</MudSelect>
|
|
</MudItem>
|
|
<MudItem xs="12" md="6">
|
|
<MudSelect @bind-Value="_config.TooManyRegionalEventsSeverity"
|
|
Label="Too Many Regional Events"
|
|
Variant="Variant.Outlined">
|
|
<MudSelectItem Value="ValidationSeverity.Warning">Warning</MudSelectItem>
|
|
<MudSelectItem Value="ValidationSeverity.Error">Error</MudSelectItem>
|
|
</MudSelect>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</MudPaper>
|
|
|
|
<MudPaper Class="pa-6">
|
|
<MudGrid>
|
|
<MudItem xs="12">
|
|
<MudButton Variant="Variant.Filled"
|
|
Color="Color.Primary"
|
|
StartIcon="@Icons.Material.Filled.Save"
|
|
OnClick="SaveConfiguration"
|
|
Disabled="_isSaving">
|
|
@if (_isSaving)
|
|
{
|
|
<MudProgressCircular Class="mr-2" Size="Size.Small" Indeterminate="true" />
|
|
<span>Saving...</span>
|
|
}
|
|
else
|
|
{
|
|
<span>Save Configuration</span>
|
|
}
|
|
</MudButton>
|
|
<MudButton Variant="Variant.Outlined"
|
|
Class="ml-2"
|
|
OnClick="ResetToDefaults"
|
|
Disabled="_isSaving">
|
|
Reset to Defaults
|
|
</MudButton>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</MudPaper>
|
|
|
|
@if (!string.IsNullOrEmpty(_statusMessage))
|
|
{
|
|
<MudAlert Severity="@_statusSeverity" Class="mt-4">@_statusMessage</MudAlert>
|
|
}
|
|
}
|
|
else
|
|
{
|
|
<MudProgressCircular Indeterminate="true" />
|
|
}
|
|
</MudContainer>
|
|
|
|
@code {
|
|
private ValidationConfiguration? _config;
|
|
private bool _isSaving;
|
|
private string? _statusMessage;
|
|
private Severity _statusSeverity = Severity.Success;
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
// Load from IConfiguration
|
|
_config = Configuration.GetSection("ValidationSettings").Get<ValidationConfiguration>()
|
|
?? ValidationConfiguration.Default;
|
|
}
|
|
|
|
private string GetAppSettingsPath()
|
|
{
|
|
return Path.Combine(
|
|
Environment.ContentRootPath,
|
|
"Data",
|
|
"appsettings.json");
|
|
}
|
|
|
|
private async Task SaveConfiguration()
|
|
{
|
|
if (_config == null) return;
|
|
|
|
_isSaving = true;
|
|
_statusMessage = null;
|
|
|
|
try
|
|
{
|
|
var appSettingsPath = GetAppSettingsPath();
|
|
|
|
// Ensure Data directory exists
|
|
var dataDir = Path.GetDirectoryName(appSettingsPath);
|
|
if (dataDir != null && !Directory.Exists(dataDir))
|
|
{
|
|
Directory.CreateDirectory(dataDir);
|
|
}
|
|
|
|
// Read existing appsettings or create new
|
|
JsonDocument? existingDoc = null;
|
|
Dictionary<string, object?> settings;
|
|
|
|
if (File.Exists(appSettingsPath))
|
|
{
|
|
var existingJson = await File.ReadAllTextAsync(appSettingsPath);
|
|
existingDoc = JsonDocument.Parse(existingJson);
|
|
settings = JsonSerializer.Deserialize<Dictionary<string, object?>>(existingJson)
|
|
?? new Dictionary<string, object?>();
|
|
}
|
|
else
|
|
{
|
|
settings = new Dictionary<string, object?>();
|
|
}
|
|
|
|
// Update ValidationSettings section
|
|
settings["ValidationSettings"] = _config;
|
|
|
|
// Write back to file
|
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
|
var json = JsonSerializer.Serialize(settings, options);
|
|
await File.WriteAllTextAsync(appSettingsPath, json);
|
|
|
|
existingDoc?.Dispose();
|
|
|
|
_statusMessage = "Configuration saved successfully! Changes will take effect on next application restart.";
|
|
_statusSeverity = Severity.Success;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_statusMessage = $"Error saving configuration: {ex.Message}";
|
|
_statusSeverity = Severity.Error;
|
|
}
|
|
finally
|
|
{
|
|
_isSaving = false;
|
|
}
|
|
}
|
|
|
|
private void ResetToDefaults()
|
|
{
|
|
_config = ValidationConfiguration.Default;
|
|
_statusMessage = "Configuration reset to defaults. Click Save to apply.";
|
|
_statusSeverity = Severity.Info;
|
|
}
|
|
}
|