update for database provider specific migrations

fix issues with live radar
This commit is contained in:
RaidMax
2020-11-27 21:52:52 -06:00
parent 8ef2959f63
commit 36a02b3d7b
309 changed files with 76554 additions and 1067 deletions

View File

@ -1,65 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Database;
using Microsoft.EntityFrameworkCore;
namespace SharedLibraryCore.Services
{
public class AliasService : IEntityService<EFAlias>
{
public async Task<EFAlias> Create(EFAlias entity)
{
throw await Task.FromResult(new Exception());
}
public async Task<EFAlias> Delete(EFAlias entity)
{
using (var context = new DatabaseContext())
{
var alias = context.Aliases
.Single(e => e.AliasId == entity.AliasId);
alias.Active = false;
await context.SaveChangesAsync();
return entity;
}
}
public async Task<IList<EFAlias>> Find(Func<EFAlias, bool> expression)
{
// todo: max better?
return await Task.Run(() =>
{
using (var context = new DatabaseContext(true))
return context.Aliases
.AsNoTracking()
.Include(a => a.Link.Children)
.Where(expression)
.ToList();
});
}
public async Task<EFAlias> Get(int entityID)
{
using (var context = new DatabaseContext(true))
return await context.Aliases
.FirstOrDefaultAsync(e => e.AliasId == entityID);
}
public Task<EFAlias> GetUnique(long entityProperty)
{
throw new NotImplementedException();
}
public async Task<EFAlias> Update(EFAlias entity)
{
throw await Task.FromResult(new Exception());
}
}
}

View File

@ -10,21 +10,18 @@ using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace SharedLibraryCore.Services
{
public class ChangeHistoryService : IEntityService<EFChangeHistory>
public class ChangeHistoryService
{
private readonly ILogger _logger;
private readonly IDatabaseContextFactory _contextFactory;
public ChangeHistoryService(ILogger<ChangeHistoryService> logger)
public ChangeHistoryService(ILogger<ChangeHistoryService> logger, IDatabaseContextFactory contextFactory)
{
_logger = logger;
}
public Task<EFChangeHistory> Create(EFChangeHistory entity)
{
throw new NotImplementedException();
_contextFactory = contextFactory;
}
public async Task<EFChangeHistory> Add(GameEvent e, DatabaseContext ctx = null)
public async Task Add(GameEvent e)
{
EFChangeHistory change = null;
@ -74,58 +71,24 @@ namespace SharedLibraryCore.Services
break;
}
if (change != null)
if (change == null)
{
bool existingCtx = ctx != null;
ctx = ctx ?? new DatabaseContext(true);
ctx.EFChangeHistory.Add(change);
try
{
await ctx.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not persist change @{change}", change);
}
finally
{
if (!existingCtx)
{
ctx.Dispose();
}
}
return;
}
return change;
}
await using var context = _contextFactory.CreateContext(false);
public Task<EFChangeHistory> Delete(EFChangeHistory entity)
{
throw new NotImplementedException();
}
context.EFChangeHistory.Add(change);
public Task<IList<EFChangeHistory>> Find(Func<EFChangeHistory, bool> expression)
{
throw new NotImplementedException();
}
try
{
await context.SaveChangesAsync();
}
public Task<EFChangeHistory> Get(int entityID)
{
throw new NotImplementedException();
}
public Task<EFChangeHistory> GetUnique(long entityProperty)
{
throw new NotImplementedException();
}
public Task<EFChangeHistory> Update(EFChangeHistory entity)
{
throw new NotImplementedException();
catch (Exception ex)
{
_logger.LogError(ex, "Could not persist change @{change}", change);
}
}
}
}

View File

