Add FormValidationService and EventDefinitionService to dependency injection

Enhanced the application's service layer by adding FormValidationService and EventDefinitionService to the dependency injection container in Program.cs. Updated Create, Edit, and other relevant components to utilize these services for improved form validation and event processing functionality.
This commit is contained in:
2025-12-28 19:56:16 -05:00
parent 0358763601
commit 065a83442c
11 changed files with 642 additions and 200 deletions
+92 -44
View File
@@ -2,10 +2,14 @@
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Components.Shared.Components
@using MudBlazor
@using WebApp.Services
@inject AppDbContext Context
@inject NavigationManager NavigationManager
@inject ISnackbar Snackbar
@inject FormValidationService ValidationService
@if (_events is null)
@if (_events is null || _editContext is null)
{
<p><em>Loading...</em></p>
return;
@@ -17,10 +21,13 @@
ShowBackButton="true"
BackButtonUrl="@(ReturnUrl ?? "/teams")" />
<EditForm method="post" Model="Team" OnValidSubmit="AddTeam" FormName="create" Enhance>
<EditForm method="post" EditContext="@_editContext" OnInvalidSubmit="OnInvalidSubmit" FormName="create" Enhance>
<FormChangeTracker @ref="_formChangeTracker" />
<AntiforgeryToken />
<DataAnnotationsValidator />
<ValidationErrorDisplay Errors="_validationErrors" />
<MudGrid>
<MudItem xs="12" sm="7">
<MudPaper Elevation="2" Class="pa-6">
@@ -79,6 +86,8 @@
private Team Team { get; set; } = new();
private FormChangeTracker? _formChangeTracker;
private EditContext? _editContext;
private List<string> _validationErrors = new();
private List<EventDefinition>? _events;
private List<Student> _students = [];
private IEnumerable<Student> _selectedStudents = [];
@@ -93,6 +102,14 @@
.ToListAsync();
_students = await Context.Students.ToListAsync();
_editContext = new EditContext(Team);
}
private void OnInvalidSubmit(EditContext editContext)
{
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, Team, errors => _validationErrors = errors);
StateHasChanged();
}
private async Task OnEventChanged(EventDefinition selectedEvent)
@@ -111,61 +128,92 @@
private async Task AddTeam()
{
// Validate before processing
if (_editContext != null)
{
var errors = ValidationService.CollectValidationErrors(_editContext, Team);
if (errors.Any())
{
_validationErrors = ValidationService.HandleInvalidSubmit(_editContext, Team, err => _validationErrors = err);
StateHasChanged();
return;
}
}
_validationErrors.Clear();
// Clear previous error message
_errorMessage = null;
// Get current count of teams for this event
var existingTeamCount = await Context.Teams
.CountAsync(t => t.Event.Id == Team.Event.Id);
// Prohibit creation of third team
if (existingTeamCount >= 2)
try
{
_errorMessage = $"Cannot create a third team for {Team.Event.Name}. Maximum of 2 teams allowed.";
return;
}
// Get current count of teams for this event
var existingTeamCount = await Context.Teams
.CountAsync(t => t.Event.Id == Team.Event.Id);
// Handle automatic numbering based on event format
if (Team.Event.EventFormat == EventFormat.Individual && _selectedStudents.Count() == 1)
{
// For individual events, use student's first name as identifier
var student = _selectedStudents.First();
Team.Identifier = student.FirstName;
Team.Captain = student;
}
else if (existingTeamCount == 1)
{
// This is the second team - assign numbers
var existingTeam = await Context.Teams
.FirstOrDefaultAsync(t => t.Event.Id == Team.Event.Id);
if (existingTeam != null)
// Prohibit creation of third team
if (existingTeamCount >= 2)
{
// Update existing team to number 1
existingTeam.Identifier = "1";
Context.Teams.Update(existingTeam);
_errorMessage = $"Cannot create a third team for {Team.Event.Name}. Maximum of 2 teams allowed.";
Snackbar.Add(_errorMessage, Severity.Error);
return;
}
// Set new team to number 2
Team.Identifier = "2";
// Handle automatic numbering based on event format
if (Team.Event.EventFormat == EventFormat.Individual && _selectedStudents.Count() == 1)
{
// For individual events, use student's first name as identifier
var student = _selectedStudents.First();
Team.Identifier = student.FirstName;
Team.Captain = student;
}
else if (existingTeamCount == 1)
{
// This is the second team - assign numbers
var existingTeam = await Context.Teams
.FirstOrDefaultAsync(t => t.Event.Id == Team.Event.Id);
if (existingTeam != null)
{
// Update existing team to number 1
existingTeam.Identifier = "1";
Context.Teams.Update(existingTeam);
}
// Set new team to number 2
Team.Identifier = "2";
}
else
{
// This is the first team - no number
Team.Identifier = null;
}
// Add selected students to the team
foreach (var student in _selectedStudents)
{
Team.Students.Add(student);
}
Context.Teams.Add(Team);
await Context.SaveChangesAsync();
Snackbar.Add($"Team '{Team}' created successfully.", Severity.Success);
_formChangeTracker?.AllowNavigation();
NavigationManager.NavigateTo(ReturnUrl ?? "/teams");
}
else
catch (DbUpdateException ex)
{
// This is the first team - no number
Team.Identifier = null;
ValidationService.HandleDbUpdateException(
ex,
"An error occurred while creating the team.",
"team",
"creating");
}
// Add selected students to the team
foreach (var student in _selectedStudents)
catch (Exception ex)
{
Team.Students.Add(student);
ValidationService.HandleException(ex);
}
Context.Teams.Add(Team);
await Context.SaveChangesAsync();
_formChangeTracker?.AllowNavigation();
NavigationManager.NavigateTo(ReturnUrl ?? "/teams");
}
private void HandleCancel()