mirror of
https://github.com/donnaskiez/ac.git
synced 2024-11-21 22:24:08 +01:00
get entity model workin
This commit is contained in:
parent
9e7fdb19f6
commit
bebcce9ac9
10 changed files with 214 additions and 86 deletions
|
@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace server.Database
|
||||
{
|
||||
public class ClientReport : DatabaseConnection
|
||||
public class ClientReport
|
||||
{
|
||||
private ILogger _logger;
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace server.Database
|
||||
{
|
||||
public class ClientSend : DatabaseConnection
|
||||
public class ClientSend
|
||||
{
|
||||
private ILogger _logger;
|
||||
|
||||
|
@ -32,12 +32,12 @@ namespace server.Database
|
|||
|
||||
switch (reportCode)
|
||||
{
|
||||
case (int)ReportCodes.MODULE_VERIFICATION_CHECKSUM_FAILURE:
|
||||
/* case (int)ReportCodes.MODULE_VERIFICATION_CHECKSUM_FAILURE:
|
||||
InsertReportWithCode10((MODULE_VERIFICATION_CHECKSUM_FAILURE)Convert.ChangeType(report, typeof(MODULE_VERIFICATION_CHECKSUM_FAILURE)));
|
||||
break;
|
||||
default:
|
||||
_logger.LogError("Unknown report code: {0}", reportCode);
|
||||
break;
|
||||
break;*/
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
using Microsoft.AspNetCore.Routing;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace server.Database
|
||||
{
|
||||
public class DatabaseConnection : IDisposable
|
||||
{
|
||||
private MySqlConnection _connection;
|
||||
|
||||
public DatabaseConnection()
|
||||
{
|
||||
string connectionString = @"server=localhost;userid=root;password=root;database=ac_db";
|
||||
_connection = new MySqlConnection(connectionString);
|
||||
}
|
||||
|
||||
public void Open()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.Open();
|
||||
}
|
||||
catch(MySqlException ex)
|
||||
{
|
||||
throw new DatabaseConnectionException("Cannot connect to server.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.Close();
|
||||
}
|
||||
catch(MySqlException ex)
|
||||
{
|
||||
throw new DatabaseConnectionException("Cannot disconnect from server.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
public class DatabaseConnectionException : Exception
|
||||
{
|
||||
internal DatabaseConnectionException(string message, MySqlException inner) : base(message, inner)
|
||||
{
|
||||
}
|
||||
|
||||
public int ErrorCode => ((MySqlException)InnerException).ErrorCode;
|
||||
}
|
||||
}
|
29
server/Database/Entity/HardwareConfiguration.cs
Normal file
29
server/Database/Entity/HardwareConfiguration.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using server.Database.Model;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace server.Database.Entity
|
||||
{
|
||||
public class HardwareConfigurationEntity : HardwareConfiguration
|
||||
{
|
||||
private readonly ModelContext _modelContext;
|
||||
|
||||
public HardwareConfigurationEntity(ModelContext modelContext)
|
||||
{
|
||||
_modelContext = modelContext;
|
||||
}
|
||||
|
||||
public bool CheckIfHardwareConfigurationExists()
|
||||
{
|
||||
return _modelContext.HardwareConfiguration.Any(h =>
|
||||
h.MotherboardSerial == MotherboardSerial &&
|
||||
h.DeviceDrive0Serial == DeviceDrive0Serial);
|
||||
}
|
||||
}
|
||||
}
|
48
server/Database/Entity/User.cs
Normal file
48
server/Database/Entity/User.cs
Normal file
|
@ -0,0 +1,48 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using server.Database.Model;
|
||||
using Serilog;
|
||||
|
||||
namespace server.Database.Entity
|
||||
{
|
||||
public class UserEntity : User
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ModelContext _modelContext;
|
||||
public HardwareConfigurationEntity HardwareConfigurationEntity { get; set; }
|
||||
|
||||
public UserEntity(ILogger logger, ModelContext modelContext)
|
||||
{
|
||||
_logger = logger;
|
||||
_modelContext = modelContext;
|
||||
HardwareConfigurationEntity = new HardwareConfigurationEntity(_modelContext);
|
||||
}
|
||||
|
||||
public bool CheckIfUserExists()
|
||||
{
|
||||
return _modelContext.Users.Any(u => u.Steam64Id == Steam64Id);
|
||||
}
|
||||
|
||||
public bool CheckIfUserIsBanned()
|
||||
{
|
||||
return _modelContext.Users.Any(u => u.Steam64Id == Steam64Id && u.IsBanned);
|
||||
}
|
||||
|
||||
public bool IsUsersHardwareBanned()
|
||||
{
|
||||
HardwareConfigurationEntity hwConfig = new HardwareConfigurationEntity(_modelContext);
|
||||
hwConfig.MotherboardSerial = HardwareConfigurationEntity.MotherboardSerial;
|
||||
hwConfig.DeviceDrive0Serial = HardwareConfigurationEntity.DeviceDrive0Serial;
|
||||
|
||||
return hwConfig.CheckIfHardwareConfigurationExists() && hwConfig.IsBanned;
|
||||
}
|
||||
|
||||
public void InsertUser()
|
||||
{
|
||||
_modelContext.Users.Add(this);
|
||||
}
|
||||
}
|
||||
}
|
28
server/Database/Model/Model.cs
Normal file
28
server/Database/Model/Model.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
||||
|
||||
namespace server.Database.Model
|
||||
{
|
||||
public class User
|
||||
{
|
||||
public int UserId { get; set; }
|
||||
public ulong Steam64Id { get; set; }
|
||||
public bool IsBanned { get; set; }
|
||||
public ICollection<HardwareConfiguration> HardwareConfigurations { get; set; }
|
||||
}
|
||||
|
||||
public class HardwareConfiguration
|
||||
{
|
||||
public int HardwareId { get; set; }
|
||||
public int UserId { get; set; }
|
||||
public virtual User User { get; set; }
|
||||
public bool IsBanned { get; set; }
|
||||
public ulong DeviceDrive0Serial { get; set; }
|
||||
public ulong MotherboardSerial { get; set; }
|
||||
}
|
||||
}
|
60
server/Database/Model/ModelContext.cs
Normal file
60
server/Database/Model/ModelContext.cs
Normal file
|
@ -0,0 +1,60 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using MySql.EntityFrameworkCore.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace server.Database.Model
|
||||
{
|
||||
public class ModelContext : DbContext
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<HardwareConfiguration> HardwareConfiguration { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseMySQL("server=localhost;userid=root;password=root;database=ac_db");
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<User>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.UserId);
|
||||
|
||||
entity.Property(e => e.UserId)
|
||||
.UseMySQLAutoIncrementColumn(entity.Property(e => e.UserId).Metadata.Name);
|
||||
|
||||
entity.Property(e => e.Steam64Id)
|
||||
.IsRequired();
|
||||
|
||||
entity.Property(e => e.IsBanned)
|
||||
.IsRequired();
|
||||
|
||||
entity.HasMany(g => g.HardwareConfigurations)
|
||||
.WithOne(s => s.User)
|
||||
.HasForeignKey(s => s.UserId)
|
||||
.HasPrincipalKey(keyExpression: e => e.UserId);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<HardwareConfiguration>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.HardwareId);
|
||||
|
||||
entity.Property(e => e.HardwareId)
|
||||
.UseMySQLAutoIncrementColumn(entity.Property(e => e.HardwareId).Metadata.Name);
|
||||
|
||||
entity.Property(f => f.IsBanned)
|
||||
.HasDefaultValue(false);
|
||||
|
||||
entity.HasOne(s => s.User)
|
||||
.WithMany(g => g.HardwareConfigurations)
|
||||
.HasForeignKey(s => s.UserId);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace server.Database
|
||||
{
|
||||
public class User : DatabaseConnection
|
||||
{
|
||||
private ILogger _logger;
|
||||
|
||||
public User(ILogger<User> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
using Serilog;
|
||||
using server.Database;
|
||||
using server.Database.Entity;
|
||||
using server.Database.Model;
|
||||
using System.Configuration;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
|
@ -15,8 +18,46 @@ namespace server
|
|||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
DatabaseConnection database = new DatabaseConnection();
|
||||
database.Open();
|
||||
using (var context = new ModelContext())
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
Database.Entity.UserEntity user = new Database.Entity.UserEntity(logger, context);
|
||||
|
||||
user.IsBanned = false;
|
||||
user.Steam64Id = 123123123;
|
||||
|
||||
user.HardwareConfigurationEntity = new HardwareConfigurationEntity(context);
|
||||
user.HardwareConfigurationEntity.MotherboardSerial = 987654321;
|
||||
user.HardwareConfigurationEntity.DeviceDrive0Serial = 123456789;
|
||||
|
||||
if (user.IsUsersHardwareBanned())
|
||||
{
|
||||
logger.Information("Users hardware is banned");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (user.CheckIfUserExists())
|
||||
{
|
||||
if (user.CheckIfUserIsBanned())
|
||||
{
|
||||
logger.Information("User is banned");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Information("User is not banned");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Information("User does not exist");
|
||||
|
||||
user.InsertUser();
|
||||
}
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
Server server = new Server(logger);
|
||||
await server.Listen();
|
||||
|
|
|
@ -9,7 +9,9 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
|
||||
<PackageReference Include="MySql.Data" Version="8.1.0" />
|
||||
<PackageReference Include="MySql.EntityFrameworkCore" Version="7.0.5" />
|
||||
<PackageReference Include="Serilog" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
|
|
Loading…
Reference in a new issue