@page "/teams/edit" @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 || _editContext is null) {

Loading...

return; } Save Cancel @code { [SupplyParameterFromQuery] private int Id { get; set; } [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } [SupplyParameterFromForm] private Team? Team { get; set; } private FormChangeTracker? _formChangeTracker; private EditContext? _editContext; private List _validationErrors = new(); private IEnumerable? _selectedStudents = []; private List _students = []; protected override async Task OnInitializedAsync() { Team ??= await Context.Teams .Include(e => e.Event) .Include(e => e.Students) .FirstOrDefaultAsync(m => m.Id == Id); _students = await Context.Students.ToListAsync(); _selectedStudents = Team?.Students.ToList(); if (Team is null) { NavigationManager.NavigateTo("notfound"); } else { _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) foreach (var s in _selectedStudents) { Team?.Students.Add(s); } // Update identifier for individual events if (Team is { Event.EventFormat: EventFormat.Individual, Students.Count: 1 }) { Team.Captain ??= Team.Students[0]; Team.Identifier = Team.Captain.FirstName; } try { await Context.SaveChangesAsync(); Snackbar.Add($"Team '{Team}' saved successfully.", Severity.Success); _formChangeTracker?.AllowNavigation(); NavigationManager.NavigateTo(ReturnUrl ?? "/teams"); } catch (DbUpdateConcurrencyException ex) { 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() { // Discard all in-memory changes by reloading from database if (Team != null) { await Context.Entry(Team).ReloadAsync(); } _formChangeTracker?.AllowNavigation(); NavigationManager.NavigateTo(ReturnUrl ?? "/teams"); } private bool TeamExists(int id) { return Context.Teams.Any(e => e.Id == id); } }