Skip to content

Commit

Permalink
Remove ordering penalty
Browse files Browse the repository at this point in the history
  • Loading branch information
Sella-GH committed Dec 30, 2024
1 parent 5f4ca82 commit e0f63bb
Showing 1 changed file with 2 additions and 23 deletions.
25 changes: 2 additions & 23 deletions src/AzzyBot.Data/Services/DbActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public Task<bool> AddAzuraCastAsync(ulong guildId, Uri baseUrl, string apiKey, u
return ExecuteDbActionAsync(async context =>
{
GuildEntity? guild = await context.Guilds
.OrderBy(static g => g.Id)
.FirstOrDefaultAsync(g => g.UniqueId == guildId);

if (guild is null)
Expand Down Expand Up @@ -90,7 +89,6 @@ public Task<bool> AddAzuraCastStationAsync(ulong guildId, int stationId, ulong s
return ExecuteDbActionAsync(async context =>
{
AzuraCastEntity? azura = await context.AzuraCast
.OrderBy(static a => a.Id)
.FirstOrDefaultAsync(a => a.Guild.UniqueId == guildId);

if (azura is null)
Expand Down Expand Up @@ -136,7 +134,6 @@ public Task<bool> AddAzuraCastStationRequestAsync(ulong guildId, int stationId,
return ExecuteDbActionAsync(async context =>
{
AzuraCastStationEntity? station = await context.AzuraCastStations
.OrderBy(static s => s.Id)
.FirstOrDefaultAsync(s => s.AzuraCast.Guild.UniqueId == guildId && s.StationId == stationId);

if (station is null)
Expand Down Expand Up @@ -169,9 +166,7 @@ public async Task<IEnumerable<DiscordGuild>> AddGuildsAsync(IReadOnlyDictionary<
{
ArgumentNullException.ThrowIfNull(guilds);

IEnumerable<GuildEntity> existingGuilds = _dbContext.Guilds
.OrderBy(static g => g.Id);

IEnumerable<GuildEntity> existingGuilds = _dbContext.Guilds;
IEnumerable<GuildEntity> newGuilds = guilds.Keys
.Where(guild => !existingGuilds.Select(static g => g.UniqueId).Contains(guild))
.Select(static guild => new GuildEntity() { UniqueId = guild })
Expand Down Expand Up @@ -213,9 +208,7 @@ public async Task<IEnumerable<ulong>> DeleteGuildsAsync(IReadOnlyDictionary<ulon
{
ArgumentNullException.ThrowIfNull(guilds);

IEnumerable<GuildEntity> existingGuilds = _dbContext.Guilds
.OrderBy(static g => g.Id);

IEnumerable<GuildEntity> existingGuilds = _dbContext.Guilds;
IEnumerable<GuildEntity> guildsToDelete = existingGuilds
.Where(guild => !guilds.Keys.Contains(guild.UniqueId))
.ToList();
Expand All @@ -241,7 +234,6 @@ public async Task<IEnumerable<ulong>> DeleteGuildsAsync(IReadOnlyDictionary<ulon
return _dbContext.AzuraCast
.AsNoTracking()
.Where(a => a.Guild.UniqueId == guildId)
.OrderBy(static a => a.Id)
.IncludeIf(loadChecks, static q => q.Include(static a => a.Checks))
.IncludeIf(loadPrefs, static q => q.Include(static a => a.Preferences))
.IncludeIf(loadStations, static q => q.Include(static a => a.Stations))
Expand All @@ -256,7 +248,6 @@ public async Task<IEnumerable<ulong>> DeleteGuildsAsync(IReadOnlyDictionary<ulon
return _dbContext.AzuraCastStations
.AsNoTracking()
.Where(s => s.AzuraCast.Guild.UniqueId == guildId && s.StationId == stationId)
.OrderBy(static s => s.Id)
.IncludeIf(loadChecks, static q => q.Include(static s => s.Checks))
.IncludeIf(loadPrefs, static q => q.Include(static s => s.Preferences))
.IncludeIf(loadRequests, static q => q.Include(static s => s.Requests))
Expand All @@ -270,7 +261,6 @@ public async Task<IEnumerable<ulong>> DeleteGuildsAsync(IReadOnlyDictionary<ulon
return _dbContext.AzuraCastStationPreferences
.AsNoTracking()
.Where(p => p.Station.AzuraCast.Guild.UniqueId == guildId && p.Station.StationId == stationId)
.OrderBy(static p => p.Id)
.IncludeIf(loadStation, static q => q.Include(static p => p.Station))
.FirstOrDefaultAsync();
}
Expand All @@ -288,7 +278,6 @@ public Task<int> GetAzuraCastStationRequestsCountAsync(ulong guildId, int statio
return _dbContext.Guilds
.AsNoTracking()
.Where(g => g.UniqueId == guildId)
.OrderBy(static g => g.Id)
.IncludeIf(loadEverything, static q => q.Include(static g => g.Preferences))
.IncludeIf(loadEverything, static q => q.Include(static g => g.AzuraCast).Include(static g => g.AzuraCast!.Checks).Include(static g => g.AzuraCast!.Preferences))
.IncludeIf(loadEverything, static q => q.Include(static g => g.AzuraCast!.Stations).ThenInclude(static s => s.Checks))
Expand All @@ -300,7 +289,6 @@ public async Task<IReadOnlyList<GuildEntity>> GetGuildsAsync(bool loadGuildPrefs
{
return await _dbContext.Guilds
.AsNoTracking()
.OrderBy(static g => g.Id)
.IncludeIf(loadGuildPrefs || loadEverything, static q => q.Include(static g => g.Preferences))
.IncludeIf(loadEverything, static q => q.Include(static g => g.AzuraCast).Include(static g => g.AzuraCast!.Checks).Include(static g => g.AzuraCast!.Preferences))
.IncludeIf(loadEverything, static q => q.Include(static g => g.AzuraCast!.Stations).ThenInclude(static s => s.Checks))
Expand All @@ -313,7 +301,6 @@ public async Task<IReadOnlyList<GuildEntity>> GetGuildsAsync(bool loadGuildPrefs
return _dbContext.Guilds
.AsNoTracking()
.Where(g => g.UniqueId == guildId)
.OrderBy(static g => g.Id)
.Select(static g => g.Preferences)
.FirstOrDefaultAsync();
}
Expand All @@ -324,7 +311,6 @@ public Task<bool> UpdateAzuraCastAsync(ulong guildId, Uri? baseUrl = null, strin
{
AzuraCastEntity? azuraCast = await context.AzuraCast
.Where(a => a.Guild.UniqueId == guildId)
.OrderBy(static a => a.Id)
.FirstOrDefaultAsync();

if (azuraCast is null)
Expand Down Expand Up @@ -352,7 +338,6 @@ public Task<bool> UpdateAzuraCastChecksAsync(ulong guildId, bool? serverStatus =
{
AzuraCastChecksEntity? checks = await context.AzuraCastChecks
.Where(c => c.AzuraCast.Guild.UniqueId == guildId)
.OrderBy(static c => c.Id)
.FirstOrDefaultAsync();

if (checks is null)
Expand Down Expand Up @@ -389,7 +374,6 @@ public Task<bool> UpdateAzuraCastPreferencesAsync(ulong guildId, ulong? instance
{
AzuraCastPreferencesEntity? preferences = await context.AzuraCastPreferences
.Where(p => p.AzuraCast.Guild.UniqueId == guildId)
.OrderBy(static p => p.Id)
.FirstOrDefaultAsync();

if (preferences is null)
Expand Down Expand Up @@ -417,7 +401,6 @@ public Task<bool> UpdateAzuraCastStationAsync(ulong guildId, int station, int? s
{
AzuraCastStationEntity? azuraStation = await context.AzuraCastStations
.Where(s => s.AzuraCast.Guild.UniqueId == guildId && s.StationId == station)
.OrderBy(static s => s.Id)
.FirstOrDefaultAsync();

if (azuraStation is null)
Expand Down Expand Up @@ -448,7 +431,6 @@ public Task<bool> UpdateAzuraCastStationChecksAsync(ulong guildId, int stationId
{
AzuraCastStationChecksEntity? checks = await context.AzuraCastStationChecks
.Where(c => c.Station.AzuraCast.Guild.UniqueId == guildId && c.Station.StationId == stationId)
.OrderBy(static c => c.Id)
.FirstOrDefaultAsync();

if (checks is null)
Expand All @@ -473,7 +455,6 @@ public Task<bool> UpdateAzuraCastStationPreferencesAsync(ulong guildId, int stat
{
AzuraCastStationPreferencesEntity? preferences = await context.AzuraCastStationPreferences
.Where(p => p.Station.AzuraCast.Guild.UniqueId == guildId && p.Station.StationId == stationId)
.OrderBy(static p => p.Id)
.FirstOrDefaultAsync();

if (preferences is null)
Expand Down Expand Up @@ -510,7 +491,6 @@ public Task<bool> UpdateGuildAsync(ulong guildId, bool? lastPermissionCheck = nu
{
GuildEntity? guild = await context.Guilds
.Where(g => g.UniqueId == guildId)
.OrderBy(static g => g.Id)
.FirstOrDefaultAsync();

if (guild is null)
Expand Down Expand Up @@ -539,7 +519,6 @@ public Task<bool> UpdateGuildPreferencesAsync(ulong guildId, ulong? adminRoleId
GuildPreferencesEntity? preferences = await context.GuildPreferences
.Where(p => p.Guild.UniqueId == guildId)
.Include(static p => p.Guild)
.OrderBy(static p => p.Id)
.FirstOrDefaultAsync();

if (preferences is null)
Expand Down

0 comments on commit e0f63bb

Please sign in to comment.