Utilize EF 7 bulk delete operations instead of RemoveRange

This commit is contained in:
Slendy 2023-04-18 20:45:42 -05:00
parent 48c3384e0c
commit f03d1d7c17
No known key found for this signature in database
GPG key ID: 7288D68361B91428
8 changed files with 17 additions and 21 deletions

View file

@ -164,7 +164,7 @@ public class LoginController : ControllerBase
}
Logger.Info($"User's username has changed, old='{user.Username}', new='{npTicket.Username}', platform={npTicket.Platform}", LogArea.Login);
user.Username = username;
this.database.PlatformLinkAttempts.RemoveWhere(p => p.UserId == user.UserId);
await this.database.PlatformLinkAttempts.RemoveWhere(p => p.UserId == user.UserId);
// unlink other platforms because the names no longer match
if (npTicket.Platform == Platform.RPCS3)
user.LinkedPsnId = 0;

View file

@ -32,9 +32,8 @@ public class LogoutController : ControllerBase
user.LastLogout = TimeHelper.TimestampMillis;
this.database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId);
this.database.LastContacts.RemoveWhere(c => c.UserId == token.UserId);
await this.database.SaveChangesAsync();
await this.database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId);
await this.database.LastContacts.RemoveWhere(c => c.UserId == token.UserId);
return this.Ok();
}

View file

@ -38,9 +38,7 @@ public class AuthenticationController : ControllerBase
invalidTokens = new[] { Platform.RPCS3, };
}
this.database.GameTokens.RemoveWhere(t => t.UserId == user.UserId && invalidTokens.Contains(t.Platform));
await this.database.SaveChangesAsync();
await this.database.GameTokens.RemoveWhere(t => t.UserId == user.UserId && invalidTokens.Contains(t.Platform));
return this.Redirect("~/authentication");
}

View file

@ -11,5 +11,5 @@ public class RemoveExpiredTokensTask : IRepeatingTask
public TimeSpan RepeatInterval => TimeSpan.FromHours(1);
public DateTime LastRan { get; set; }
public Task Run(DatabaseContext database) => database.RemoveExpiredTokens();
public async Task Run(DatabaseContext database) => await database.RemoveExpiredTokens();
}

View file

@ -170,9 +170,7 @@ public partial class DatabaseContext
{
if (userId == blockedUser.UserId) return;
this.BlockedProfiles.RemoveWhere(bp => bp.BlockedUserId == blockedUser.UserId && bp.UserId == userId);
await this.SaveChangesAsync();
await this.BlockedProfiles.RemoveWhere(bp => bp.BlockedUserId == blockedUser.UserId && bp.UserId == userId);
}
public async Task<bool> IsUserBlockedBy(int userId, int targetId)

View file

@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Extensions;
@ -91,18 +92,18 @@ public partial class DatabaseContext
public async Task RemoveExpiredTokens()
{
foreach (GameTokenEntity token in await this.GameTokens.Where(t => DateTime.Now > t.ExpiresAt).ToListAsync())
List<GameTokenEntity> expiredTokens = await this.GameTokens.Where(t => DateTime.Now > t.ExpiresAt).ToListAsync();
foreach (GameTokenEntity token in expiredTokens)
{
UserEntity? user = await this.Users.FirstOrDefaultAsync(u => u.UserId == token.UserId);
if (user != null) user.LastLogout = TimeHelper.TimestampMillis;
this.GameTokens.Remove(token);
}
this.WebTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt);
this.EmailVerificationTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt);
this.EmailSetTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt);
await this.SaveChangesAsync();
await this.GameTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt);
await this.WebTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt);
await this.EmailVerificationTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt);
await this.EmailSetTokens.RemoveWhere(t => DateTime.Now > t.ExpiresAt);
}
public async Task RemoveRegistrationToken(string? tokenString)

View file

@ -55,6 +55,6 @@ public static class DatabaseExtensions
public static async Task<bool> Has<T>(this IQueryable<T> queryable, Expression<Func<T, bool>> predicate)
=> await queryable.FirstOrDefaultAsync(predicate) != null;
public static void RemoveWhere<T>(this DbSet<T> queryable, Expression<Func<T, bool>> predicate) where T : class
=> queryable.RemoveRange(queryable.Where(predicate));
public static async Task RemoveWhere<T>(this DbSet<T> queryable, Expression<Func<T, bool>> predicate) where T : class
=> await queryable.Where(predicate).ExecuteDeleteAsync();
}

View file

@ -51,7 +51,7 @@ public partial class SMTPHelper
string? existingToken = await database.EmailVerificationTokens.Where(v => v.UserId == user.UserId)
.Select(v => v.EmailToken)
.FirstOrDefaultAsync();
if (existingToken != null) database.EmailVerificationTokens.RemoveWhere(t => t.EmailToken == existingToken);
if (existingToken != null) await database.EmailVerificationTokens.RemoveWhere(t => t.EmailToken == existingToken);
EmailVerificationTokenEntity verifyToken = new()
{