Skip to content

Commit

Permalink
perf: improve flags addition and removal performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed Nov 28, 2024
1 parent 36482b3 commit 4ca568b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/GZCTF/Controllers/EditController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,6 @@ public async Task<IActionResult> AddFlags([FromRoute] int id, [FromRoute] int cI
return NotFound(new RequestResponse(localizer[nameof(Resources.Program.Challenge_NotFound)],
StatusCodes.Status404NotFound));

await challengeRepository.LoadFlags(challenge, token);
await challengeRepository.AddFlags(challenge, models, token);

return Ok();
Expand Down Expand Up @@ -864,7 +863,6 @@ public async Task<IActionResult> RemoveFlag([FromRoute] int id, [FromRoute] int
return NotFound(new RequestResponse(localizer[nameof(Resources.Program.Challenge_NotFound)],
StatusCodes.Status404NotFound));

await challengeRepository.LoadFlags(challenge, token);
return Ok(await challengeRepository.RemoveFlag(challenge, fId, token));
}
}
12 changes: 8 additions & 4 deletions src/GZCTF/Repositories/GameChallengeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public async Task RemoveChallenge(GameChallenge challenge, bool save = true, Can

public async Task<TaskStatus> RemoveFlag(GameChallenge challenge, int flagId, CancellationToken token = default)
{
FlagContext? flag = challenge.Flags.FirstOrDefault(f => f.Id == flagId);
FlagContext? flag = await Context.FlagContexts
.FirstOrDefaultAsync(f => f.Challenge == challenge && f.Id == flagId, token);

if (flag is null)
return TaskStatus.NotFound;
Expand All @@ -81,11 +82,14 @@ public async Task<TaskStatus> RemoveFlag(GameChallenge challenge, int flagId, Ca

Context.Remove(flag);

if (challenge.Flags.Count == 0)
challenge.IsEnabled = false;

await SaveAsync(token);

if (await Context.FlagContexts.CountAsync(f => f.Challenge == challenge, token) == 0)
{
challenge.IsEnabled = false;
await SaveAsync(token);
}

return TaskStatus.Success;
}

Expand Down
2 changes: 1 addition & 1 deletion src/GZCTF/Repositories/GameRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Task<TaskStatus> DeleteGame(Game game, CancellationToken token = de
LogLevel.Debug
);

foreach (GameChallenge chal in await Context.GameChallenges.Include(c => c.Flags).Where(c => c.Game == game)
foreach (GameChallenge chal in await Context.GameChallenges.Where(c => c.Game == game)
.ToArrayAsync(token))
await challengeRepository.RemoveChallenge(chal, false, token);

Expand Down

0 comments on commit 4ca568b

Please sign in to comment.