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:
@@ -3,8 +3,13 @@
|
||||
@using WebApp.Components.Shared.Components
|
||||
@using Core.Utility
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using MudBlazor
|
||||
@using WebApp.Services
|
||||
@inject AppDbContext context
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ISnackbar Snackbar
|
||||
@inject FormValidationService ValidationService
|
||||
@inject EventDefinitionService EventDefinitionService
|
||||
|
||||
<PageHeader
|
||||
Title="Create"
|
||||
@@ -12,11 +17,13 @@
|
||||
ShowBackButton="true"
|
||||
BackButtonUrl="@(ReturnUrl ?? "/events")" />
|
||||
|
||||
<EditForm Model="EventDefinition" OnValidSubmit="OnValidSubmit" Enhance>
|
||||
<EditForm EditContext="@_editContext" OnValidSubmit="OnValidSubmit" OnInvalidSubmit="OnInvalidSubmit" Enhance>
|
||||
<FormChangeTracker @ref="_formChangeTracker" />
|
||||
<AntiforgeryToken />
|
||||
<DataAnnotationsValidator />
|
||||
|
||||
<ValidationErrorDisplay Errors="_validationErrors" />
|
||||
|
||||
<MudStack Spacing="4">
|
||||
<MudPaper Elevation="2" Class="pa-6">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Basic Information</MudText>
|
||||
@@ -111,66 +118,51 @@
|
||||
private EventDefinition EventDefinition { get; set; } = new();
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
private EditContext? _editContext;
|
||||
private List<string> _validationErrors = new();
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_editContext = new EditContext(EventDefinition);
|
||||
}
|
||||
|
||||
private void OnInvalidSubmit(EditContext editContext)
|
||||
{
|
||||
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, EventDefinition, errors => _validationErrors = errors);
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task OnValidSubmit()
|
||||
{
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
_validationErrors.Clear();
|
||||
|
||||
// Normalize and process related careers
|
||||
await ProcessRelatedCareersAsync(EventDefinition);
|
||||
try
|
||||
{
|
||||
// Normalize and process related careers
|
||||
await EventDefinitionService.ProcessRelatedCareersAsync(EventDefinition);
|
||||
|
||||
context.Events.Add(EventDefinition);
|
||||
await context.SaveChangesAsync();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/events");
|
||||
context.Events.Add(EventDefinition);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
Snackbar.Add($"Event '{EventDefinition.Name}' created successfully.", Severity.Success);
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/events");
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
ValidationService.HandleDbUpdateException(
|
||||
ex,
|
||||
"An error occurred while creating the event.",
|
||||
"event",
|
||||
"creating",
|
||||
"name");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ValidationService.HandleException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessRelatedCareersAsync(EventDefinition eventDefinition)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(eventDefinition.RelatedCareersText))
|
||||
{
|
||||
eventDefinition.RelatedCareers.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
var normalizedNames = CareerNormalizer.NormalizeCareerNames(eventDefinition.RelatedCareersText).ToList();
|
||||
|
||||
if (!normalizedNames.Any())
|
||||
{
|
||||
eventDefinition.RelatedCareers.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all existing careers from database (case-insensitive lookup)
|
||||
var existingCareers = await context.Careers.ToListAsync();
|
||||
var careerLookup = existingCareers.ToDictionary(
|
||||
c => CareerNormalizer.GetNormalizedKey(c.Name),
|
||||
c => c,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var careersToAdd = new List<Career>();
|
||||
|
||||
foreach (var normalizedName in normalizedNames)
|
||||
{
|
||||
var normalizedKey = CareerNormalizer.GetNormalizedKey(normalizedName);
|
||||
|
||||
if (careerLookup.TryGetValue(normalizedKey, out var existingCareer))
|
||||
{
|
||||
// Use existing career (preserve original capitalization)
|
||||
careersToAdd.Add(existingCareer);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create new career with the normalized name (preserving capitalization from input)
|
||||
var newCareer = new Career { Name = normalizedName };
|
||||
context.Careers.Add(newCareer);
|
||||
careersToAdd.Add(newCareer);
|
||||
careerLookup[normalizedKey] = newCareer;
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the collection
|
||||
eventDefinition.RelatedCareers = careersToAdd;
|
||||
}
|
||||
|
||||
private void HandleCancel()
|
||||
{
|
||||
|
||||
@@ -3,10 +3,16 @@
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Components.Shared.Components
|
||||
@using Core.Utility
|
||||
@using MudBlazor
|
||||
@using System.ComponentModel.DataAnnotations
|
||||
@using WebApp.Services
|
||||
@inject AppDbContext context
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ISnackbar Snackbar
|
||||
@inject FormValidationService ValidationService
|
||||
@inject EventDefinitionService EventDefinitionService
|
||||
|
||||
@if (EventDefinition is null)
|
||||
@if (EventDefinition is null || _editContext is null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
return;
|
||||
@@ -18,11 +24,13 @@
|
||||
ShowBackButton="true"
|
||||
BackButtonUrl="@(ReturnUrl ?? "/events")" />
|
||||
|
||||
<EditForm Model="EventDefinition" OnValidSubmit="OnValidSubmit" Enhance>
|
||||
<EditForm EditContext="@_editContext" OnValidSubmit="OnValidSubmit" OnInvalidSubmit="OnInvalidSubmit" Enhance>
|
||||
<FormChangeTracker @ref="_formChangeTracker" />
|
||||
<AntiforgeryToken />
|
||||
<DataAnnotationsValidator />
|
||||
|
||||
<ValidationErrorDisplay Errors="_validationErrors" />
|
||||
|
||||
<MudStack Spacing="4">
|
||||
<MudPaper Elevation="2" Class="pa-6">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Basic Information</MudText>
|
||||
@@ -120,6 +128,8 @@
|
||||
private EventDefinition? EventDefinition { get; set; }
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
private EditContext? _editContext;
|
||||
private List<string> _validationErrors = new();
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
@@ -135,96 +145,74 @@
|
||||
{
|
||||
// Populate RelatedCareersText from RelatedCareers collection
|
||||
EventDefinition.RelatedCareersText = string.Join("\n", EventDefinition.RelatedCareers.Select(c => c.Name));
|
||||
|
||||
// Create EditContext for validation
|
||||
_editContext = new EditContext(EventDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInvalidSubmit(EditContext editContext)
|
||||
{
|
||||
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, EventDefinition!, 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 OnValidSubmit()
|
||||
{
|
||||
// Get the tracked entity from the database
|
||||
var trackedEntity = await context.Events
|
||||
.Include(e => e.RelatedCareers)
|
||||
.FirstOrDefaultAsync(e => e.Id == EventDefinition!.Id);
|
||||
|
||||
if (trackedEntity == null)
|
||||
{
|
||||
NavigationManager.NavigateTo("notfound");
|
||||
return;
|
||||
}
|
||||
|
||||
// Update scalar properties from the form-bound entity
|
||||
context.Entry(trackedEntity).CurrentValues.SetValues(EventDefinition!);
|
||||
|
||||
// Normalize and process related careers
|
||||
await ProcessRelatedCareersAsync(trackedEntity);
|
||||
|
||||
try
|
||||
{
|
||||
// Get the tracked entity from the database
|
||||
var trackedEntity = await context.Events
|
||||
.Include(e => e.RelatedCareers)
|
||||
.FirstOrDefaultAsync(e => e.Id == EventDefinition!.Id);
|
||||
|
||||
if (trackedEntity == null)
|
||||
{
|
||||
Snackbar.Add("Event not found. It may have been deleted.", Severity.Error);
|
||||
NavigationManager.NavigateTo("notfound");
|
||||
return;
|
||||
}
|
||||
|
||||
// Update scalar properties from the form-bound entity
|
||||
context.Entry(trackedEntity).CurrentValues.SetValues(EventDefinition!);
|
||||
|
||||
// Normalize and process related careers
|
||||
await EventDefinitionService.ProcessRelatedCareersAsync(trackedEntity);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
_validationErrors.Clear();
|
||||
Snackbar.Add($"Event '{EventDefinition!.Name}' saved successfully.", Severity.Success);
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/events");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
catch (DbUpdateConcurrencyException ex)
|
||||
{
|
||||
if (!EventDefinitionExists(EventDefinition!.Id))
|
||||
{
|
||||
NavigationManager.NavigateTo("notfound");
|
||||
}
|
||||
else
|
||||
if (!ValidationService.HandleDbUpdateConcurrencyException(
|
||||
ex,
|
||||
() => EventDefinitionExists(EventDefinition!.Id),
|
||||
"event",
|
||||
() => NavigationManager.NavigateTo("notfound")))
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
ValidationService.HandleDbUpdateException(
|
||||
ex,
|
||||
"An error occurred while saving the event.",
|
||||
"event",
|
||||
"saving",
|
||||
"name");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ValidationService.HandleException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessRelatedCareersAsync(EventDefinition eventDefinition)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(eventDefinition.RelatedCareersText))
|
||||
{
|
||||
eventDefinition.RelatedCareers.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
var normalizedNames = CareerNormalizer.NormalizeCareerNames(eventDefinition.RelatedCareersText).ToList();
|
||||
|
||||
if (!normalizedNames.Any())
|
||||
{
|
||||
eventDefinition.RelatedCareers.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all existing careers from database (case-insensitive lookup)
|
||||
var existingCareers = await context.Careers.ToListAsync();
|
||||
var careerLookup = existingCareers.ToDictionary(
|
||||
c => CareerNormalizer.GetNormalizedKey(c.Name),
|
||||
c => c,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var careersToAdd = new List<Career>();
|
||||
|
||||
foreach (var normalizedName in normalizedNames)
|
||||
{
|
||||
var normalizedKey = CareerNormalizer.GetNormalizedKey(normalizedName);
|
||||
|
||||
if (careerLookup.TryGetValue(normalizedKey, out var existingCareer))
|
||||
{
|
||||
// Use existing career (preserve original capitalization)
|
||||
careersToAdd.Add(existingCareer);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create new career with the normalized name (preserving capitalization from input)
|
||||
var newCareer = new Career { Name = normalizedName };
|
||||
context.Careers.Add(newCareer);
|
||||
careersToAdd.Add(newCareer);
|
||||
careerLookup[normalizedKey] = newCareer;
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the collection
|
||||
eventDefinition.RelatedCareers = careersToAdd;
|
||||
}
|
||||
|
||||
private async Task HandleCancel()
|
||||
{
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
@page "/students/create"
|
||||
@attribute [Authorize]
|
||||
@using WebApp.Components.Shared.Components
|
||||
@using MudBlazor
|
||||
@using WebApp.Services
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@inject AppDbContext Context
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ISnackbar Snackbar
|
||||
@inject FormValidationService ValidationService
|
||||
|
||||
<PageHeader
|
||||
Title="Create"
|
||||
@@ -10,11 +15,13 @@
|
||||
ShowBackButton="true"
|
||||
BackButtonUrl="@(ReturnUrl ?? "/students")" />
|
||||
|
||||
<EditForm Model="Student" OnValidSubmit="OnValidSubmit" Enhance>
|
||||
<EditForm EditContext="@_editContext" OnValidSubmit="OnValidSubmit" OnInvalidSubmit="OnInvalidSubmit" Enhance>
|
||||
<FormChangeTracker @ref="_formChangeTracker" />
|
||||
<AntiforgeryToken />
|
||||
<DataAnnotationsValidator />
|
||||
|
||||
<ValidationErrorDisplay Errors="_validationErrors" />
|
||||
|
||||
<MudPaper Elevation="2" Class="pa-6">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Student Information</MudText>
|
||||
<MudGrid Spacing="3">
|
||||
@@ -53,14 +60,45 @@
|
||||
private Student Student { get; set; } = new() { TsaYear = 1 };
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
private EditContext? _editContext;
|
||||
private List<string> _validationErrors = new();
|
||||
|
||||
private void OnValidSubmit(EditContext context)
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
_editContext = new EditContext(Student);
|
||||
}
|
||||
|
||||
Context.Students.Add(Student);
|
||||
Context.SaveChanges();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/students");
|
||||
private void OnInvalidSubmit(EditContext editContext)
|
||||
{
|
||||
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, Student, errors => _validationErrors = errors);
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task OnValidSubmit()
|
||||
{
|
||||
_validationErrors.Clear();
|
||||
|
||||
try
|
||||
{
|
||||
Context.Students.Add(Student);
|
||||
await Context.SaveChangesAsync();
|
||||
|
||||
Snackbar.Add($"Student '{Student.FirstNameLastName}' created successfully.", Severity.Success);
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/students");
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
ValidationService.HandleDbUpdateException(
|
||||
ex,
|
||||
"An error occurred while creating the student.",
|
||||
"student",
|
||||
"creating");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ValidationService.HandleException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCancel()
|
||||
|
||||
@@ -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 (Student is null)
|
||||
@if (Student is null || _editContext is null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
return;
|
||||
@@ -19,10 +23,13 @@
|
||||
|
||||
@* https://www.mudblazor.com/components/form *@
|
||||
@* https://medium.com/@husainalbar/applying-mudblazor-for-crud-operations-in-our-blazor-project-a343037a52ef *@
|
||||
<EditForm method="post" Model="Student" OnValidSubmit="UpdateStudent" 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">
|
||||
@@ -64,6 +71,8 @@
|
||||
private Student? Student { get; set; }
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
private EditContext? _editContext;
|
||||
private List<string> _validationErrors = new();
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
@@ -73,12 +82,36 @@
|
||||
{
|
||||
NavigationManager.NavigateTo("notfound");
|
||||
}
|
||||
else
|
||||
{
|
||||
_editContext = new EditContext(Student);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInvalidSubmit(EditContext editContext)
|
||||
{
|
||||
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, Student!, 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 UpdateStudent()
|
||||
{
|
||||
// Validate before processing
|
||||
if (_editContext != null)
|
||||
{
|
||||
var errors = ValidationService.CollectValidationErrors(_editContext, Student!);
|
||||
if (errors.Any())
|
||||
{
|
||||
_validationErrors = ValidationService.HandleInvalidSubmit(_editContext, Student!, err => _validationErrors = err);
|
||||
StateHasChanged();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_validationErrors.Clear();
|
||||
|
||||
if (Student?.OfficerRole == 0)
|
||||
Student.OfficerRole = null;
|
||||
|
||||
@@ -87,20 +120,33 @@
|
||||
try
|
||||
{
|
||||
await Context.SaveChangesAsync();
|
||||
Snackbar.Add($"Student '{Student!.FirstNameLastName}' saved successfully.", Severity.Success);
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/students");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
catch (DbUpdateConcurrencyException ex)
|
||||
{
|
||||
if (!StudentExists(Student!.Id))
|
||||
{
|
||||
NavigationManager.NavigateTo("notfound");
|
||||
}
|
||||
else
|
||||
if (!ValidationService.HandleDbUpdateConcurrencyException(
|
||||
ex,
|
||||
() => StudentExists(Student!.Id),
|
||||
"student",
|
||||
() => NavigationManager.NavigateTo("notfound")))
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
ValidationService.HandleDbUpdateException(
|
||||
ex,
|
||||
"An error occurred while saving the student.",
|
||||
"student",
|
||||
"saving");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ValidationService.HandleException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleCancel()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
@namespace WebApp.Components.Shared.Components
|
||||
@using MudBlazor
|
||||
|
||||
@if (Errors != null && Errors.Any())
|
||||
{
|
||||
<MudAlert Severity="Severity.Error" Class="mb-4">
|
||||
<MudText Typo="Typo.h6" Class="mb-2">Please fix the following validation errors:</MudText>
|
||||
<ul style="margin: 0; padding-left: 20px;">
|
||||
@foreach (var error in Errors)
|
||||
{
|
||||
<li>@error</li>
|
||||
}
|
||||
</ul>
|
||||
</MudAlert>
|
||||
}
|
||||
|
||||
@code {
|
||||
/// <summary>
|
||||
/// List of validation error messages to display
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public List<string>? Errors { get; set; }
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<MudThemeProvider Theme="CustomThemes.Ceruleantheme" />
|
||||
<MudPopoverProvider />
|
||||
<MudDialogProvider />
|
||||
<MudSnackbarProvider />
|
||||
|
||||
<MudLayout>
|
||||
<MudAppBar Class="no-print">
|
||||
|
||||
@@ -174,6 +174,8 @@ builder.Services.AddScoped<ClipboardService>();
|
||||
builder.Services.AddScoped<WebApp.LocalStorageService>();
|
||||
builder.Services.AddScoped<WebApp.Services.IEventOccurrenceService, WebApp.Services.EventOccurrenceService>();
|
||||
builder.Services.AddScoped<Core.Services.IEventOccurrenceParserService, Core.Services.EventOccurrenceParserService>();
|
||||
builder.Services.AddScoped<WebApp.Services.FormValidationService>();
|
||||
builder.Services.AddScoped<WebApp.Services.EventDefinitionService>();
|
||||
|
||||
// State container for maintaining state per user connection (Blazor Server)
|
||||
builder.Services.AddScoped<StateContainer>();
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using Core.Entities;
|
||||
using Core.Utility;
|
||||
using Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace WebApp.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for handling EventDefinition-related operations
|
||||
/// </summary>
|
||||
public class EventDefinitionService
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public EventDefinitionService(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes and normalizes related careers for an EventDefinition.
|
||||
/// Matches existing careers by normalized name (case-insensitive) or creates new ones.
|
||||
/// </summary>
|
||||
/// <param name="eventDefinition">The EventDefinition to process careers for</param>
|
||||
public async Task ProcessRelatedCareersAsync(EventDefinition eventDefinition)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(eventDefinition.RelatedCareersText))
|
||||
{
|
||||
eventDefinition.RelatedCareers.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
var normalizedNames = CareerNormalizer.NormalizeCareerNames(eventDefinition.RelatedCareersText).ToList();
|
||||
|
||||
if (!normalizedNames.Any())
|
||||
{
|
||||
eventDefinition.RelatedCareers.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all existing careers from database (case-insensitive lookup)
|
||||
var existingCareers = await _context.Careers
|
||||
.Where(c => !string.IsNullOrWhiteSpace(c.Name))
|
||||
.ToListAsync();
|
||||
|
||||
// Build lookup dictionary, handling potential duplicates by taking the first occurrence
|
||||
var careerLookup = new Dictionary<string, Career>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var career in existingCareers)
|
||||
{
|
||||
var normalizedKey = CareerNormalizer.GetNormalizedKey(career.Name);
|
||||
if (!careerLookup.ContainsKey(normalizedKey))
|
||||
{
|
||||
careerLookup[normalizedKey] = career;
|
||||
}
|
||||
}
|
||||
|
||||
var careersToAdd = new List<Career>();
|
||||
|
||||
foreach (var normalizedName in normalizedNames)
|
||||
{
|
||||
var normalizedKey = CareerNormalizer.GetNormalizedKey(normalizedName);
|
||||
|
||||
if (careerLookup.TryGetValue(normalizedKey, out var existingCareer))
|
||||
{
|
||||
// Use existing career (preserve original capitalization)
|
||||
careersToAdd.Add(existingCareer);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create new career with the normalized name (preserving capitalization from input)
|
||||
var newCareer = new Career { Name = normalizedName };
|
||||
_context.Careers.Add(newCareer);
|
||||
careersToAdd.Add(newCareer);
|
||||
careerLookup[normalizedKey] = newCareer;
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the collection
|
||||
eventDefinition.RelatedCareers = careersToAdd;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Components.Forms;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MudBlazor;
|
||||
|
||||
namespace WebApp.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for handling form validation errors and displaying them to users
|
||||
/// </summary>
|
||||
public class FormValidationService
|
||||
{
|
||||
private readonly ISnackbar _snackbar;
|
||||
|
||||
public FormValidationService(ISnackbar snackbar)
|
||||
{
|
||||
_snackbar = snackbar;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Collects all validation errors from an EditContext and model validation
|
||||
/// </summary>
|
||||
/// <param name="editContext">The EditContext from the form</param>
|
||||
/// <param name="model">The model being validated</param>
|
||||
/// <returns>List of validation error messages</returns>
|
||||
public List<string> CollectValidationErrors(EditContext editContext, object model)
|
||||
{
|
||||
var errors = new List<string>();
|
||||
|
||||
// Validate the entire model using data annotations
|
||||
var validationResults = new List<ValidationResult>();
|
||||
var validationContext = new ValidationContext(model);
|
||||
bool isValid = Validator.TryValidateObject(model, validationContext, validationResults, true);
|
||||
|
||||
if (!isValid)
|
||||
{
|
||||
foreach (var result in validationResults)
|
||||
{
|
||||
foreach (var memberName in result.MemberNames)
|
||||
{
|
||||
var errorMessage = $"{memberName}: {result.ErrorMessage}";
|
||||
if (!errors.Contains(errorMessage))
|
||||
{
|
||||
errors.Add(errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Also get validation messages from EditContext (this gets all field-level messages)
|
||||
var validationMessages = editContext.GetValidationMessages();
|
||||
foreach (var message in validationMessages)
|
||||
{
|
||||
if (!errors.Contains(message))
|
||||
{
|
||||
errors.Add(message);
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles invalid form submission by collecting errors and displaying them
|
||||
/// </summary>
|
||||
/// <param name="editContext">The EditContext from the form</param>
|
||||
/// <param name="model">The model being validated</param>
|
||||
/// <param name="onErrorsCollected">Optional callback to handle the collected errors (e.g., store in component state)</param>
|
||||
/// <returns>List of validation error messages</returns>
|
||||
public List<string> HandleInvalidSubmit(EditContext editContext, object model, Action<List<string>>? onErrorsCollected = null)
|
||||
{
|
||||
var errors = CollectValidationErrors(editContext, model);
|
||||
|
||||
if (onErrorsCollected != null)
|
||||
{
|
||||
onErrorsCollected(errors);
|
||||
}
|
||||
|
||||
if (errors.Any())
|
||||
{
|
||||
var errorText = string.Join("; ", errors);
|
||||
_snackbar.Add($"Validation failed: {errorText}", Severity.Error);
|
||||
}
|
||||
else
|
||||
{
|
||||
_snackbar.Add("Please correct the validation errors and try again.", Severity.Warning);
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles DbUpdateException and displays user-friendly error messages
|
||||
/// </summary>
|
||||
/// <param name="ex">The DbUpdateException to handle</param>
|
||||
/// <param name="defaultErrorMessage">Default error message if specific handling isn't available</param>
|
||||
/// <param name="entityName">Name of the entity being saved (e.g., "event", "student", "team")</param>
|
||||
/// <param name="operation">Operation being performed (e.g., "saving", "creating")</param>
|
||||
/// <param name="uniqueConstraintField">Field name for UNIQUE constraint errors (e.g., "name", "email")</param>
|
||||
public void HandleDbUpdateException(
|
||||
DbUpdateException ex,
|
||||
string defaultErrorMessage,
|
||||
string entityName = "record",
|
||||
string operation = "saving",
|
||||
string? uniqueConstraintField = null)
|
||||
{
|
||||
var errorMessage = defaultErrorMessage;
|
||||
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
var innerMessage = ex.InnerException.Message;
|
||||
|
||||
if (innerMessage.Contains("UNIQUE constraint", StringComparison.OrdinalIgnoreCase) ||
|
||||
innerMessage.Contains("duplicate key", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(uniqueConstraintField))
|
||||
{
|
||||
errorMessage = $"A {entityName} with this {uniqueConstraintField} already exists. Please choose a different {uniqueConstraintField}.";
|
||||
}
|
||||
else
|
||||
{
|
||||
errorMessage = $"A {entityName} with this information already exists. Please check for duplicates.";
|
||||
}
|
||||
}
|
||||
else if (innerMessage.Contains("FOREIGN KEY constraint", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
errorMessage = $"Cannot {operation}: This {entityName} is referenced by other records.";
|
||||
}
|
||||
else
|
||||
{
|
||||
errorMessage = $"Database error: {innerMessage}";
|
||||
}
|
||||
}
|
||||
|
||||
_snackbar.Add(errorMessage, Severity.Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles generic exceptions and displays user-friendly error messages
|
||||
/// </summary>
|
||||
/// <param name="ex">The Exception to handle</param>
|
||||
public void HandleException(Exception ex)
|
||||
{
|
||||
var errorMessage = $"An unexpected error occurred: {ex.Message}";
|
||||
_snackbar.Add(errorMessage, Severity.Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles DbUpdateConcurrencyException and displays user-friendly error messages
|
||||
/// </summary>
|
||||
/// <param name="ex">The DbUpdateConcurrencyException to handle</param>
|
||||
/// <param name="entityExists">Function to check if the entity still exists</param>
|
||||
/// <param name="entityName">Name of the entity (e.g., "event", "student", "team")</param>
|
||||
/// <param name="onNotFound">Optional action to perform when entity is not found (e.g., navigate to notfound page)</param>
|
||||
/// <returns>True if the exception was handled (entity not found), false if it should be rethrown</returns>
|
||||
public bool HandleDbUpdateConcurrencyException(
|
||||
DbUpdateConcurrencyException ex,
|
||||
Func<bool> entityExists,
|
||||
string entityName = "record",
|
||||
Action? onNotFound = null)
|
||||
{
|
||||
if (!entityExists())
|
||||
{
|
||||
_snackbar.Add($"{entityName.Substring(0, 1).ToUpper()}{entityName.Substring(1)} not found. It may have been deleted.", Severity.Error);
|
||||
onNotFound?.Invoke();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_snackbar.Add($"The {entityName} was modified by another user. Please refresh and try again.", Severity.Warning);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user