/// <summary> /// Edit the roster of a team /// </summary> /// <param name="team">The new values for the team</param> public async Task EditTeamRosterAsync(TournamentTeam team) { var existing = await Context.TournamentTeams .Include(t => t.Participants).ThenInclude(p => p.Player) .Where(t => t.TeamId == team.TeamId) .AsTracking() .SingleOrDefaultAsync(); if (existing is null) { throw new KeyNotFoundException(); } if (!Authorizer.HasClaim(EditTeamRoster, existing)) { throw new UnauthorizedAccessException(); } // Validate that the registration rules are adhered to var rules = await Context.TournamentRegistrationRules .Include(r => r.Tournament).ThenInclude(t => t.Teams) .Where(r => r.TournamentId == team.TournamentId && r.Region == team.Region) .SingleOrDefaultAsync(); var validator = new TournamentRegistrationRulesValidator(rules); if (!validator.CanEditTeamRoster()) { throw new ArgumentNullException("Team Rosters can no longer be adjusted."); } validator.ValidateTeam(team, rules.Tournament.Teams.Where(t => t.TeamId != team.TeamId)); await SetWoWsPlayersAsync(team.Region, team.Participants); await validator.ValidateDiscordAsync(team, getDiscordUser); // Update participants await InitializeParticipantsAsync(rules, team); await AddTeamRepClaims(team); await RemoveTeamRepClaims(team); var orphans = existing.Participants.Where(p => !team.Participants.Any(d => d.ParticipantId == p.ParticipantId)); await TrimOrphanParticipantsAsync(team, orphans); // Update roster on existing team existing.Participants = team.Participants; await Context.SaveChangesAsync(); }
/// <summary> /// Creates a tournament team /// </summary> /// <param name="team">The team that should be created</param> public async Task CreateTeamAsync(TournamentTeam team) { // Normalize team team.Tag = team.Tag.ToUpper(); // Validate that registration rules are adhered to var rules = await Context.TournamentRegistrationRules .Include(r => r.Tournament).ThenInclude(t => t.Teams) .Where(r => r.TournamentId == team.TournamentId && r.Region == team.Region) .SingleOrDefaultAsync(); var validator = new TournamentRegistrationRulesValidator(rules); validator.ValidateTeam(team, rules.Tournament.Teams); await SetWoWsPlayersAsync(team.Region, team.Participants); await validator.ValidateDiscordAsync(team, getDiscordUser); // Determine the team's status. For the time being, we accept any valid team that isn't waitlisted var teamCount = await Context.TournamentTeams .Where(t => t.TournamentId == team.TournamentId && t.Region == team.Region) .CountAsync(); if (teamCount < rules.Capacity) { team.Status = TeamStatus.Registered; } else { team.Status = TeamStatus.WaitListed; } team.Created = DateTime.UtcNow; await InitializeParticipantsAsync(rules, team); // Create the team Context.TournamentTeams.Add(team); await Context.SaveChangesAsync(); // Add claims after team is created to get correct ID await AddTeamRepClaims(team); await Context.SaveChangesAsync(); }
/// <summary> /// Edit the generic information about a team /// </summary> /// <param name="team">The new values for the team</param> public async Task EditTeamInfoAsync(TournamentTeam team) { // Normalize team team.Tag = team.Tag.ToUpper(); // TODO don't use find?? var existing = await Context.TournamentTeams .Where(t => t.TeamId == team.TeamId) .AsTracking() .SingleOrDefaultAsync(); if (existing is null) { throw new KeyNotFoundException(); } if (!Authorizer.HasClaim(EditTeamInfo, existing)) { throw new UnauthorizedAccessException(); } // Validate that the registration rules are adhered to var rules = await Context.TournamentRegistrationRules .Include(r => r.Tournament).ThenInclude(t => t.Teams) .Where(r => r.TournamentId == team.TournamentId && r.Region == team.Region) .SingleOrDefaultAsync(); var validator = new TournamentRegistrationRulesValidator(rules); if (!validator.CanEditTeamInfo()) { throw new ArgumentException("Team Info can no longer be adjusted."); } validator.ValidateTeam(team, rules.Tournament.Teams.Where(t => t.TeamId != team.TeamId)); // Update properties on existing team existing.Name = team.Name; existing.Tag = team.Tag; existing.Description = team.Description; existing.Icon = team.Icon; existing.QuestionResponses = team.QuestionResponses; await Context.SaveChangesAsync(); }