1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-25 14:40:31 -05:00

Moved from SQLITE to EntityFramework.

Lots of things are broken!
This commit is contained in:
RaidMax
2017-11-25 19:29:58 -06:00
parent c56d98d11c
commit 23eb641113
61 changed files with 1690 additions and 1685 deletions

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using System.Data.Entity;
using SharedLibrary.Interfaces;
using SharedLibrary.Database.Models;
using SharedLibrary.Database;
namespace SharedLibrary.Services
{
public class AliasService : IEntityService<EFAlias>
{
public async Task<EFAlias> Create(EFAlias entity)
{
using (var context = new IW4MAdminDatabaseContext())
{
entity.Link = await context.AliasLinks.FirstAsync(a => a.AliasLinkId == entity.Link.AliasLinkId);
var addedEntity = context.Aliases.Add(entity);
await context.SaveChangesAsync();
return addedEntity;
}
}
public Task<EFAlias> CreateProxy()
{
return null;
}
public async Task<EFAlias> Delete(EFAlias entity)
{
using (var context = new IW4MAdminDatabaseContext())
{
entity = context.Aliases.Single(e => e.AliasId == entity.AliasId);
entity.Active = false;
context.Entry(entity).State = EntityState.Modified;
await context.SaveChangesAsync();
return entity;
}
}
public async Task<IList<EFAlias>> Find(Func<EFAlias, bool> expression)
{
using (var context = new IW4MAdminDatabaseContext())
return await Task.Run(() => context.Aliases.Where(expression).ToList());
}
public async Task<EFAlias> Get(int entityID)
{
using (var context = new IW4MAdminDatabaseContext())
return await context.Aliases
.SingleOrDefaultAsync(e => e.AliasId == entityID);
}
public Task<EFAlias> GetUnique(string entityProperty)
{
throw new NotImplementedException();
}
public async Task<EFAlias> Update(EFAlias entity)
{
using (var context = new IW4MAdminDatabaseContext())
{
entity = context.Aliases.Attach(entity);
context.Entry(entity).State = EntityState.Modified;
await context.SaveChangesAsync();
return entity;
}
}
public async Task<EFAliasLink> CreateLink(EFAliasLink link)
{
using (var context = new IW4MAdminDatabaseContext())
{
context.AliasLinks.Add(link);
await context.SaveChangesAsync();
return link;
}
}
}
}

View File

@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using SharedLibrary.Database;
using SharedLibrary.Database.Models;
using System.Linq.Expressions;
namespace SharedLibrary.Services
{
public class ClientService : Interfaces.IEntityService<EFClient>
{
private Dictionary<int, IW4MAdminDatabaseContext> _context;
public ClientService()
{
_context = new Dictionary<int, IW4MAdminDatabaseContext>();
}
public async Task<EFClient> Create(EFClient entity)
{
using (var context = new IW4MAdminDatabaseContext())
{
// get all aliases by IP
var alias = await context.Aliases.FirstOrDefaultAsync(a => a.IP == entity.IPAddress);
EFAliasLink link = alias?.Link;
var client = new EFClient()
{
Active = true,
Name = entity.Name,
FirstConnection = DateTime.UtcNow,
Connections = 1,
IPAddress = entity.IPAddress,
LastConnection = DateTime.UtcNow,
Level = Objects.Player.Permission.User,
Masked = false,
NetworkId = entity.NetworkId,
AliasLink = link ?? new EFAliasLink() { Active = true }
};
client.AliasLink.Children.Add(new EFAlias()
{
Active = true,
DateAdded = DateTime.UtcNow,
IP = entity.IPAddress,
Link = client.AliasLink,
Name = entity.Name
});
context.Clients.Add(client);
await context.SaveChangesAsync();
return client;
}
}
public async Task<EFClient> Delete(EFClient entity)
{
using (var context = new IW4MAdminDatabaseContext())
{
entity = context.Clients.Single(e => e.ClientId == entity.ClientId);
entity.Active = false;
entity.Level = Objects.Player.Permission.User;
context.Entry(entity).State = EntityState.Modified;
await context.SaveChangesAsync();
return entity;
}
}
public async Task<IList<EFClient>> Find(Func<EFClient, bool> e)
{
using (var context = new IW4MAdminDatabaseContext())
return await Task.Run(() => context.Clients
.Include(c => c.AliasLink.Children)
.Where(e).ToList());
}
public async Task<EFClient> Get(int entityID)
{
using (var context = new IW4MAdminDatabaseContext())
return await new IW4MAdminDatabaseContext().Clients
.Include(c => c.AliasLink.Children)
.SingleOrDefaultAsync(e => e.ClientId == entityID);
}
public async Task<EFClient> GetUnique(string entityAttribute)
{
using (var context = new IW4MAdminDatabaseContext())
{
return await context.Clients
.Include(c => c.AliasLink.Children)
.SingleOrDefaultAsync(c => c.NetworkId == entityAttribute);
}
}
public async Task<EFClient> Update(EFClient entity)
{
using (var context = new IW4MAdminDatabaseContext())
{
entity = context.Clients.Attach(entity);
context.Entry(entity).State = EntityState.Modified;
await context.SaveChangesAsync();
return entity;
}
}
#region ServiceSpecific
public async Task<IList<EFClient>> GetOwners()
{
using (var context = new IW4MAdminDatabaseContext())
return await context.Clients.Where(c => c.Level == Objects.Player.Permission.Owner).ToListAsync();
}
public async Task<IList<EFClient>> GetPrivilegedClients()
{
using (var context = new IW4MAdminDatabaseContext())
return await new IW4MAdminDatabaseContext().Clients
.Where(c => c.Level >= Objects.Player.Permission.Trusted)
.ToListAsync();
}
public async Task<IList<EFClient>> GetRecentClients(int offset, int count)
{
using (var context = new IW4MAdminDatabaseContext())
return await context.Clients.OrderByDescending(p => p.ClientId).Skip(offset).Take(count).ToListAsync();
}
public async Task<IList<EFClient>> PruneInactivePrivilegedClients(int inactiveDays)
{
using (var context = new IW4MAdminDatabaseContext())
{
var inactive = await context.Clients.Where(c => c.Level > Objects.Player.Permission.Flagged)
.Where(c => (DateTime.UtcNow - c.LastConnection).TotalDays >= inactiveDays)
.ToListAsync();
inactive.ForEach(c => c.Level = Objects.Player.Permission.User);
await context.SaveChangesAsync();
return inactive;
}
}
public async Task<int> GetTotalClientsAsync()
{
using (var context = new IW4MAdminDatabaseContext())
return await context.Clients.CountAsync();
}
public Task<EFClient> CreateProxy()
{
throw new NotImplementedException();
}
#endregion
}
}