@ -7,6 +7,7 @@ using SharedLibraryCore.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Serilog.Context;
@ -28,7 +29,7 @@ namespace SharedLibraryCore.Services
public async Task<EFClient> Create(EFClient entity)
{
using (var context = new DatabaseContext())
await using var context = _contextFactory.CreateContext(true);
using (LogContext.PushProperty("Server", entity?.CurrentServer?.ToString()))
{
int? linkId = null;
@ -67,7 +68,7 @@ namespace SharedLibraryCore.Services
};
_logger.LogDebug("[create] adding {entity} to context", entity.ToString());
context.Clients.Add(client);
// they're just using a new GUID
if (aliasId.HasValue)
@ -106,12 +107,11 @@ namespace SharedLibraryCore.Services
Link = link
};
link.Children.Add(alias);
client.AliasLink = link;
client.CurrentAlias = alias;
}
context.Clients.Add(client);
await context.SaveChangesAsync();
return client;
@ -266,40 +266,38 @@ namespace SharedLibraryCore.Services
/// <returns></returns>
public virtual async Task UpdateLevel(Permission newPermission, EFClient temporalClient, EFClient origin)
{
using (var ctx = new DatabaseContext())
await using var ctx = _contextFactory.CreateContext(true);
var entity = await ctx.Clients
.Where(_client => _client.ClientId == temporalClient.ClientId)
.FirstAsync();
var oldPermission = entity.Level;
entity.Level = newPermission;
await ctx.SaveChangesAsync();
using (LogContext.PushProperty("Server", entity?.CurrentServer?.ToString()))
{
var entity = await ctx.Clients
.Where(_client => _client.ClientId == temporalClient.ClientId)
.FirstAsync();
_logger.LogInformation("Updated {clientId} to {newPermission}", temporalClient.ClientId, newPermission);
var oldPermission = entity.Level;
entity.Level = newPermission;
await ctx.SaveChangesAsync();
using (LogContext.PushProperty("Server", entity?.CurrentServer?.ToString()))
var linkedPermissionSet = new[] {Permission.Banned, Permission.Flagged};
// if their permission level has been changed to level that needs to be updated on all accounts
if (linkedPermissionSet.Contains(newPermission) || linkedPermissionSet.Contains(oldPermission))
{
_logger.LogInformation("Updated {clientId} to {newPermission}", temporalClient.ClientId, newPermission);
//get all clients that have the same linkId
var iqMatchingClients = ctx.Clients
.Where(_client => _client.AliasLinkId == entity.AliasLinkId);
var linkedPermissionSet = new[] {Permission.Banned, Permission.Flagged};
// if their permission level has been changed to level that needs to be updated on all accounts
if (linkedPermissionSet.Contains(newPermission) || linkedPermissionSet.Contains(oldPermission))
// this updates the level for all the clients with the same LinkId
// only if their new level is flagged or banned
await iqMatchingClients.ForEachAsync(_client =>
{
//get all clients that have the same linkId
var iqMatchingClients = ctx.Clients
.Where(_client => _client.AliasLinkId == entity.AliasLinkId);
_client.Level = newPermission;
_logger.LogInformation("Updated linked {clientId} to {newPermission}", _client.ClientId,
newPermission);
});
// this updates the level for all the clients with the same LinkId
// only if their new level is flagged or banned
await iqMatchingClients.ForEachAsync(_client =>
{
_client.Level = newPermission;
_logger.LogInformation("Updated linked {clientId} to {newPermission}", _client.ClientId,
newPermission);
});
await ctx.SaveChangesAsync();
}
await ctx.SaveChangesAsync();
}
}
@ -308,15 +306,7 @@ namespace SharedLibraryCore.Services
public async Task<EFClient> Delete(EFClient entity)
{
using (var context = new DatabaseContext())
{
var client = context.Clients
.Single(e => e.ClientId == entity.ClientId);
entity.Active = false;
context.Entry(entity).State = EntityState.Modified;
await context.SaveChangesAsync();
return entity;
}
throw new NotImplementedException();
}
public Task<IList<EFClient>> Find(Func<EFClient, bool> e)
@ -327,72 +317,71 @@ namespace SharedLibraryCore.Services
public async Task<EFClient> Get(int entityId)
{
// todo: this needs to be optimized for large linked accounts
using (var context = new DatabaseContext(true))
await using var context = _contextFactory.CreateContext(false);
var client = context.Clients
.Select(_client => new EFClient()
{
ClientId = _client.ClientId,
AliasLinkId = _client.AliasLinkId,
Level = _client.Level,
Connections = _client.Connections,
FirstConnection = _client.FirstConnection,
LastConnection = _client.LastConnection,
Masked = _client.Masked,
NetworkId = _client.NetworkId,
CurrentAlias = new EFAlias()
{
Name = _client.CurrentAlias.Name,
IPAddress = _client.CurrentAlias.IPAddress
},
TotalConnectionTime = _client.TotalConnectionTime
})
.FirstOrDefault(_client => _client.ClientId == entityId);
if (client == null)
{
var client = context.Clients
.Select(_client => new EFClient()
{
ClientId = _client.ClientId,
AliasLinkId = _client.AliasLinkId,
Level = _client.Level,
Connections = _client.Connections,
FirstConnection = _client.FirstConnection,
LastConnection = _client.LastConnection,
Masked = _client.Masked,
NetworkId = _client.NetworkId,
CurrentAlias = new EFAlias()
{
Name = _client.CurrentAlias.Name,
IPAddress = _client.CurrentAlias.IPAddress
},
TotalConnectionTime = _client.TotalConnectionTime
})
.FirstOrDefault(_client => _client.ClientId == entityId);
if (client == null)
{
return null;
}
client.AliasLink = new EFAliasLink()
{
AliasLinkId = client.AliasLinkId,
Children = await context.Aliases
.Where(_alias => _alias.LinkId == client.AliasLinkId)
.Select(_alias => new EFAlias()
{
Name = _alias.Name,
IPAddress = _alias.IPAddress
}).ToListAsync()
};
var foundClient = new
{
Client = client,
LinkedAccounts = await context.Clients.Where(_client => _client.AliasLinkId == client.AliasLinkId)
.Select(_linkedClient => new
{
_linkedClient.ClientId,
_linkedClient.NetworkId
})
.ToListAsync()
};
if (foundClient == null)
{
return null;
}
foundClient.Client.LinkedAccounts = new Dictionary<int, long>();
// todo: find out the best way to do this
// I'm doing this here because I don't know the best way to have multiple awaits in the query
foreach (var linked in foundClient.LinkedAccounts)
{
foundClient.Client.LinkedAccounts.Add(linked.ClientId, linked.NetworkId);
}
return foundClient.Client;
return null;
}
client.AliasLink = new EFAliasLink()
{
AliasLinkId = client.AliasLinkId,
Children = await context.Aliases
.Where(_alias => _alias.LinkId == client.AliasLinkId)
.Select(_alias => new EFAlias()
{
Name = _alias.Name,
IPAddress = _alias.IPAddress
}).ToListAsync()
};
var foundClient = new
{
Client = client,
LinkedAccounts = await context.Clients.Where(_client => _client.AliasLinkId == client.AliasLinkId)
.Select(_linkedClient => new
{
_linkedClient.ClientId,
_linkedClient.NetworkId
})
.ToListAsync()
};
if (foundClient == null)
{
return null;
}
foundClient.Client.LinkedAccounts = new Dictionary<int, long>();
// todo: find out the best way to do this
// I'm doing this here because I don't know the best way to have multiple awaits in the query
foreach (var linked in foundClient.LinkedAccounts)
{
foundClient.Client.LinkedAccounts.Add(linked.ClientId, linked.NetworkId);
}
return foundClient.Client;
}
private static readonly Func<DatabaseContext, long, Task<EFClient>> _getUniqueQuery =
@ -418,30 +407,27 @@ namespace SharedLibraryCore.Services
public virtual async Task<EFClient> GetUnique(long entityAttribute)
{
using (var context = new DatabaseContext(true))
{
return await _getUniqueQuery(context, entityAttribute);
}
await using var context = _contextFactory.CreateContext(false);
return await _getUniqueQuery(context, entityAttribute);
}
public async Task UpdateAlias(EFClient temporalClient)
{
using (var context = new DatabaseContext())
{
var entity = context.Clients
.Include(c => c.AliasLink)
.Include(c => c.CurrentAlias)
.First(e => e.ClientId == temporalClient.ClientId);
await using var context = _contextFactory.CreateContext();
entity.CurrentServer = temporalClient.CurrentServer;
var entity = context.Clients
.Include(c => c.AliasLink)
.Include(c => c.CurrentAlias)
.First(e => e.ClientId == temporalClient.ClientId);
await UpdateAlias(temporalClient.Name, temporalClient.IPAddress, entity, context);
entity.CurrentServer = temporalClient.CurrentServer;
temporalClient.CurrentAlias = entity.CurrentAlias;
temporalClient.CurrentAliasId = entity.CurrentAliasId;
temporalClient.AliasLink = entity.AliasLink;
temporalClient.AliasLinkId = entity.AliasLinkId;
}
await UpdateAlias(temporalClient.Name, temporalClient.IPAddress, entity, context);
temporalClient.CurrentAlias = entity.CurrentAlias;
temporalClient.CurrentAliasId = entity.CurrentAliasId;
temporalClient.AliasLink = entity.AliasLink;
temporalClient.AliasLinkId = entity.AliasLinkId;
}
public async Task<EFClient> Update(EFClient temporalClient)
@ -453,54 +439,57 @@ namespace SharedLibraryCore.Services
return null;
}
using (var context = new DatabaseContext())
await using var context = _contextFactory.CreateContext();
// grab the context version of the entity
var entity = context.Clients
.First(client => client.ClientId == temporalClient.ClientId);
if (temporalClient.LastConnection > entity.LastConnection)
{
// grab the context version of the entity
var entity = context.Clients
.First(client => client.ClientId == temporalClient.ClientId);
if (temporalClient.LastConnection > entity.LastConnection)
{
entity.LastConnection = temporalClient.LastConnection;
}
if (temporalClient.Connections > entity.Connections)
{
entity.Connections = temporalClient.Connections;
}
entity.Masked = temporalClient.Masked;
if (temporalClient.TotalConnectionTime > entity.TotalConnectionTime)
{
entity.TotalConnectionTime = temporalClient.TotalConnectionTime;
}
if (temporalClient.Password != null)
{
entity.Password = temporalClient.Password;
}
if (temporalClient.PasswordSalt != null)
{
entity.PasswordSalt = temporalClient.PasswordSalt;
}
// update in database
await context.SaveChangesAsync();
return entity;
entity.LastConnection = temporalClient.LastConnection;
}
if (temporalClient.Connections > entity.Connections)
{
entity.Connections = temporalClient.Connections;
}
entity.Masked = temporalClient.Masked;
if (temporalClient.TotalConnectionTime > entity.TotalConnectionTime)
{
entity.TotalConnectionTime = temporalClient.TotalConnectionTime;
}
if (temporalClient.Password != null)
{
entity.Password = temporalClient.Password;
}
if (temporalClient.PasswordSalt != null)
{
entity.PasswordSalt = temporalClient.PasswordSalt;
}
// update in database
await context.SaveChangesAsync();
return entity;
}
#region ServiceSpecific
public async Task<IList<EFClient>> GetOwners()
{
using (var context = new DatabaseContext())
{
return await context.Clients
.Where(c => c.Level == Permission.Owner)
.ToListAsync();
}
await using var context = _contextFactory.CreateContext(false);
return await context.Clients
.Where(c => c.Level == Permission.Owner)
.ToListAsync();
}
public async Task<bool> HasOwnerAsync(CancellationToken token)
{
await using var context = _contextFactory.CreateContext(false);
return await context.Clients.AnyAsync(client => client.Level == Permission.Owner, token);
}
/// <summary>
@ -510,55 +499,50 @@ namespace SharedLibraryCore.Services
/// <returns></returns>
public virtual async Task<int> GetOwnerCount()
{
using (var ctx = new DatabaseContext(true))
{
return await ctx.Clients
.CountAsync(_client => _client.Level == Permission.Owner);
}
await using var context = _contextFactory.CreateContext(false);
return await context.Clients
.CountAsync(_client => _client.Level == Permission.Owner);
}
public async Task<EFClient> GetClientForLogin(int clientId)
{
using (var ctx = new DatabaseContext(true))
{
return await ctx.Clients
.Select(_client => new EFClient()
await using var context = _contextFactory.CreateContext(false);
return await context.Clients
.Select(_client => new EFClient()
{
NetworkId = _client.NetworkId,
ClientId = _client.ClientId,
CurrentAlias = new EFAlias()
{
NetworkId = _client.NetworkId,
ClientId = _client.ClientId,
CurrentAlias = new EFAlias()
{
Name = _client.CurrentAlias.Name
},
Password = _client.Password,
PasswordSalt = _client.PasswordSalt,
Level = _client.Level
})
.FirstAsync(_client => _client.ClientId == clientId);
}
Name = _client.CurrentAlias.Name
},
Password = _client.Password,
PasswordSalt = _client.PasswordSalt,
Level = _client.Level
})
.FirstAsync(_client => _client.ClientId == clientId);
}
public async Task<List<EFClient>> GetPrivilegedClients(bool includeName = true)
{
using (var context = new DatabaseContext(disableTracking: true))
{
var iqClients = from client in context.Clients.AsNoTracking()
where client.Level >= Permission.Trusted
where client.Active
select new EFClient()
{
AliasLinkId = client.AliasLinkId,
CurrentAlias = client.CurrentAlias,
ClientId = client.ClientId,
Level = client.Level,
Password = client.Password,
PasswordSalt = client.PasswordSalt,
NetworkId = client.NetworkId,
LastConnection = client.LastConnection
};
await using var context = _contextFactory.CreateContext(false);
return await iqClients.ToListAsync();
}
var iqClients = from client in context.Clients.AsNoTracking()
where client.Level >= Permission.Trusted
where client.Active
select new EFClient()
{
AliasLinkId = client.AliasLinkId,
CurrentAlias = client.CurrentAlias,
ClientId = client.ClientId,
Level = client.Level,
Password = client.Password,
PasswordSalt = client.PasswordSalt,
NetworkId = client.NetworkId,
LastConnection = client.LastConnection
};
return await iqClients.ToListAsync();
}
public async Task<IList<PlayerInfo>> FindClientsByIdentifier(string identifier)
@ -568,71 +552,67 @@ namespace SharedLibraryCore.Services
return new List<PlayerInfo>();
}
using (var context = new DatabaseContext(disableTracking: true))
await using var context = _contextFactory.CreateContext(false);
long? networkId = null;
try
{
long? networkId = null;
try
{
networkId = identifier.ConvertGuidToLong(System.Globalization.NumberStyles.HexNumber);
}
catch { }
int? ipAddress = identifier.ConvertToIP();
IQueryable<EFAlias> iqLinkIds = context.Aliases.Where(_alias => _alias.Active);
// we want to query for the IP ADdress
if (ipAddress != null)
{
iqLinkIds = iqLinkIds.Where(_alias => _alias.IPAddress == ipAddress);
}
// want to find them by name (wildcard)
else
{
iqLinkIds = iqLinkIds.Where(_alias => EF.Functions.Like((_alias.SearchableName ?? _alias.Name.ToLower()), $"%{identifier.ToLower()}%"));
}
var linkIds = await iqLinkIds
.Select(_alias => _alias.LinkId)
.ToListAsync();
// get all the clients that match the alias link or the network id
var iqClients = context.Clients
.Where(_client => _client.Active);
iqClients = iqClients.Where(_client => networkId == _client.NetworkId || linkIds.Contains(_client.AliasLinkId));
// we want to project our results
var iqClientProjection = iqClients.OrderByDescending(_client => _client.LastConnection)
.Select(_client => new PlayerInfo()
{
Name = _client.CurrentAlias.Name,
LevelInt = (int)_client.Level,
LastConnection = _client.LastConnection,
ClientId = _client.ClientId,
});
var clients = await iqClientProjection.ToListAsync();
// this is so we don't try to evaluate this in the linq to entities query
foreach (var client in clients)
{
client.Level = ((Permission)client.LevelInt).ToLocalizedLevelName();
}
return clients;
networkId = identifier.ConvertGuidToLong(System.Globalization.NumberStyles.HexNumber);
}
catch { }
int? ipAddress = identifier.ConvertToIP();
IQueryable<EFAlias> iqLinkIds = context.Aliases.Where(_alias => _alias.Active);
// we want to query for the IP ADdress
if (ipAddress != null)
{
iqLinkIds = iqLinkIds.Where(_alias => _alias.IPAddress == ipAddress);
}
// want to find them by name (wildcard)
else
{
iqLinkIds = iqLinkIds.Where(_alias => EF.Functions.Like((_alias.SearchableName ?? _alias.Name.ToLower()), $"%{identifier.ToLower()}%"));
}
var linkIds = await iqLinkIds
.Select(_alias => _alias.LinkId)
.ToListAsync();
// get all the clients that match the alias link or the network id
var iqClients = context.Clients
.Where(_client => _client.Active);
iqClients = iqClients.Where(_client => networkId == _client.NetworkId || linkIds.Contains(_client.AliasLinkId));
// we want to project our results
var iqClientProjection = iqClients.OrderByDescending(_client => _client.LastConnection)
.Select(_client => new PlayerInfo()
{
Name = _client.CurrentAlias.Name,
LevelInt = (int)_client.Level,
LastConnection = _client.LastConnection,
ClientId = _client.ClientId,
});
var clients = await iqClientProjection.ToListAsync();
// this is so we don't try to evaluate this in the linq to entities query
foreach (var client in clients)
{
client.Level = ((Permission)client.LevelInt).ToLocalizedLevelName();
}
return clients;
}
public async Task<int> GetTotalClientsAsync()
{
using (var context = new DatabaseContext(true))
{
return await context.Clients
.CountAsync();
}
await using var context = _contextFactory.CreateContext(false);
return await context.Clients
.CountAsync();
}
/// <summary>
@ -641,13 +621,11 @@ namespace SharedLibraryCore.Services
/// <returns></returns>
public async Task<int> GetRecentClientCount()
{
using (var context = new DatabaseContext(true))
{
var startOfPeriod = DateTime.UtcNow.AddHours(-24);
var iqQuery = context.Clients.Where(_client => _client.LastConnection >= startOfPeriod);
await using var context = _contextFactory.CreateContext(false);
var startOfPeriod = DateTime.UtcNow.AddHours(-24);
var iqQuery = context.Clients.Where(_client => _client.LastConnection >= startOfPeriod);
return await iqQuery.CountAsync();
}
return await iqQuery.CountAsync();
}
/// <summary>
@ -658,22 +636,20 @@ namespace SharedLibraryCore.Services
{
var startOfPeriod = DateTime.UtcNow.AddHours(-24);
using (var context = new DatabaseContext(true))
{
var iqClients = context.Clients
.Where(_client => _client.CurrentAlias.IPAddress != null)
.Where(_client => _client.FirstConnection >= startOfPeriod)
.OrderByDescending(_client => _client.FirstConnection)
.Select(_client => new PlayerInfo()
{
ClientId = _client.ClientId,
Name = _client.CurrentAlias.Name,
IPAddress = _client.CurrentAlias.IPAddress.ConvertIPtoString(),
LastConnection = _client.FirstConnection
});
await using var context = _contextFactory.CreateContext(false);
var iqClients = context.Clients
.Where(_client => _client.CurrentAlias.IPAddress != null)
.Where(_client => _client.FirstConnection >= startOfPeriod)
.OrderByDescending(_client => _client.FirstConnection)
.Select(_client => new PlayerInfo()
{
ClientId = _client.ClientId,
Name = _client.CurrentAlias.Name,
IPAddress = _client.CurrentAlias.IPAddress.ConvertIPtoString(),
LastConnection = _client.FirstConnection
});
return await iqClients.ToListAsync();
}
return await iqClients.ToListAsync();
}
#endregion
@ -684,14 +660,12 @@ namespace SharedLibraryCore.Services
/// <returns></returns>
public async Task<int> GetClientReportCount(int clientId)
{
using (var ctx = new DatabaseContext(true))
{
return await ctx.Penalties
.Where(_penalty => _penalty.Active)
.Where(_penalty => _penalty.OffenderId == clientId)
.Where(_penalty => _penalty.Type == EFPenalty.PenaltyType.Report)
.CountAsync();
}
await using var context = _contextFactory.CreateContext(false);
return await context.Penalties
.Where(_penalty => _penalty.Active)
.Where(_penalty => _penalty.OffenderId == clientId)
.Where(_penalty => _penalty.Type == EFPenalty.PenaltyType.Report)
.CountAsync();
}
/// <summary>
@ -701,17 +675,16 @@ namespace SharedLibraryCore.Services
/// <returns></returns>
public async Task<bool> IsAutoFlagged(int clientId)
{
using (var ctx = new DatabaseContext(true))
{
var now = DateTime.UtcNow;
return await ctx.Penalties
.Where(_penalty => _penalty.Active)
.Where(_penalty => _penalty.OffenderId == clientId)
.Where(_penalty => _penalty.Type == EFPenalty.PenaltyType.Flag)
.Where(_penalty => _penalty.PunisherId == 1)
.Where(_penalty => _penalty.Expires == null || _penalty.Expires > now)
.AnyAsync();
}
await using var context = _contextFactory.CreateContext(false);
var now = DateTime.UtcNow;
return await context.Penalties
.Where(_penalty => _penalty.Active)
.Where(_penalty => _penalty.OffenderId == clientId)
.Where(_penalty => _penalty.Type == EFPenalty.PenaltyType.Flag)
.Where(_penalty => _penalty.PunisherId == 1)
.Where(_penalty => _penalty.Expires == null || _penalty.Expires > now)
.AnyAsync();
}
/// <summary>
@ -721,22 +694,20 @@ namespace SharedLibraryCore.Services
/// <returns></returns>
public async Task UnlinkClient(int clientId)
{
using (var ctx = new DatabaseContext())
{
var newLink = new EFAliasLink() { Active = true };
ctx.AliasLinks.Add(newLink);
await ctx.SaveChangesAsync();
await using var ctx = _contextFactory.CreateContext();
var newLink = new EFAliasLink() { Active = true };
ctx.AliasLinks.Add(newLink);
await ctx.SaveChangesAsync();
var client = await ctx.Clients.Include(_client => _client.CurrentAlias)
.FirstAsync(_client => _client.ClientId == clientId);
client.AliasLinkId = newLink.AliasLinkId;
client.Level = Permission.User;
var client = await ctx.Clients.Include(_client => _client.CurrentAlias)
.FirstAsync(_client => _client.ClientId == clientId);
client.AliasLinkId = newLink.AliasLinkId;
client.Level = Permission.User;
await ctx.Aliases.Where(_alias => _alias.IPAddress == client.IPAddress)
.ForEachAsync(_alias => _alias.LinkId = newLink.AliasLinkId);
await ctx.Aliases.Where(_alias => _alias.IPAddress == client.IPAddress)
.ForEachAsync(_alias => _alias.LinkId = newLink.AliasLinkId);
await ctx.SaveChangesAsync();
}
await ctx.SaveChangesAsync();
}
/// <summary>
@ -747,7 +718,7 @@ namespace SharedLibraryCore.Services
public async Task<ResourceQueryHelperResult<FindClientResult>> QueryResource(FindClientRequest query)
{
var result = new ResourceQueryHelperResult<FindClientResult>();
using var context = _contextFactory.CreateContext(enableTracking: false);
await using var context = _contextFactory.CreateContext(enableTracking: false);
IQueryable<EFClient> iqClients = null;

View File

@ -7,32 +7,38 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Services
{
public class PenaltyService : Interfaces.IEntityService<EFPenalty>
{
private readonly IDatabaseContextFactory _contextFactory;
public PenaltyService(IDatabaseContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
public virtual async Task<EFPenalty> Create(EFPenalty newEntity)
{
using (var context = new DatabaseContext())
await using var context = _contextFactory.CreateContext();
var penalty = new EFPenalty()
{
var penalty = new EFPenalty()
{
Active = true,
OffenderId = newEntity.Offender.ClientId,
PunisherId = newEntity.Punisher.ClientId,
LinkId = newEntity.Link.AliasLinkId,
Type = newEntity.Type,
Expires = newEntity.Expires,
Offense = newEntity.Offense,
When = DateTime.UtcNow,
AutomatedOffense = newEntity.AutomatedOffense ?? newEntity.Punisher.AdministeredPenalties?.FirstOrDefault()?.AutomatedOffense,
IsEvadedOffense = newEntity.IsEvadedOffense
};
Active = true,
OffenderId = newEntity.Offender.ClientId,
PunisherId = newEntity.Punisher.ClientId,
LinkId = newEntity.Link.AliasLinkId,
Type = newEntity.Type,
Expires = newEntity.Expires,
Offense = newEntity.Offense,
When = DateTime.UtcNow,
AutomatedOffense = newEntity.AutomatedOffense ?? newEntity.Punisher.AdministeredPenalties?.FirstOrDefault()?.AutomatedOffense,
IsEvadedOffense = newEntity.IsEvadedOffense
};
context.Penalties.Add(penalty);
await context.SaveChangesAsync();
}
context.Penalties.Add(penalty);
await context.SaveChangesAsync();
return newEntity;
}
@ -64,32 +70,30 @@ namespace SharedLibraryCore.Services
public async Task<IList<PenaltyInfo>> GetRecentPenalties(int count, int offset, EFPenalty.PenaltyType showOnly = EFPenalty.PenaltyType.Any, bool ignoreAutomated = true)
{
using (var context = new DatabaseContext(true))
{
var iqPenalties = context.Penalties
.Where(p => showOnly == EFPenalty.PenaltyType.Any ? p.Type != EFPenalty.PenaltyType.Any : p.Type == showOnly)
.Where(_penalty => ignoreAutomated ? _penalty.PunisherId != 1 : true)
.OrderByDescending(p => p.When)
.Skip(offset)
.Take(count)
.Select(_penalty => new PenaltyInfo()
{
Id = _penalty.PenaltyId,
Offense = _penalty.Offense,
AutomatedOffense = _penalty.AutomatedOffense,
OffenderId = _penalty.OffenderId,
OffenderName = _penalty.Offender.CurrentAlias.Name,
PunisherId = _penalty.PunisherId,
PunisherName = _penalty.Punisher.CurrentAlias.Name,
PunisherLevel = _penalty.Punisher.Level,
PenaltyType = _penalty.Type,
Expires = _penalty.Expires,
TimePunished = _penalty.When,
IsEvade = _penalty.IsEvadedOffense
});
await using var context = _contextFactory.CreateContext(false);
var iqPenalties = context.Penalties
.Where(p => showOnly == EFPenalty.PenaltyType.Any ? p.Type != EFPenalty.PenaltyType.Any : p.Type == showOnly)
.Where(_penalty => ignoreAutomated ? _penalty.PunisherId != 1 : true)
.OrderByDescending(p => p.When)
.Skip(offset)
.Take(count)
.Select(_penalty => new PenaltyInfo()
{
Id = _penalty.PenaltyId,
Offense = _penalty.Offense,
AutomatedOffense = _penalty.AutomatedOffense,
OffenderId = _penalty.OffenderId,
OffenderName = _penalty.Offender.CurrentAlias.Name,
PunisherId = _penalty.PunisherId,
PunisherName = _penalty.Punisher.CurrentAlias.Name,
PunisherLevel = _penalty.Punisher.Level,
PenaltyType = _penalty.Type,
Expires = _penalty.Expires,
TimePunished = _penalty.When,
IsEvade = _penalty.IsEvadedOffense
});
return await iqPenalties.ToListAsync();
}
return await iqPenalties.ToListAsync();
}
/// <summary>
@ -104,37 +108,35 @@ namespace SharedLibraryCore.Services
{
var linkedPenaltyType = Utilities.LinkedPenaltyTypes();
using (var ctx = new DatabaseContext(true))
{
var linkId = await ctx.Clients.AsNoTracking()
.Where(_penalty => _penalty.ClientId == clientId)
.Select(_penalty => _penalty.AliasLinkId)
.FirstOrDefaultAsync();
await using var context = _contextFactory.CreateContext(false);
var linkId = await context.Clients.AsNoTracking()
.Where(_penalty => _penalty.ClientId == clientId)
.Select(_penalty => _penalty.AliasLinkId)
.FirstOrDefaultAsync();
var iqPenalties = ctx.Penalties.AsNoTracking()
.Where(_penalty => _penalty.OffenderId == clientId || _penalty.PunisherId == clientId || (linkedPenaltyType.Contains(_penalty.Type) && _penalty.LinkId == linkId))
.Where(_penalty => _penalty.When <= startAt)
.OrderByDescending(_penalty => _penalty.When)
.Skip(offset)
.Take(count)
.Select(_penalty => new PenaltyInfo()
{
Id = _penalty.PenaltyId,
Offense = _penalty.Offense,
AutomatedOffense = _penalty.AutomatedOffense,
OffenderId = _penalty.OffenderId,
OffenderName = _penalty.Offender.CurrentAlias.Name,
PunisherId = _penalty.PunisherId,
PunisherName = _penalty.Punisher.CurrentAlias.Name,
PunisherLevel = _penalty.Punisher.Level,
PenaltyType = _penalty.Type,
Expires = _penalty.Expires,
TimePunished = _penalty.When,
IsEvade = _penalty.IsEvadedOffense
});
var iqPenalties = context.Penalties.AsNoTracking()
.Where(_penalty => _penalty.OffenderId == clientId || _penalty.PunisherId == clientId || (linkedPenaltyType.Contains(_penalty.Type) && _penalty.LinkId == linkId))
.Where(_penalty => _penalty.When <= startAt)
.OrderByDescending(_penalty => _penalty.When)
.Skip(offset)
.Take(count)
.Select(_penalty => new PenaltyInfo()
{
Id = _penalty.PenaltyId,
Offense = _penalty.Offense,
AutomatedOffense = _penalty.AutomatedOffense,
OffenderId = _penalty.OffenderId,
OffenderName = _penalty.Offender.CurrentAlias.Name,
PunisherId = _penalty.PunisherId,
PunisherName = _penalty.Punisher.CurrentAlias.Name,
PunisherLevel = _penalty.Punisher.Level,
PenaltyType = _penalty.Type,
Expires = _penalty.Expires,
TimePunished = _penalty.When,
IsEvade = _penalty.IsEvadedOffense
});
return await iqPenalties.Distinct().ToListAsync();
}
return await iqPenalties.Distinct().ToListAsync();
}
public async Task<List<EFPenalty>> GetActivePenaltiesAsync(int linkId, int? ip = null, bool includePunisherName = false)
@ -150,42 +152,39 @@ namespace SharedLibraryCore.Services
p.Active &&
(p.Expires == null || p.Expires > now));
using (var context = new DatabaseContext(true))
{
var iqLinkPenalties = context.Penalties
.Where(p => p.LinkId == linkId)
.Where(filter);
await using var context = _contextFactory.CreateContext(false);
var iqLinkPenalties = context.Penalties
.Where(p => p.LinkId == linkId)
.Where(filter);
var iqIPPenalties = context.Aliases
.Where(a => a.IPAddress != null && a.IPAddress == ip)
.SelectMany(a => a.Link.ReceivedPenalties)
.Where(filter);
var iqIPPenalties = context.Aliases
.Where(a => a.IPAddress != null && a.IPAddress == ip)
.SelectMany(a => a.Link.ReceivedPenalties)
.Where(filter);
var activePenalties = (await iqLinkPenalties.ToListAsync())
.Union(await iqIPPenalties.ToListAsync())
.Distinct();
var activePenalties = (await iqLinkPenalties.ToListAsync())
.Union(await iqIPPenalties.ToListAsync())
.Distinct();
// this is a bit more performant in memory (ordering)
return activePenalties.OrderByDescending(p => p.When).ToList();
}
// this is a bit more performant in memory (ordering)
return activePenalties.OrderByDescending(p => p.When).ToList();
}
public virtual async Task RemoveActivePenalties(int aliasLinkId)
{
using (var context = new DatabaseContext())
{
var now = DateTime.UtcNow;
await context.Penalties
.Where(p => p.LinkId == aliasLinkId)
.Where(p => p.Expires > now || p.Expires == null)
.ForEachAsync(p =>
{
p.Active = false;
p.Expires = now;
});
await using var context = _contextFactory.CreateContext();
await context.SaveChangesAsync();
}
var now = DateTime.UtcNow;
await context.Penalties
.Where(p => p.LinkId == aliasLinkId)
.Where(p => p.Expires > now || p.Expires == null)
.ForEachAsync(p =>
{
p.Active = false;
p.Expires = now;
});
await context.SaveChangesAsync();
}
}
}