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

Loading...

return; } @foreach (var evt in _events) { } @switch (_existingTeams?.Count) { case 1: A team for @Team.Event.Name already exists. This will be team 2, and the existing team will become team 1. break; case >= 2: Two teams for @Team.Event.Name already exist. Cannot create a third team. break; } @if (!string.IsNullOrEmpty(_errorMessage)) { @_errorMessage } Add Cancel @code { [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } [SupplyParameterFromForm] private Team Team { get; set; } = new(); private FormChangeTracker? _formChangeTracker; private EditContext? _editContext; private List _validationErrors = new(); private List? _events; private List _students = []; private IEnumerable _selectedStudents = []; private string? _errorMessage; private List? _existingTeams; protected override async Task OnInitializedAsync() { _events = await Context.Events .OrderBy(e => e.Name) .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) { Team.Event = selectedEvent; // Clear any previous error messages _errorMessage = null; // Get all existing teams for this event _existingTeams = await Context.Teams .Include(t => t.Event) .Where(t => t.Event.Id == selectedEvent.Id) .ToListAsync(); } 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; try { // 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) { _errorMessage = $"Cannot create a third team for {Team.Event.Name}. Maximum of 2 teams allowed."; Snackbar.Add(_errorMessage, Severity.Error); return; } // 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"); } catch (DbUpdateException ex) { ValidationService.HandleDbUpdateException( ex, "An error occurred while creating the team.", "team", "creating"); } catch (Exception ex) { ValidationService.HandleException(ex); } } private void HandleCancel() { _formChangeTracker?.AllowNavigation(); NavigationManager.NavigateTo(ReturnUrl ?? "/teams"); } }