View File

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using SharedLibrary.Database;
using SharedLibrary.Database.Models;
using System.Linq.Expressions;
namespace SharedLibrary.Services
{
public class PenaltyService : Interfaces.IEntityService<EFPenalty>
{
public async Task<EFPenalty> Create(EFPenalty entity)
{
using (var context = new IW4MAdminDatabaseContext())
{
entity.Offender = context.Clients.First(e => e.ClientId == entity.Offender.ClientId);
entity.Punisher = context.Clients.First(e => e.ClientId == entity.Punisher.ClientId);
entity.Link = context.AliasLinks.First(l => l.AliasLinkId == entity.Link.AliasLinkId);
if (entity.Expires == DateTime.MinValue)
entity.Expires = DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MaxValue.ToString());
context.Penalties.Add(entity);
await context.SaveChangesAsync();
return entity;
}
}
public Task<EFPenalty> CreateProxy()
{
throw new NotImplementedException();
}
public Task<EFPenalty> Delete(EFPenalty entity)
{
throw new NotImplementedException();
}
public async Task<IList<EFPenalty>> Find(Func<EFPenalty, bool> expression)
{
using (var context = new IW4MAdminDatabaseContext())
{
return await Task.Run(() => context.Penalties
.Include(p => p.Offender)
.Include(p => p.Punisher)
.Where(expression)
.Where(p => p.Active)
.ToList());
}
}
public Task<EFPenalty> Get(int entityID)
{
throw new NotImplementedException();
}
public Task<EFPenalty> GetUnique(string entityProperty)
{
throw new NotImplementedException();
}
public Task<EFPenalty> Update(EFPenalty entity)
{
throw new NotImplementedException();
}
public async Task<IList<EFPenalty>> GetRecentPenalties(int count, int offset)
{
using (var context = new IW4MAdminDatabaseContext())
return await context.Penalties
.Include(p => p.Offender)
.Include(p => p.Punisher)
.Where(p => p.Active)
.OrderByDescending(p => p.When)
.Skip(offset)
.Take(count)
.ToListAsync();
}
public async Task<IList<EFPenalty>> GetClientPenaltiesAsync(int clientId)
{
using (var context = new IW4MAdminDatabaseContext())
return await context.Penalties
.Where(p => p.OffenderId == clientId)
.Where(p => p.Active)
.Include(p => p.Offender)
.Include(p => p.Punisher)
.ToListAsync();
}
public async Task RemoveActivePenalties(int aliasLinkId)
{
using (var context = new IW4MAdminDatabaseContext())
{
var now = DateTime.UtcNow;
var penalties = await context.Penalties
.Include(p => p.Link.Children)
.Where(p => p.LinkId == aliasLinkId)
.Where(p => p.Expires > now)
.ToListAsync();
penalties.ForEach(async p =>
{
p.Active = false;
var clients = await context.Clients.Where(cl => cl.AliasLinkId == p.LinkId).ToListAsync();
foreach (var c in clients)
if (c.Level == Objects.Player.Permission.Banned)
c.Level = Objects.Player.Permission.User;
});
await context.SaveChangesAsync();
}
}
}
}