mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-23 23:41:27 +00:00
Utilize EF 7 bulk delete operations instead of RemoveRange
This commit is contained in:
parent
48c3384e0c
commit
f03d1d7c17
8 changed files with 17 additions and 21 deletions
|
@ -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);
|
Logger.Info($"User's username has changed, old='{user.Username}', new='{npTicket.Username}', platform={npTicket.Platform}", LogArea.Login);
|
||||||
user.Username = username;
|
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
|
// unlink other platforms because the names no longer match
|
||||||
if (npTicket.Platform == Platform.RPCS3)
|
if (npTicket.Platform == Platform.RPCS3)
|
||||||
user.LinkedPsnId = 0;
|
user.LinkedPsnId = 0;
|
||||||
|
|
|
@ -32,9 +32,8 @@ public class LogoutController : ControllerBase
|
||||||
|
|
||||||
user.LastLogout = TimeHelper.TimestampMillis;
|
user.LastLogout = TimeHelper.TimestampMillis;
|
||||||
|
|
||||||
this.database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId);
|
await this.database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId);
|
||||||
this.database.LastContacts.RemoveWhere(c => c.UserId == token.UserId);
|
await this.database.LastContacts.RemoveWhere(c => c.UserId == token.UserId);
|
||||||
await this.database.SaveChangesAsync();
|
|
||||||
|
|
||||||
return this.Ok();
|
return this.Ok();
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,9 +38,7 @@ public class AuthenticationController : ControllerBase
|
||||||
invalidTokens = new[] { Platform.RPCS3, };
|
invalidTokens = new[] { Platform.RPCS3, };
|
||||||
}
|
}
|
||||||
|
|
||||||
this.database.GameTokens.RemoveWhere(t => t.UserId == user.UserId && invalidTokens.Contains(t.Platform));
|
await this.database.GameTokens.RemoveWhere(t => t.UserId == user.UserId && invalidTokens.Contains(t.Platform));
|
||||||
|
|
||||||
await this.database.SaveChangesAsync();
|
|
||||||
|
|
||||||
return this.Redirect("~/authentication");
|
return this.Redirect("~/authentication");
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,5 +11,5 @@ public class RemoveExpiredTokensTask : IRepeatingTask
|
||||||
public TimeSpan RepeatInterval => TimeSpan.FromHours(1);
|
public TimeSpan RepeatInterval => TimeSpan.FromHours(1);
|
||||||
public DateTime LastRan { get; set; }
|
public DateTime LastRan { get; set; }
|
||||||
|
|
||||||
public Task Run(DatabaseContext database) => database.RemoveExpiredTokens();
|
public async Task Run(DatabaseContext database) => await database.RemoveExpiredTokens();
|
||||||
}
|
}
|
|
@ -170,9 +170,7 @@ public partial class DatabaseContext
|
||||||
{
|
{
|
||||||
if (userId == blockedUser.UserId) return;
|
if (userId == blockedUser.UserId) return;
|
||||||
|
|
||||||
this.BlockedProfiles.RemoveWhere(bp => bp.BlockedUserId == blockedUser.UserId && bp.UserId == userId);
|
await this.BlockedProfiles.RemoveWhere(bp => bp.BlockedUserId == blockedUser.UserId && bp.UserId == userId);
|
||||||
|
|
||||||
await this.SaveChangesAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> IsUserBlockedBy(int userId, int targetId)
|
public async Task<bool> IsUserBlockedBy(int userId, int targetId)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#nullable enable
|
#nullable enable
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using LBPUnion.ProjectLighthouse.Extensions;
|
using LBPUnion.ProjectLighthouse.Extensions;
|
||||||
|
@ -91,18 +92,18 @@ public partial class DatabaseContext
|
||||||
|
|
||||||
public async Task RemoveExpiredTokens()
|
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);
|
UserEntity? user = await this.Users.FirstOrDefaultAsync(u => u.UserId == token.UserId);
|
||||||
if (user != null) user.LastLogout = TimeHelper.TimestampMillis;
|
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.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)
|
public async Task RemoveRegistrationToken(string? tokenString)
|
||||||
|
|
|
@ -55,6 +55,6 @@ public static class DatabaseExtensions
|
||||||
public static async Task<bool> Has<T>(this IQueryable<T> queryable, Expression<Func<T, bool>> predicate)
|
public static async Task<bool> Has<T>(this IQueryable<T> queryable, Expression<Func<T, bool>> predicate)
|
||||||
=> await queryable.FirstOrDefaultAsync(predicate) != null;
|
=> await queryable.FirstOrDefaultAsync(predicate) != null;
|
||||||
|
|
||||||
public static void RemoveWhere<T>(this DbSet<T> queryable, Expression<Func<T, bool>> predicate) where T : class
|
public static async Task RemoveWhere<T>(this DbSet<T> queryable, Expression<Func<T, bool>> predicate) where T : class
|
||||||
=> queryable.RemoveRange(queryable.Where(predicate));
|
=> await queryable.Where(predicate).ExecuteDeleteAsync();
|
||||||
}
|
}
|
|
@ -51,7 +51,7 @@ public partial class SMTPHelper
|
||||||
string? existingToken = await database.EmailVerificationTokens.Where(v => v.UserId == user.UserId)
|
string? existingToken = await database.EmailVerificationTokens.Where(v => v.UserId == user.UserId)
|
||||||
.Select(v => v.EmailToken)
|
.Select(v => v.EmailToken)
|
||||||
.FirstOrDefaultAsync();
|
.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()
|
EmailVerificationTokenEntity verifyToken = new()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue