From c4219a8d8b97b96dd73ddb0008294fb89cd7edef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vegard=20L=C3=B8kken?= Date: Tue, 3 Sep 2024 10:53:55 +0200 Subject: [PATCH] Add dev tool for clearing challenge winner Don't think this should be supported as a common feature, so for now it's only possible in dev mode. --- Tevling/Components/DevTools.razor | 4 ++++ Tevling/Components/DevTools.razor.cs | 9 +++++++++ Tevling/Services/ChallengeService.cs | 25 +++++-------------------- Tevling/Services/IChallengeService.cs | 3 +++ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Tevling/Components/DevTools.razor b/Tevling/Components/DevTools.razor index 1ac015b..bc436dc 100644 --- a/Tevling/Components/DevTools.razor +++ b/Tevling/Components/DevTools.razor @@ -8,4 +8,8 @@ +
+ + +
diff --git a/Tevling/Components/DevTools.razor.cs b/Tevling/Components/DevTools.razor.cs index 446bed4..9b9af15 100644 --- a/Tevling/Components/DevTools.razor.cs +++ b/Tevling/Components/DevTools.razor.cs @@ -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() { @@ -137,4 +138,12 @@ await ChallengeService.CreateChallengeAsync(new ChallengeFormModel CreatedBy = Athlete.Id }); } + + private async Task ClearChallengeWinner() + { + if (ClearChallengeWinnerId > 0) + { + await ChallengeService.ClearChallengeWinnerAsync(ClearChallengeWinnerId); + } + } } diff --git a/Tevling/Services/ChallengeService.cs b/Tevling/Services/ChallengeService.cs index 7b657eb..e984b13 100644 --- a/Tevling/Services/ChallengeService.cs +++ b/Tevling/Services/ChallengeService.cs @@ -342,29 +342,14 @@ operation which EFCore requires. return null; } - public async Task 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 { Item = challenge, Action = FeedAction.Update }); - - return challenge; + challenge.WinnerId = null; + await dataContext.UpdateChallengeAsync(challenge, CancellationToken.None); } } diff --git a/Tevling/Services/IChallengeService.cs b/Tevling/Services/IChallengeService.cs index 7170ac5..bb920f5 100644 --- a/Tevling/Services/IChallengeService.cs +++ b/Tevling/Services/IChallengeService.cs @@ -21,5 +21,8 @@ Task UpdateChallengeAsync(int challengeId, ChallengeFormModel editCha Task DeleteChallengeAsync(int challengeId, CancellationToken ct = default); Task GetScoreBoardAsync(int challengeId, CancellationToken ct = default); + Task DrawChallengeWinnerAsync(int challengeId, CancellationToken ct = default); + + Task ClearChallengeWinnerAsync(int challengeId, CancellationToken ct = default); }