Skip to content

Commit

Permalink
Add dev tool for clearing challenge winner
Browse files Browse the repository at this point in the history
Don't think this should be supported as a common feature, so for now
it's only possible in dev mode.
  • Loading branch information
veloek committed Sep 3, 2024
1 parent 6ec2711 commit c4219a8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Tevling/Components/DevTools.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
<button class="btn btn-primary" @onclick="AddOthersPrivateChallengeInvited" disabled="@NoOtherAthletes">Add private challenge by others where you're invited</button>
<button class="btn btn-primary" @onclick="() => ImportAllAthletesActivities(7)" disabled="@NoOtherAthletes">Import last 7 days of activities for ALL athletes</button>
<button class="btn btn-primary" @onclick="() => ImportAllAthletesActivities(14)" disabled="@NoOtherAthletes">Import last 14 days of activities for ALL athletes</button>
<div class="d-flex flex-row cg-3">
<input class="form-control" type="text" @bind="ClearChallengeWinnerId" placeholder="Challenge ID" style="width:150px" />
<button class="btn btn-primary" @onclick="ClearChallengeWinner">Clear challenge winner</button>
</div>
</div>
9 changes: 9 additions & 0 deletions Tevling/Components/DevTools.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public partial class DevTools : ComponentBase

private Athlete[] _athletes = [];
private bool NoOtherAthletes => _athletes.Length == 1;
private int ClearChallengeWinnerId { get; set; }

protected override async Task OnInitializedAsync()
{
Expand Down Expand Up @@ -137,4 +138,12 @@ await ChallengeService.CreateChallengeAsync(new ChallengeFormModel
CreatedBy = Athlete.Id
});
}

private async Task ClearChallengeWinner()
{
if (ClearChallengeWinnerId > 0)
{
await ChallengeService.ClearChallengeWinnerAsync(ClearChallengeWinnerId);
}
}
}
25 changes: 5 additions & 20 deletions Tevling/Services/ChallengeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,29 +342,14 @@ operation which EFCore requires.
return null;
}

public async Task<Challenge> InviteAthleteAsync(int athleteId, int challengeId, CancellationToken ct = default)
public async Task ClearChallengeWinnerAsync(int challengeId, CancellationToken ct = default)
{
using DataContext dataContext = await _dataContextFactory.CreateDbContextAsync(ct);

Challenge challenge = await dataContext.Challenges
.Include(c => c.Athletes)
.Include(c => c.CreatedBy)
.Include(c => c.InvitedAthletes)
.AsTracking()
.FirstOrDefaultAsync(c => c.Id == challengeId, ct)
?? throw new Exception($"Challenge ID {challengeId} not found");

Athlete athlete = await dataContext.Athletes
.AsTracking()
.FirstOrDefaultAsync(a => a.Id == athleteId, ct)
?? throw new Exception($"Athlete ID {athleteId} not found");
Challenge? challenge = await dataContext.Challenges.FirstOrDefaultAsync(c => c.Id == challengeId, ct);
if (challenge == null) return;

challenge.InvitedAthletes!.Add(athlete);

await dataContext.UpdateChallengeAsync(challenge, ct);

_challengeFeed.OnNext(new FeedUpdate<Challenge> { Item = challenge, Action = FeedAction.Update });

return challenge;
challenge.WinnerId = null;
await dataContext.UpdateChallengeAsync(challenge, CancellationToken.None);
}
}
3 changes: 3 additions & 0 deletions Tevling/Services/IChallengeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ Task<Challenge> UpdateChallengeAsync(int challengeId, ChallengeFormModel editCha
Task DeleteChallengeAsync(int challengeId, CancellationToken ct = default);

Task<ScoreBoard> GetScoreBoardAsync(int challengeId, CancellationToken ct = default);

Task<Athlete?> DrawChallengeWinnerAsync(int challengeId, CancellationToken ct = default);

Task ClearChallengeWinnerAsync(int challengeId, CancellationToken ct = default);
}

0 comments on commit c4219a8

Please sign in to comment.