132 lines
4.0 KiB
Plaintext
132 lines
4.0 KiB
Plaintext
@page "/teams/edit"
|
|
@attribute [Authorize]
|
|
@using Microsoft.EntityFrameworkCore
|
|
@using WebApp.Components.Shared.Components
|
|
@inject AppDbContext Context
|
|
@inject NavigationManager NavigationManager
|
|
|
|
@if (Team is null)
|
|
{
|
|
<p><em>Loading...</em></p>
|
|
return;
|
|
}
|
|
|
|
<PageHeader Title="Edit Team"
|
|
Subtitle=@Team.ToString()
|
|
ShowBackButton="true"
|
|
BackButtonUrl="/teams" />
|
|
|
|
<EditForm method="post" Model="Team" OnValidSubmit="UpdateTeam" FormName="edit" Enhance>
|
|
<FormChangeTracker @ref="_formChangeTracker" />
|
|
<AntiforgeryToken />
|
|
<DataAnnotationsValidator/>
|
|
<MudGrid>
|
|
<MudItem xs="12" sm="7">
|
|
<MudPaper Class="pa-4">
|
|
<StudentToggleSelector Students="@_students"
|
|
@bind-SelectedStudents="_selectedStudents"
|
|
Title="Students"
|
|
ShowFullName="true" />
|
|
<MudStack Class="mt-4">
|
|
<TeamCaptainSelector Students="@_selectedStudents"
|
|
@bind-SelectedCaptain="Team.Captain"
|
|
Title="Captain" />
|
|
</MudStack>
|
|
</MudPaper>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</EditForm>
|
|
|
|
<FormActions>
|
|
<MudButton OnClick="UpdateTeam" Variant="Variant.Filled" Color="Color.Primary">Save</MudButton>
|
|
<MudButton OnClick="HandleCancel" Variant="Variant.Text">Cancel</MudButton>
|
|
</FormActions>
|
|
|
|
@code {
|
|
[SupplyParameterFromQuery]
|
|
private int Id { get; set; }
|
|
|
|
[SupplyParameterFromForm]
|
|
private Team? Team { get; set; }
|
|
|
|
private FormChangeTracker? _formChangeTracker;
|
|
private IEnumerable<Student>? _selectedStudents = [];
|
|
private List<Student> _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");
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
// 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()
|
|
{
|
|
//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();
|
|
_formChangeTracker?.AllowNavigation();
|
|
NavigationManager.NavigateTo("/teams");
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!TeamExists(Team!.Id))
|
|
{
|
|
NavigationManager.NavigateTo("notfound");
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HandleCancel()
|
|
{
|
|
_formChangeTracker?.AllowNavigation();
|
|
NavigationManager.NavigateTo("/teams");
|
|
}
|
|
|
|
private bool TeamExists(int id)
|
|
{
|
|
return Context.Teams.Any(e => e.Id == id);
|
|
}
|
|
}
|