Add Related Careers functionality to EventDefinition entity and update related components
Introduced a many-to-many relationship between EventDefinition and Career entities, allowing for the association of multiple careers with an event. Updated the AppDbContext to include a DbSet for Careers and modified the EventDefinitionConfiguration to handle the new relationship. Enhanced the Create, Edit, and Details components to support input and display of related careers, including normalization and processing logic for career names. Updated the database schema to reflect these changes.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
@page "/events/create"
|
||||
@attribute [Authorize]
|
||||
@using WebApp.Components.Shared.Components
|
||||
@using Core.Utility
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@inject AppDbContext context
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
@@ -52,6 +54,9 @@
|
||||
<MudItem xs="12">
|
||||
<MudTextField T="string" Label="Documentation" @bind-Value="EventDefinition.Documentation" For="@(() => EventDefinition.Documentation)" Variant="Variant.Outlined"></MudTextField>
|
||||
</MudItem>
|
||||
<MudItem xs="12">
|
||||
<MudTextField T="string" Label="Related Careers" AutoGrow="true" Lines="5" @bind-Value="EventDefinition.RelatedCareersText" HelperText="Enter one career per line (bullet points will be removed automatically)" Variant="Variant.Outlined"></MudTextField>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
</MudPaper>
|
||||
|
||||
@@ -107,15 +112,66 @@
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
|
||||
private void OnValidSubmit()
|
||||
private async Task OnValidSubmit()
|
||||
{
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
|
||||
// Normalize and process related careers
|
||||
await ProcessRelatedCareersAsync(EventDefinition);
|
||||
|
||||
context.Events.Add(EventDefinition);
|
||||
context.SaveChanges();
|
||||
await context.SaveChangesAsync();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/events");
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
|
||||
Reference in New Issue
Block a user