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
+64 -18
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 (Team is null)
@if (Team is null || _editContext is null)
{
<p><em>Loading...</em></p>
return;
@@ -16,10 +20,13 @@
ShowBackButton="true"
BackButtonUrl="@(ReturnUrl ?? "/teams")" />
<EditForm method="post" Model="Team" OnValidSubmit="UpdateTeam" FormName="edit" Enhance>
<EditForm method="post" EditContext="@_editContext" OnInvalidSubmit="OnInvalidSubmit" FormName="edit" Enhance>
<FormChangeTracker @ref="_formChangeTracker" />
<AntiforgeryToken />
<DataAnnotationsValidator/>
<ValidationErrorDisplay Errors="_validationErrors" />
<MudGrid>
<MudItem xs="12" sm="7">
<MudPaper Elevation="2" Class="pa-6">
@@ -53,6 +60,8 @@
private Team? Team { get; set; }
private FormChangeTracker? _formChangeTracker;
private EditContext? _editContext;
private List<string> _validationErrors = new();
private IEnumerable<Student>? _selectedStudents = [];
private List<Student> _students = [];
@@ -69,24 +78,48 @@
{
NavigationManager.NavigateTo("notfound");
}
switch (Team!.Event.EventFormat)
else
{
case EventFormat.Individual when Team.Students.Count == 1:
Team.Captain ??= Team.Students[0];
Team.Identifier ??= Team.Captain.FirstName;
break;
case EventFormat.Team:
break;
default:
throw new ArgumentOutOfRangeException();
_editContext = new EditContext(Team);
switch (Team.Event.EventFormat)
{
case EventFormat.Individual when Team.Students.Count == 1:
Team.Captain ??= Team.Students[0];
Team.Identifier ??= Team.Captain.FirstName;
break;
case EventFormat.Team:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
private void OnInvalidSubmit(EditContext editContext)
{
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, Team!, errors => _validationErrors = errors);
StateHasChanged();
}
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more information, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks.
private async Task UpdateTeam()
{
// 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();
//Context.Attach(Team!).Entity = EntityState.Modified;
Team?.Students.Clear();
if (_selectedStudents != null)
@@ -105,20 +138,33 @@
try
{
await Context.SaveChangesAsync();
Snackbar.Add($"Team '{Team}' saved successfully.", Severity.Success);
_formChangeTracker?.AllowNavigation();
NavigationManager.NavigateTo(ReturnUrl ?? "/teams");
}
catch (DbUpdateConcurrencyException)
catch (DbUpdateConcurrencyException ex)
{
if (!TeamExists(Team!.Id))
{
NavigationManager.NavigateTo("notfound");
}
else
if (!ValidationService.HandleDbUpdateConcurrencyException(
ex,
() => TeamExists(Team!.Id),
"team",
() => NavigationManager.NavigateTo("notfound")))
{
throw;
}
}
catch (DbUpdateException ex)
{
ValidationService.HandleDbUpdateException(
ex,
"An error occurred while saving the team.",
"team",
"saving");
}
catch (Exception ex)
{
ValidationService.HandleException(ex);
}
}
private async Task HandleCancel()