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) .Include(r => r.SignUps)
.ThenInclude(s => s.RaidRole) .ThenInclude(s => s.RaidRole)
.Include(r => r.DiscordRaidMessages) .Include(r => r.DiscordRaidMessages)
.ToList() .FirstOrDefault(r => r.RaidId == raidId);
.FirstOrDefault(r => r.RaidId == raidId, new Raid());
if(raid == null) return;
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName); var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);

View file

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

View file

@ -34,9 +34,8 @@ namespace Lieb.Data
public Raid GetRaid(int raidId) public Raid GetRaid(int raidId)
{ {
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();
return context.Raids Raid raid = context.Raids
.Include(r => r.Roles) .Include(r => r.Roles)
.Include(r => r.RaidLogs)
.Include(r => r.Reminders) .Include(r => r.Reminders)
.Include(r => r.SignUps) .Include(r => r.SignUps)
.ThenInclude(s => s.LiebUser) .ThenInclude(s => s.LiebUser)
@ -45,8 +44,9 @@ namespace Lieb.Data
.Include(r => r.SignUps) .Include(r => r.SignUps)
.ThenInclude(s => s.RaidRole) .ThenInclude(s => s.RaidRole)
.Include(r => r.DiscordRaidMessages) .Include(r => r.DiscordRaidMessages)
.ToList() .FirstOrDefault(r => r.RaidId == raidId);
.FirstOrDefault(r => r.RaidId == raidId, new Raid()); 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) public async Task AddOrEditRaid(Raid raid, List<RaidRole> rolesToDelete, List<RaidReminder> remindersToDelete, List<DiscordRaidMessage> messagesToDelete, ulong? changedBy)
@ -111,7 +111,9 @@ namespace Lieb.Data
if(userId != null) if(userId != null)
{ {
LiebUser user = context.LiebUsers.ToList().FirstOrDefault(u => u.Id == userId, new LiebUser()); LiebUser user = context.LiebUsers.FirstOrDefault(u => u.Id == userId);
if(user != null)
{
RaidLog logEntry = new RaidLog() RaidLog logEntry = new RaidLog()
{ {
LogEntry = $"The Raid \"{raid.Title}\" was deleted by {user.Name}", LogEntry = $"The Raid \"{raid.Title}\" was deleted by {user.Name}",
@ -123,6 +125,7 @@ namespace Lieb.Data
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
} }
}
public async Task<bool> SignUp(int raidId, ulong liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType, ulong signedUpByUserId = 0) public async Task<bool> SignUp(int raidId, ulong liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType, ulong signedUpByUserId = 0)
{ {
@ -487,8 +490,9 @@ namespace Lieb.Data
Raid raid = context.Raids Raid raid = context.Raids
.Include(r => r.DiscordRaidMessages) .Include(r => r.DiscordRaidMessages)
.ToList() .FirstOrDefault(r => r.RaidId == signUp.RaidId);
.FirstOrDefault(r => r.RaidId == signUp.RaidId, new Raid());
if(raid == null) return;
if(raid.DiscordRaidMessages.Count > 0) if(raid.DiscordRaidMessages.Count > 0)
{ {
@ -496,9 +500,12 @@ namespace Lieb.Data
if(signedUpBy > 0) if(signedUpBy > 0)
{ {
LiebUser signedUpByUser = context.LiebUsers LiebUser signedUpByUser = context.LiebUsers
.ToList() .FirstOrDefault(u => u.Id == signedUpBy);
.FirstOrDefault(u => u.Id == signedUpBy, new LiebUser());
if(signedUpByUser != null)
signedUpByUserName = signedUpByUser.Name; signedUpByUserName = signedUpByUser.Name;
else
signedUpByUserName = "user not found";
} }
string message = $"{signedUpByUserName} signed up {userName} as {signUp.SignUpType.ToString()}"; 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.Roles)
.Include(r => r.Reminders) .Include(r => r.Reminders)
.Include(r => r.DiscordRaidMessages) .Include(r => r.DiscordRaidMessages)
.Include(r => r.TemplateLogs)
.FirstOrDefault(t => t.RaidTemplateId == raidTemplateId); .FirstOrDefault(t => t.RaidTemplateId == raidTemplateId);
} }
@ -75,7 +74,9 @@ namespace Lieb.Data
context.RaidTemplates.Remove(template); context.RaidTemplates.Remove(template);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
LiebUser user = context.LiebUsers.ToList().FirstOrDefault(u => u.Id == userId, new LiebUser()); LiebUser user = context.LiebUsers.FirstOrDefault(u => u.Id == userId);
if(user != null)
{
RaidLog logEntry = new RaidLog() RaidLog logEntry = new RaidLog()
{ {
LogEntry = $"The Template \"{template.Title}\" was deleted by {user.Name}", LogEntry = $"The Template \"{template.Title}\" was deleted by {user.Name}",
@ -86,6 +87,7 @@ namespace Lieb.Data
context.RaidLogs.Add(logEntry); context.RaidLogs.Add(logEntry);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
}
public async Task CreateRaidFromTemplate(int raidTempalteId) public async Task CreateRaidFromTemplate(int raidTempalteId)
{ {

View file

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

View file

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