removed defaults from FirstOrDefault to avoid reading everything with ToList

This commit is contained in:
Sarah Faey 2022-12-05 11:55:21 +01:00
parent 24ad2b11e5
commit a4c29d8891
6 changed files with 50 additions and 36 deletions

View file

@ -74,8 +74,9 @@ namespace Lieb.Data
.Include(r => r.SignUps)
.ThenInclude(s => s.RaidRole)
.Include(r => r.DiscordRaidMessages)
.ToList()
.FirstOrDefault(r => r.RaidId == raidId, new Raid());
.FirstOrDefault(r => r.RaidId == raidId);
if(raid == null) return;
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);

View file

@ -19,7 +19,6 @@ namespace Lieb.Data
using var context = _contextFactory.CreateDbContext();
Raid? raid = context.Raids
.Include(r => r.Roles)
.Include(r => r.RaidLogs)
.Include(r => r.Reminders)
.Include(r => r.SignUps)
.ThenInclude(s => s.LiebUser)

View file

@ -34,9 +34,8 @@ namespace Lieb.Data
public Raid GetRaid(int raidId)
{
using var context = _contextFactory.CreateDbContext();
return context.Raids
Raid raid = context.Raids
.Include(r => r.Roles)
.Include(r => r.RaidLogs)
.Include(r => r.Reminders)
.Include(r => r.SignUps)
.ThenInclude(s => s.LiebUser)
@ -45,8 +44,9 @@ namespace Lieb.Data
.Include(r => r.SignUps)
.ThenInclude(s => s.RaidRole)
.Include(r => r.DiscordRaidMessages)
.ToList()
.FirstOrDefault(r => r.RaidId == raidId, new Raid());
.FirstOrDefault(r => r.RaidId == raidId);
if(raid != null) return raid;
else return new Raid();
}
public async Task AddOrEditRaid(Raid raid, List<RaidRole> rolesToDelete, List<RaidReminder> remindersToDelete, List<DiscordRaidMessage> messagesToDelete, ulong? changedBy)
@ -111,16 +111,19 @@ namespace Lieb.Data
if(userId != null)
{
LiebUser user = context.LiebUsers.ToList().FirstOrDefault(u => u.Id == userId, new LiebUser());
RaidLog logEntry = new RaidLog()
LiebUser user = context.LiebUsers.FirstOrDefault(u => u.Id == userId);
if(user != null)
{
LogEntry = $"The Raid \"{raid.Title}\" was deleted by {user.Name}",
Time = DateTimeOffset.UtcNow,
Type = RaidLog.LogType.Raid,
UserId = userId
};
context.RaidLogs.Add(logEntry);
await context.SaveChangesAsync();
RaidLog logEntry = new RaidLog()
{
LogEntry = $"The Raid \"{raid.Title}\" was deleted by {user.Name}",
Time = DateTimeOffset.UtcNow,
Type = RaidLog.LogType.Raid,
UserId = userId
};
context.RaidLogs.Add(logEntry);
await context.SaveChangesAsync();
}
}
}
@ -487,8 +490,9 @@ namespace Lieb.Data
Raid raid = context.Raids
.Include(r => r.DiscordRaidMessages)
.ToList()
.FirstOrDefault(r => r.RaidId == signUp.RaidId, new Raid());
.FirstOrDefault(r => r.RaidId == signUp.RaidId);
if(raid == null) return;
if(raid.DiscordRaidMessages.Count > 0)
{
@ -496,9 +500,12 @@ namespace Lieb.Data
if(signedUpBy > 0)
{
LiebUser signedUpByUser = context.LiebUsers
.ToList()
.FirstOrDefault(u => u.Id == signedUpBy, new LiebUser());
signedUpByUserName = signedUpByUser.Name;
.FirstOrDefault(u => u.Id == signedUpBy);
if(signedUpByUser != null)
signedUpByUserName = signedUpByUser.Name;
else
signedUpByUserName = "user not found";
}
string message = $"{signedUpByUserName} signed up {userName} as {signUp.SignUpType.ToString()}";

View file

@ -32,7 +32,6 @@ namespace Lieb.Data
.Include(r => r.Roles)
.Include(r => r.Reminders)
.Include(r => r.DiscordRaidMessages)
.Include(r => r.TemplateLogs)
.FirstOrDefault(t => t.RaidTemplateId == raidTemplateId);
}
@ -75,16 +74,19 @@ namespace Lieb.Data
context.RaidTemplates.Remove(template);
await context.SaveChangesAsync();
LiebUser user = context.LiebUsers.ToList().FirstOrDefault(u => u.Id == userId, new LiebUser());
RaidLog logEntry = new RaidLog()
LiebUser user = context.LiebUsers.FirstOrDefault(u => u.Id == userId);
if(user != null)
{
LogEntry = $"The Template \"{template.Title}\" was deleted by {user.Name}",
Time = DateTimeOffset.UtcNow,
Type = RaidLog.LogType.RaidTemplate,
UserId = userId
};
context.RaidLogs.Add(logEntry);
await context.SaveChangesAsync();
RaidLog logEntry = new RaidLog()
{
LogEntry = $"The Template \"{template.Title}\" was deleted by {user.Name}",
Time = DateTimeOffset.UtcNow,
Type = RaidLog.LogType.RaidTemplate,
UserId = userId
};
context.RaidLogs.Add(logEntry);
await context.SaveChangesAsync();
}
}
public async Task CreateRaidFromTemplate(int raidTempalteId)

View file

@ -66,8 +66,10 @@ namespace Lieb.Data
using var context = _contextFactory.CreateDbContext();
LiebUser user = context.LiebUsers
.Include(u => u.GuildWars2Accounts)
.ToList()
.FirstOrDefault(u => u.Id == userId, new LiebUser());
if(user == null) return new GuildWars2Account();
return user.GuildWars2Accounts.FirstOrDefault(g => g.GuildWars2AccountId == user.MainGW2Account, new GuildWars2Account());
}
@ -284,8 +286,9 @@ namespace Lieb.Data
using var context = _contextFactory.CreateDbContext();
LiebUser user = GetLiebUserGW2AccountOnly(userId);
Raid raid = context.Raids
.ToList()
.FirstOrDefault(r => r.RaidId == raidId, new Raid());
.FirstOrDefault(r => r.RaidId == raidId);
if(raid == null) return new List<GuildWars2Account>();
List<GuildWars2Account> accounts = GetAllUsableAccounts(user, raid.RaidType);
if(user.AlwaysSignUpWithMainAccount && accounts.Where(a => a.GuildWars2AccountId == user.MainGW2Account).Any())
@ -303,8 +306,9 @@ namespace Lieb.Data
using var context = _contextFactory.CreateDbContext();
LiebUser user = GetLiebUserGW2AccountOnly(userId);
Raid raid = context.Raids
.ToList()
.FirstOrDefault(r => r.RaidId == raidId, new Raid());
.FirstOrDefault(r => r.RaidId == raidId);
if(raid == null) return 0;
List<GuildWars2Account> usableAccounts = GetAllUsableAccounts(user, raid.RaidType);

View file

@ -72,6 +72,7 @@ namespace Lieb.Models.GuildWars2.Raid
this.RequiredRole = template.RequiredRole;
this.MoveFlexUsers = template.MoveFlexUsers;
this.RaidOwnerId = template.RaidOwnerId;
this.EventType = template.EventType;
foreach (RaidRole role in template.Roles)
{