Skip to content

Commit

Permalink
Remove unnecessary DB calls in service method
Browse files Browse the repository at this point in the history
  • Loading branch information
zysim authored Oct 23, 2024
1 parent 68e35b6 commit d86c90a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions LeaderboardBackend/Services/Impl/LeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<CreateLeaderboardResult> CreateLeaderboard(CreateLeaderboardRe
public async Task<RestoreLeaderboardResult> RestoreLeaderboard(long id)
{
Leaderboard? lb = await applicationContext.Leaderboards.FindAsync(id);

if (lb == null)
{
return new LeaderboardNotFound();
Expand All @@ -58,16 +58,18 @@ public async Task<RestoreLeaderboardResult> RestoreLeaderboard(long id)
return new LeaderboardNeverDeleted();
}

Leaderboard? maybe = await applicationContext.Leaderboards.SingleOrDefaultAsync(board => board.Slug == lb.Slug && board.DeletedAt == null);
lb.DeletedAt = null;

if (maybe != null)
try
{
return new RestoreLeaderboardConflict(maybe);
await applicationContext.SaveChangesAsync();
}
catch (DbUpdateException e)
when(e.InnerException is PostgresException { SqlState: PostgresErrorCodes.UniqueViolation } pgEx)
{
Leaderboard conflict = await applicationContext.Leaderboards.SingleAsync(c => c.Slug == lb.Slug && c.DeletedAt == null);
return new RestoreLeaderboardConflict(conflict);
}

lb.DeletedAt = null;

await applicationContext.SaveChangesAsync();

return lb;
}
Expand Down

0 comments on commit d86c90a

Please sign in to comment.