From a4c29d88913507f9728cb8c147ee763c400bc76f Mon Sep 17 00:00:00 2001 From: Sarah Faey Date: Mon, 5 Dec 2022 11:55:21 +0100 Subject: [PATCH] removed defaults from FirstOrDefault to avoid reading everything with ToList --- Lieb/Data/DiscordService.cs | 5 +-- Lieb/Data/RaidRandomizerService.cs | 1 - Lieb/Data/RaidService.cs | 43 ++++++++++++++----------- Lieb/Data/RaidTemplateService.cs | 22 +++++++------ Lieb/Data/UserService.cs | 14 +++++--- Lieb/Models/GuildWars2/Raid/RaidBase.cs | 1 + 6 files changed, 50 insertions(+), 36 deletions(-) diff --git a/Lieb/Data/DiscordService.cs b/Lieb/Data/DiscordService.cs index 61ac9ac..78106bc 100644 --- a/Lieb/Data/DiscordService.cs +++ b/Lieb/Data/DiscordService.cs @@ -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); diff --git a/Lieb/Data/RaidRandomizerService.cs b/Lieb/Data/RaidRandomizerService.cs index 190f39f..7269e4d 100644 --- a/Lieb/Data/RaidRandomizerService.cs +++ b/Lieb/Data/RaidRandomizerService.cs @@ -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) diff --git a/Lieb/Data/RaidService.cs b/Lieb/Data/RaidService.cs index 8026187..b3ad173 100644 --- a/Lieb/Data/RaidService.cs +++ b/Lieb/Data/RaidService.cs @@ -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 rolesToDelete, List remindersToDelete, List 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()}"; diff --git a/Lieb/Data/RaidTemplateService.cs b/Lieb/Data/RaidTemplateService.cs index 9bb93ba..8e66b1b 100644 --- a/Lieb/Data/RaidTemplateService.cs +++ b/Lieb/Data/RaidTemplateService.cs @@ -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) diff --git a/Lieb/Data/UserService.cs b/Lieb/Data/UserService.cs index 534b23b..9f6e393 100644 --- a/Lieb/Data/UserService.cs +++ b/Lieb/Data/UserService.cs @@ -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(); List 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 usableAccounts = GetAllUsableAccounts(user, raid.RaidType); diff --git a/Lieb/Models/GuildWars2/Raid/RaidBase.cs b/Lieb/Models/GuildWars2/Raid/RaidBase.cs index 78c24f9..334e240 100644 --- a/Lieb/Models/GuildWars2/Raid/RaidBase.cs +++ b/Lieb/Models/GuildWars2/Raid/RaidBase.cs @@ -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) {