mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2025-12-12 14:34:39 +01:00
accounts db & http sdk db impl
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
|
||||
<PackageReference Include="protobuf-net" Version="3.2.30" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
34
BLHX.Server.Common/Database/Account.cs
Normal file
34
BLHX.Server.Common/Database/Account.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace BLHX.Server.Common.Database
|
||||
{
|
||||
public sealed class AccountContext : DbContext, IBLHXDBContext<AccountContext>
|
||||
{
|
||||
public static string DbPath => "Databases/accounts.db";
|
||||
public DbSet<Account> Accounts { get; set; }
|
||||
|
||||
public AccountContext()
|
||||
{
|
||||
Database.EnsureCreated();
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||
=> options.UseSqlite($"Data Source={((IBLHXDBContext)this).GetFullDbPath()}");
|
||||
}
|
||||
|
||||
[PrimaryKey(nameof(Uid))]
|
||||
public class Account
|
||||
{
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public uint Uid { get; set; }
|
||||
public string DeviceId { get; set; }
|
||||
public string Token { get; set; }
|
||||
|
||||
public Account(string deviceId, string token)
|
||||
{
|
||||
DeviceId = deviceId;
|
||||
Token = token;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
BLHX.Server.Common/Database/DBManager.cs
Normal file
36
BLHX.Server.Common/Database/DBManager.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using BLHX.Server.Common.Utils;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BLHX.Server.Common.Database
|
||||
{
|
||||
public static class DBManager
|
||||
{
|
||||
public static readonly Logger c = new(nameof(DBManager), ConsoleColor.DarkCyan);
|
||||
public static AccountContext AccountContext { get; }
|
||||
|
||||
static DBManager()
|
||||
{
|
||||
foreach (var dbCtx in Assembly.GetExecutingAssembly().GetTypes().Where(p => typeof(IBLHXDBContext).IsAssignableFrom(p) && !p.IsInterface))
|
||||
{
|
||||
var dbPath = (string)dbCtx.GetProperty(nameof(AccountContext.DbPath))!.GetValue(null)!;
|
||||
var saveDir = Path.GetDirectoryName(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, dbPath))!;
|
||||
if (!Directory.Exists(saveDir))
|
||||
Directory.CreateDirectory(saveDir);
|
||||
}
|
||||
|
||||
AccountContext = new AccountContext();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IBLHXDBContext
|
||||
{
|
||||
public static abstract string DbPath { get; }
|
||||
public string GetFullDbPath();
|
||||
}
|
||||
|
||||
public interface IBLHXDBContext<TSelf> : IBLHXDBContext where TSelf : DbContext
|
||||
{
|
||||
string IBLHXDBContext.GetFullDbPath() => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, (string)typeof(TSelf).GetProperty(nameof(DbPath))!.GetValue(null)!);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user