Skip to content

Commit

Permalink
Fix invited athletes in edited challenges
Browse files Browse the repository at this point in the history
The list of invited athletes was not updated when editing a challenge.
This is now added, but I had to remove the challenge creator from the
search result when inviting athletes as it would break some tracking
rules in EF Core. This is not a problem though and perhaps it's better
this way. The creator will always see the challenge (and is able to join
it) anyways, so inviting yourself never made much sense.
  • Loading branch information
veloek committed Feb 29, 2024
1 parent 6ed1f64 commit d01c1b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Tevling/Components/ChallengeForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ protected override void OnParametersSet()

private async Task<IEnumerable<Athlete>> SearchAthletes(string searchText)
{
AthleteFilter filter = new() { SearchText = searchText, NotIn = Challenge.InvitedAthletes?.Select(a => a.Id) };
AthleteFilter filter = new()
{
SearchText = searchText,
NotIn = Challenge.InvitedAthletes?.Select(a => a.Id).Append(Athlete.Id)
};
Athlete[] result = await AthleteService.GetAthletesAsync(filter, new Paging(MaximumSuggestions));

return result;
Expand Down
15 changes: 15 additions & 0 deletions Tevling/Services/ChallengeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,27 @@ public async Task<Challenge> UpdateChallengeAsync(int challengeId, ChallengeForm
Challenge challenge = await dataContext.Challenges
.Include(c => c.Athletes)
.Include(c => c.CreatedBy)
.Include(c => c.InvitedAthletes)
.AsSplitQuery()
.AsTracking()
.FirstOrDefaultAsync(c => c.Id == challengeId, ct)
?? throw new Exception($"Unknown challenge ID {challengeId}");

_logger.LogInformation("Updating challenge ID {ChallengeId}", challengeId);

if (editChallenge.InvitedAthletes != null)
{
bool stillInvited(Athlete a) => editChallenge.InvitedAthletes.Any(i => i.Id == a.Id);

IEnumerable<Athlete> newInvites = editChallenge.InvitedAthletes
.Where(i => !challenge.InvitedAthletes!.Any(ii => ii.Id == i.Id));

challenge.InvitedAthletes = challenge.InvitedAthletes!
.Where(stillInvited)
.Concat(newInvites)
.ToList();
}

challenge.Title = editChallenge.Title;
challenge.Description = editChallenge.Description;
challenge.Start = editChallenge.Start;
Expand Down

0 comments on commit d01c1b6

Please sign in to comment.